Prompt specification
Build or customize the Three.js 3D scene "Same population in two settlement shapes" 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: Same population in two settlement shapes
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/same-population-in-two-settlement-shapes-x1334
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts population density. Arrange the same number of household markers in a compact cluster and dispersed settlement. Display occupied area and keep population totals identical in both layouts. Voxel style.
Working Three.js Starter (same-population-in-two-settlement-shapes-x1334):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Population Density — Same People, Different Place</title>
<style>
:root {
--paper: #f3f1ea;
--surface: #ffffff;
--ink: #17231f;
--muted: #6e7772;
--line: #d9ddd6;
--compact: #e86f57;
--dispersed: #6e62d5;
--green: #218b73;
--amber: #f0b74d;
--violet: #786bd9;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button {
font: inherit;
}
.app {
height: 100dvh;
min-height: 700px;
display: grid;
grid-template-rows: auto minmax(380px, 1fr) auto;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 28px;
padding: 20px 28px;
background: rgba(255, 255, 255, 0.78);
border-bottom: 1px solid var(--line);
z-index: 10;
}
.brand {
min-width: 0;
}
.kicker {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.kicker::before {
content: "";
width: 9px;
height: 9px;
background: var(--green);
border-radius: 2px;
box-shadow: 0 0 0 4px rgba(33, 139, 115, 0.12);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 760;
}
.toolbar {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 10px;
flex-wrap: wrap;
}
.equilibrium {
display: flex;
align-items: center;
gap: 9px;
margin-right: 5px;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: #fafbf8;
color: var(--muted);
font-size: 12px;
white-space: nowrap;
}
.equilibrium b {
color: var(--ink);
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.equilibrium .equals {
color: var(--green);
font-size: 17px;
font-weight: 800;
}
.control {
min-height: 40px;
padding: 9px 13px;
color: var(--ink);
background: var(--surface);
border: 1px solid var(--line);
border-radius: 11px;
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
.control:hover {
transform: translateY(-1px);
border-color: #aeb9b2;
background: #fafcf9;
}
.control:focus-visible {
outline: 3px solid rgba(33, 139, 115, 0.25);
outline-offset: 2px;
}
.control[aria-pressed="true"] {
color: #fff;
background: var(--green);
border-color: var(--green);
}
.stage {
position: relative;
min-height: 0;
overflow: hidden;
isolation: isolate;
background: #e9eee8;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.divider {
position: absolute;
z-index: 3;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
background: rgba(23, 35, 31, 0.17);
pointer-events: none;
}
.divider::after {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 7px;
height: 7px;
border-radius: 50%;
background: #fff;
border: 1px solid rgba(23, 35, 31, 0.22);
transform: translate(-50%, -50%);
box-shadow: 0 2px 8px rgba(23, 35, 31, 0.12);
}
.view-label {
position: absolute;
z-index: 4;
top: 22px;
left: 25px;
min-width: 205px;
padding: 12px 14px;
border: 1px solid rgba(23, 35, 31, 0.12);
border-radius: 13px;
background: rgba(255, 255, 255, 0.88);
backdrop-filter: blur(12px);
box-shadow: 0 8px 30px rgba(28, 43, 37, 0.08);
pointer-events: none;
}
.view-label.dispersed {
left: calc(50% + 25px);
}
.label-heading {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.label-heading::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--compact);
}
.dispersed .label-heading::before {
background: var(--dispersed);
}
.label-meta {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
color: var(--muted);
font-size: 11px;
}
.label-meta span + span::before {
content: "·";
margin-right: 9px;
color: #a4aca7;
}
.stage-note {
position: absolute;
z-index: 4;
right: 18px;
bottom: 16px;
padding: 7px 9px;
border-radius: 8px;
color: rgba(23, 35, 31, 0.62);
background: rgba(255, 255, 255, 0.72);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.controls-hint {
position: absolute;
z-index: 4;
left: 18px;
bottom: 16px;
padding: 7px 9px;
border-radius: 8px;
color: rgba(23, 35, 31, 0.62);
background: rgba(255, 255, 255, 0.72);
font-size: 10px;
pointer-events: none;
}
.tooltip {
position: absolute;
z-index: 8;
width: 168px;
padding: 11px 12px;
border: 1px solid rgba(23, 35, 31, 0.14);
border-radius: 11px;
color: var(--ink);
background: rgba(255, 255, 255, 0.96);
box-shadow: 0 12px 32px rgba(23, 35, 31, 0.16);
font-size: 11px;
line-height: 1.45;
pointer-events: none;
opacity: 0;
transform: translateY(4px);
transition:
opacity 100ms ease,
transform 100ms ease;
}
.tooltip.visible {
opacity: 1;
transform: translateY(0);
}
.tooltip strong {
display: block;
margin-bottom: 2px;
font-size: 12px;
}
.tooltip span {
color: var(--muted);
}
.metrics {
padding: 14px;
background: var(--paper);
border-top: 1px solid var(--line);
}
.metric-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.metric-card {
position: relative;
overflow: hidden;
padding: 15px 17px 14px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--surface);
box-shadow: 0 8px 28px rgba(33, 43, 38, 0.045);
}
.metric-card::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 4px;
height: 100%;
background: var(--compact);
}
.metric-card.dispersed::before {
background: var(--dispersed);
}
.metric-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.metric-kicker {
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.metric-card h2 {
margin: 3px 0 0;
font-size: 17px;
letter-spacing: -0.025em;
}
.comparison {
padding: 6px 9px;
border-radius: 999px;
color: #b84e39;
background: #fdece7;
font-size: 10px;
font-weight: 850;
white-space: nowrap;
}
.dispersed .comparison {
color: #5146b2;
background: #eeecff;
}
.numbers {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 10px;
}
.number {
min-width: 0;
padding-right: 8px;
border-right: 1px solid #e7e9e5;
}
.number:last-child {
border-right: 0;
}
.number span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.number strong {
display: block;
font-size: clamp(16px, 1.7vw, 23px);
line-height: 1;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.density-track {
height: 4px;
margin-top: 14px;
overflow: hidden;
border-radius: 99px;
background: #edf0eb;
}
.density-track i {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--compact);
transition: width 500ms ease;
}
.dispersed .density-track i {
background: var(--dispersed);
}
.explanation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 10px 3px 0;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.legend {
display: flex;
align-items: center;
gap: 14px;
flex-wrap: wrap;
flex-shrink: 0;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
white-space: nowrap;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--compact);
}
.legend .two-three {
background: var(--compact);
}
.legend .four {
background: var(--amber);
}
.legend .seven {
background: var(--violet);
}
.legend .footprint {
border: 1px solid var(--green);
background: rgba(33, 139, 115, 0.18);
}
@media (max-width: 760px) {
body {
overflow: auto;
}
.app {
display: block;
height: auto;
min-height: 100dvh;
}
.topbar {
align-items: flex-start;
flex-direction: column;
gap: 16px;
padding: 18px;
}
.toolbar {
width: 100%;
justify-content: flex-start;
}
.equilibrium {
flex: 1;
}
.stage {
height: 760px;
}
.divider {
top: 50%;
bottom: auto;
left: 0;
width: 100%;
height: 1px;
}
.divider::after {
top: 50%;
left: 50%;
}
.view-label {
top: 15px;
left: 15px;
min-width: 185px;
padding: 10px 12px;
}
.view-label.dispersed {
top: calc(50% + 15px);
left: 15px;
}
.controls-hint {
display: none;
}
.stage-note {
right: 12px;
bottom: 12px;
}
.metric-grid {
grid-template-columns: 1fr;
}
.metrics {
padding: 12px;
}
.numbers {
grid-template-columns: repeat(2, 1fr);
row-gap: 14px;
}
.number:nth-child(2) {
border-right: 0;
}
.explanation {
align-items: flex-start;
flex-direction: column;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="kicker">Population density study / 01</div>
<h1>Same people. Different place.</h1>
</div>
<div class="toolbar">
<div class="equilibrium" aria-label="Equal population in both layouts">
<span><b id="totalHouseholds">48</b> households</span>
<span class="equals">=</span>
<span><b id="totalResidents">192</b> residents</span>
</div>
<button
class="control"
id="footprintToggle"
type="button"
aria-pressed="true"
>
Footprint on
</button>
<button class="control" id="resetView" type="button">
Reset view
</button>
</div>
</header>
<main class="stage" id="stage">
<canvas
id="scene"
aria-label="Interactive voxel comparison of compact and dispersed settlements"
></canvas>
<div class="divider" aria-hidden="true"></div>
<div class="view-label compact">
<div class="label-heading">Compact cluster</div>
<div class="label-meta">
<span>48 households</span>
<span id="compactSceneArea">0.00 ha footprint</span>
</div>
</div>
<div class="view-label dispersed">
<div class="label-heading">Dispersed settlement</div>
<div class="label-meta">
<span>48 households</span>
<span id="dispersedSceneArea">0.00 ha footprint</span>
</div>
</div>
<div class="controls-hint">Drag to orbit · Scroll to zoom · Hover a house</div>
<div class="stage-note">12 m grid / same-scale views</div>
<div class="tooltip" id="tooltip" role="status" aria-live="polite"></div>
</main>
<footer class="metrics">
<div class="metric-grid">
<article class="metric-card compact">
<div class="metric-top">
<div>
<div class="metric-kicker">Layout A / high density</div>
<h2>Compact cluster</h2>
</div>
<div class="comparison" id="compactComparison">More dense</div>
</div>
<div class="numbers">
<div class="number">
<span>Households</span>
<strong>48</strong>
</div>
<div class="number">
<span>Residents</span>
<strong>192</strong>
</div>
<div class="number">
<span>Occupied area</span>
<strong id="compactArea">0.00 ha</strong>
</div>
<div class="number">
<span>Density</span>
<strong id="compactDensity">0/ha</strong>
</div>
</div>
<div class="density-track"><i id="compactBar"></i></div>
</article>
<article class="metric-card dispersed">
<div class="metric-top">
<div>
<div class="metric-kicker">Layout B / low density</div>
<h2>Dispersed settlement</h2>
</div>
<div class="comparison" id="dispersedComparison">Larger footprint</div>
</div>
<div class="numbers">
<div class="number">
<span>Households</span>
<strong>48</strong>
</div>
<div class="number">
<span>Residents</span>
<strong>192</strong>
</div>
<div class="number">
<span>Occupied area</span>
<strong id="dispersedArea">0.00 ha</strong>
</div>
<div class="number">
<span>Density</span>
<strong id="dispersedDensity">0/ha</strong>
</div>
</div>
<div class="density-track"><i id="dispersedBar"></i></div>
</article>
</div>
<div class="explanation">
<div class="legend" aria-label="Scene legend">
<span><i class="two-three"></i>2–3 residents</span>
<span><i class="four"></i>4 residents</span>
<span><i class="seven"></i>5+ residents</span>
<span><i class="footprint"></i>Occupied footprint</span>
</div>
<div>
Occupied area is the settlement footprint: the convex hull of the
10 × 10 m household cells. Both layouts use the identical 48-household
roster and 192 residents.
</div>
</div>
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const HOUSEHOLD_COUNT = 48;
const CELL_SIZE = 10;
// Every household has exactly the same population in both settlements.
const householdPopulations = Array.from(
{ length: HOUSEHOLD_COUNT },
(_, index) => [2, 3, 4, 4, 4, 7][index % 6]
);
const totalResidents = householdPopulations.reduce(
(sum, population) => sum + population,
0
);
const stage = document.getElementById("stage");
const canvas = document.getElementById("scene");
const tooltip = document.getElementById("tooltip");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
renderer.autoClear = false;
const scenes = [new THREE.Scene(), new THREE.Scene()];
const cameras = [
new THREE.OrthographicCamera(-1, 1, 1, -1, 1, 1600),
new THREE.OrthographicCamera(-1, 1, 1, -1, 1, 1600)
];
scenes[0].background = new THREE.Color(0xe8eee8);
scenes[1].background = new THREE.Color(0xebece7);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const outlineGeometry = new THREE.EdgesGeometry(unitBox);
const materials = {
body: new THREE.MeshStandardMaterial({
color: 0xf7f0df,
roughness: 0.86,
metalness: 0.02
}),
roofTwoThree: new THREE.MeshStandardMaterial({
color: 0xe86f57,
roughness: 0.72
}),
roofFour: new THREE.MeshStandardMaterial({
color: 0xf0b74d,
roughness: 0.72
}),
roofFivePlus: new THREE.MeshStandardMaterial({
color: 0x786bd9,
roughness: 0.7
}),
marker: new THREE.MeshStandardMaterial({
color: 0xfff9e9,
roughness: 0.75
}),
door: new THREE.MeshStandardMaterial({
color: 0x40534d,
roughness: 0.85
}),
window: new THREE.MeshStandardMaterial({
color: 0x9ed8d1,
emissive: 0x4c9188,
emissiveIntensity: 0.22,
roughness: 0.35
}),
footprint: new THREE.MeshBasicMaterial({
color: 0x3aa181,
transparent: true,
opacity: 0.13,
side: THREE.DoubleSide,
depthWrite: false
}),
footprintLine: new THREE.LineBasicMaterial({
color: 0x218b73,
transparent: true,
opacity: 0.92
}),
lot: new THREE.MeshBasicMaterial({
color: 0x3aa181,
transparent: true,
opacity: 0.36,
depthWrite: false
}),
compactGround: new THREE.MeshStandardMaterial({
color: 0xdce8df,
roughness: 1
}),
dispersedGround: new THREE.MeshStandardMaterial({
color: 0xe3e4dc,
roughness: 1
})
};
const compactPositions = [];
const columns = 8;
const rows = 6;
const spacing = 12;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
compactPositions.push({
x: (column - (columns - 1) / 2) * spacing,
z: (row - (rows - 1) / 2) * spacing
});
}
}
// Deterministic golden-angle distribution: no random changes between loads.
const dispersedPositions = Array.from(
{ length: HOUSEHOLD_COUNT },
(_, index) => {
const fraction = (index + 0.5) / HOUSEHOLD_COUNT;
const radius = Math.sqrt(fraction) * 138;
const angle = index * 2.399963229728653 + 0.38;
const jitter = ((index % 5) - 2) * 0.65;
return {
x: Math.cos(angle) * radius + jitter,
z: Math.sin(angle) * radius - jitter
};
}
);
function cross2(o, a, b) {
return (
(a.x - o.x) * (b.y - o.y) -
(a.y - o.y) * (b.x - o.x)
);
}
function convexHull(points) {
const sorted = [...points].sort((a, b) =>
a.x === b.x ? a.y - b.y : a.x - b.x
);
const lower = [];
for (const point of sorted) {
while (
lower.length >= 2 &&
cross2(lower[lower.length - 2], lower[lower.length - 1], point) <= 0
) {
lower.pop();
}
lower.push(point);
}
const upper = [];
for (let index = sorted.length - 1; index >= 0; index--) {
const point = sorted[index];
while (
upper.length >= 2 &&
cross2(
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 index = 0; index < points.length; index++) {
const current = points[index];
const next = points[(index + 1) % points.length];
area += current.x * next.y - next.x * current.y;
}
return Math.abs(area) / 2;
}
function householdCellCorners(position) {
const half = CELL_SIZE / 2;
return [
{ x: position.x - half, y: position.z - half },
{ x: position.x + half, y: position.z - half },
{ x: position.x + half, y: position.z + half },
{ x: position.x - half, y: position.z + half }
];
}
function calculateFootprint(positions) {
const corners = positions.flatMap(householdCellCorners);
const hull = convexHull(corners);
return {
hull,
area: polygonArea(hull)
};
}
const compactFootprint = calculateFootprint(compactPositions);
const dispersedFootprint = calculateFootprint(dispersedPositions);
function roofMaterial(population) {
if (population <= 3) return materials.roofTwoThree;
if (population === 4) return materials.roofFour;
return materials.roofFivePlus;
}
function addHouse(scene, position, population, householdNumber, pickables) {
const group = new THREE.Group();
group.position.set(position.x, 0, position.z);
const height = 7 + population * 1.1;
const info = {
number: householdNumber,
population,
height,
x: position.x,
z: position.z
};
const body = new THREE.Mesh(unitBox, materials.body);
body.scale.set(9, height, 9);
body.position.y = height / 2;
body.castShadow = true;
body.receiveShadow = true;
body.userData.info = info;
group.add(body);
const roof = new THREE.Mesh(unitBox, roofMaterial(population));
roof.scale.set(11, 2.1, 11);
roof.position.y = height + 1.05;
roof.castShadow = true;
roof.receiveShadow = true;
roof.userData.info = info;
group.add(roof);
// One cream cap identifies one household marker.
const marker = new THREE.Mesh(unitBox, materials.marker);
marker.scale.set(2.8, 0.8, 2.8);
marker.position.y = height + 2.5;
marker.castShadow = true;
group.add(marker);
const door = new THREE.Mesh(unitBox, materials.door);
door.scale.set(2.2, 4.4, 0.35);
door.position.set(0, 2.2, 4.55);
group.add(door);
for (const x of [-2.8, 2.8]) {
const windowMesh = new THREE.Mesh(unitBox, materials.window);
windowMesh.scale.set(2.25, 2.05, 0.3);
windowMesh.position.set(x, 4.4, 4.55);
group.add(windowMesh);
}
scene.add(group);
pickables.push(body, roof);
}
function addFootprint(scene, positions, footprint) {
const group = new THREE.Group();
const shape = new THREE.Shape();
footprint.hull.forEach((point, index) => {
const shapePoint = new THREE.Vector2(point.x, -point.y);
if (index === 0) {
shape.moveTo(shapePoint.x, shapePoint.y);
} else {
shape.lineTo(shapePoint.x, shapePoint.y);
}
});
shape.closePath();
const footprintGeometry = new THREE.ShapeGeometry(shape);
footprintGeometry.rotateX(-Math.PI / 2);
const footprintMesh = new THREE.Mesh(
footprintGeometry,
materials.footprint
);
footprintMesh.position.y = 0.035;
footprintMesh.renderOrder = 1;
group.add(footprintMesh);
const linePoints = footprint.hull.map(
(point) => new THREE.Vector3(point.x, 0.18, point.y)
);
const footprintLine = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(linePoints),
materials.footprintLine
);
footprintLine.renderOrder = 2;
group.add(footprintLine);
for (const position of positions) {
const lot = new THREE.Mesh(unitBox, materials.lot);
lot.scale.set(CELL_SIZE, 0.12, CELL_SIZE);
lot.position.set(position.x, 0.12, position.z);
lot.renderOrder = 1;
group.add(lot);
}
scene.add(group);
return group;
}
function addLighting(scene) {
const hemisphere = new THREE.HemisphereLight(
0xffffff,
0xa7b7ab,
2.25
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d7, 3.1);
sun.position.set(-120, 220, 110);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -180;
sun.shadow.camera.right = 180;
sun.shadow.camera.top = 180;
sun.shadow.camera.bottom = -180;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 650;
sun.shadow.bias = -0.0006;
sun.shadow.normalBias = 0.04;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight(0xc9e2ff, 0.55);
fill.position.set(130, 100, -170);
scene.add(fill);
}
function buildScene(
scene,
positions,
footprint,
groundMaterial,
pickables
) {
addLighting(scene);
const ground = new THREE.Mesh(unitBox, groundMaterial);
ground.scale.set(300, 4, 300);
ground.position.y = -2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
300,
25,
0x9eb3a6,
0xb7c3ba
);
grid.position.y = 0.07;
grid.material.transparent = true;
grid.material.opacity = 0.35;
scene.add(grid);
positions.forEach((position, index) => {
addHouse(
scene,
position,
householdPopulations[index],
index + 1,
pickables
);
});
return addFootprint(scene, positions, footprint);
}
const pickables = [[], []];
const footprintGroups = [
buildScene(
scenes[0],
compactPositions,
compactFootprint,
materials.compactGround,
pickables[0]
),
buildScene(
scenes[1],
dispersedPositions,
dispersedFootprint,
materials.dispersedGround,
pickables[1]
)
];
const selections = scenes.map((scene) => {
const selection = new THREE.LineSegments(
outlineGeometry,
new THREE.LineBasicMaterial({
color: 0xffd35c,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
selection.visible = false;
selection.renderOrder = 20;
scene.add(selection);
return selection;
});
const compactHectares = compactFootprint.area / 10000;
const dispersedHectares = dispersedFootprint.area / 10000;
const compactDensity = totalResidents / compactHectares;
const dispersedDensity = totalResidents / dispersedHectares;
const footprintRatio = dispersedFootprint.area / compactFootprint.area;
document.getElementById("totalHouseholds").textContent =
HOUSEHOLD_COUNT;
document.getElementById("totalResidents").textContent =
totalResidents;
document.getElementById("compactArea").textContent =
`${compactHectares.toFixed(2)} ha`;
document.getElementById("dispersedArea").textContent =
`${dispersedHectares.toFixed(2)} ha`;
document.getElementById("compactDensity").textContent =
`${Math.round(compactDensity)}/ha`;
document.getElementById("dispersedDensity").textContent =
`${Math.round(dispersedDensity)}/ha`;
document.getElementById("compactSceneArea").textContent =
`${compactHectares.toFixed(2)} ha footprint`;
document.getElementById("dispersedSceneArea").textContent =
`${dispersedHectares.toFixed(2)} ha footprint`;
document.getElementById("compactComparison").textContent =
`${footprintRatio.toFixed(1)}× denser`;
document.getElementById("dispersedComparison").textContent =
`${footprintRatio.toFixed(1)}× larger footprint`;
document.getElementById("compactBar").style.width = "100%";
document.getElementById("dispersedBar").style.width =
`${100 / footprintRatio}%`;
const defaultView = {
yaw: -0.72,
pitch: 0.82,
zoom: 1
};
const view = {
yaw: defaultView.yaw,
pitch: defaultView.pitch,
zoom: defaultView.zoom,
targetYaw: defaultView.yaw,
targetPitch: defaultView.pitch,
targetZoom: defaultView.zoom
};
let width = 1;
let height = 1;
let stacked = window.innerWidth <= 760;
let dragging = false;
let activePointer = null;
let lastPointerX = 0;
let lastPointerY = 0;
let hoveredScene = -1;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function resize() {
const rect = stage.getBoundingClientRect();
width = Math.max(1, Math.round(rect.width));
height = Math.max(1, Math.round(rect.height));
stacked = window.innerWidth <= 760;
renderer.setSize(width, height, false);
}
function updateCameras() {
const viewportWidth = stacked ? width : width / 2;
const viewportHeight = stacked ? height / 2 : height;
const aspect = viewportWidth / viewportHeight;
const fitSize = 410;
cameras.forEach((camera) => {
const verticalSize =
Math.max(fitSize, fitSize / aspect) / view.zoom;
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
const radius = 700;
const horizontalRadius =
radius * Math.cos(view.pitch);
camera.position.set(
Math.sin(view.yaw) * horizontalRadius,
Math.sin(view.pitch) * radius,
Math.cos(view.yaw) * horizontalRadius
);
camera.lookAt(0, 3, 0);
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
});
}
function renderViewport(
sceneIndex,
x,
y,
viewportWidth,
viewportHeight
) {
renderer.setViewport(x, y, viewportWidth, viewportHeight);
renderer.setScissor(x, y, viewportWidth, viewportHeight);
renderer.clear(true, true, true);
renderer.render(scenes[sceneIndex], cameras[sceneIndex]);
}
function render() {
renderer.setScissorTest(true);
if (stacked) {
const topHeight = Math.floor(height / 2);
const bottomHeight = height - topHeight;
// WebGL viewport coordinates begin at the bottom.
renderViewport(
0,
0,
bottomHeight,
width,
topHeight
);
renderViewport(
1,
0,
0,
width,
bottomHeight
);
} else {
const leftWidth = Math.floor(width / 2);
const rightWidth = width - leftWidth;
renderViewport(0, 0, 0, leftWidth, height);
renderViewport(
1,
leftWidth,
0,
rightWidth,
height
);
}
}
function pointerLocation(event) {
const rect = stage.getBoundingClientRect();
const localX = event.clientX - rect.left;
const localY = event.clientY - rect.top;
if (stacked) {
if (localY < height / 2) {
return {
sceneIndex: 0,
localX,
localY,
viewportWidth: width,
viewportHeight: height / 2
};
}
return {
sceneIndex: 1,
localX,
localY: localY - height / 2,
viewportWidth: width,
viewportHeight: height / 2
};
}
if (localX < width / 2) {
return {
sceneIndex: 0,
localX,
localY,
viewportWidth: width / 2,
viewportHeight: height
};
}
return {
sceneIndex: 1,
localX: localX - width / 2,
localY,
viewportWidth: width / 2,
viewportHeight: height
};
}
function clearHover() {
tooltip.classList.remove("visible");
selections.forEach((selection) => {
selection.visible = false;
});
hoveredScene = -1;
}
function updateHover(event) {
const location = pointerLocation(event);
pointer.x =
(location.localX / location.viewportWidth) * 2 - 1;
pointer.y =
-(location.localY / location.viewportHeight) * 2 + 1;
raycaster.setFromCamera(pointer, cameras[location.sceneIndex]);
const intersections = raycaster.intersectObjects(
pickables[location.sceneIndex],
false
);
selections.forEach((selection) => {
selection.visible = false;
});
if (!intersections.length) {
tooltip.classList.remove("visible");
hoveredScene = -1;
canvas.style.cursor = dragging ? "grabbing" : "grab";
return;
}
const info = intersections[0].object.userData.info;
const selection = selections[location.sceneIndex];
selection.visible = true;
selection.position.set(info.x, (info.height + 4.4) / 2, info.z);
selection.scale.set(12.2, info.height + 4.4, 12.2);
tooltip.innerHTML = `
<strong>Household ${String(info.number).padStart(2, "0")}</strong>
<span>${info.population} residents · identical in both layouts</span>
`;
const rect = stage.getBoundingClientRect();
const tooltipX = Math.min(
Math.max(8, event.clientX - rect.left + 14),
width - 184
);
const tooltipY = Math.min(
Math.max(8, event.clientY - rect.top + 14),
height - 76
);
tooltip.style.left = `${tooltipX}px`;
tooltip.style.top = `${tooltipY}px`;
tooltip.classList.add("visible");
hoveredScene = location.sceneIndex;
canvas.style.cursor = "pointer";
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
activePointer = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
tooltip.classList.remove("visible");
});
canvas.addEventListener("pointermove", (event) => {
if (dragging && event.pointerId === activePointer) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
view.targetYaw -= deltaX * 0.006;
view.targetPitch = THREE.MathUtils.clamp(
view.targetPitch + deltaY * 0.005,
0.38,
1.3
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else {
updateHover(event);
}
});
function endDrag(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
activePointer = null;
canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("pointerleave", () => {
if (!dragging) clearHover();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.targetZoom = THREE.MathUtils.clamp(
view.targetZoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.4
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
view.targetYaw = defaultView.yaw;
view.targetPitch = defaultView.pitch;
view.targetZoom = defaultView.zoom;
});
document.getElementById("resetView").addEventListener("click", () => {
view.targetYaw = defaultView.yaw;
view.targetPitch = defaultView.pitch;
view.targetZoom = defaultView.zoom;
});
document.getElementById("footprintToggle").addEventListener("click", (event) => {
const isVisible = footprintGroups[0].visible;
footprintGroups.forEach((group) => {
group.visible = !isVisible;
});
event.currentTarget.setAttribute("aria-pressed", String(!isVisible));
event.currentTarget.textContent = isVisible
? "Footprint off"
: "Footprint on";
});
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
view.targetYaw -= 0.12;
} else if (event.key === "ArrowRight") {
view.targetYaw += 0.12;
} else if (event.key === "ArrowUp") {
view.targetPitch = THREE.MathUtils.clamp(
view.targetPitch - 0.08,
0.38,
1.3
);
} else if (event.key === "ArrowDown") {
view.targetPitch = THREE.MathUtils.clamp(
view.targetPitch + 0.08,
0.38,
1.3
);
} else if (event.key === "+" || event.key === "=") {
view.targetZoom = Math.min(2.4, view.targetZoom * 1.1);
} else if (event.key === "-") {
view.targetZoom = Math.max(0.72, view.targetZoom / 1.1);
} else if (event.key.toLowerCase() === "r") {
view.targetYaw = defaultView.yaw;
view.targetPitch = defaultView.pitch;
view.targetZoom = defaultView.zoom;
}
});
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
view.yaw += (view.targetYaw - view.yaw) * 0.12;
view.pitch += (view.targetPitch - view.pitch) * 0.12;
view.zoom += (view.targetZoom - view.zoom) * 0.12;
updateCameras();
render();
}
resize();
updateCameras();
animate();
</script>
</body>
</html>
```