Prompt specification
Build or customize the Three.js 3D scene "Population grid and reachable sample" 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: Population grid and reachable sample
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/population-grid-and-reachable-sample-x1829
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: population grid and reachable sample. Build a grid of differently marked synthetic households with a sample tool limited to one corner; show the resulting selection pattern beside the full population. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (population-grid-and-reachable-sample-x1829):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reachable Sample — Low-Poly Population Grid</title>
<style>
:root {
--ink: #edf6f2;
--muted: #91a7a5;
--mint: #7cf7c8;
--amber: #ffc857;
--panel: rgba(12, 24, 27, 0.84);
--line: rgba(172, 216, 207, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 38%, #1c3031 0, #101a1d 45%, #091113 100%);
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%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.heading {
position: absolute;
top: 27px;
left: 31px;
max-width: min(680px, calc(100vw - 420px));
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.4);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--mint);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 2px;
background: var(--mint);
box-shadow: 0 0 12px rgba(124, 247, 200, 0.5);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 760;
}
h1 span {
color: #91aaa7;
font-weight: 470;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.01em;
}
.constraint {
position: absolute;
top: 29px;
right: 30px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(12, 24, 27, 0.68);
backdrop-filter: blur(12px);
color: #c9d8d5;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.16);
}
.constraint i {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 13px rgba(124, 247, 200, 0.9);
}
.legend {
position: absolute;
left: 30px;
bottom: 27px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(10, 21, 24, 0.72);
backdrop-filter: blur(12px);
box-shadow: 0 14px 35px rgba(0, 0, 0, 0.17);
}
.legend-title {
margin-bottom: 9px;
color: #78908e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-items {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
max-width: 470px;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
color: #c4d2cf;
font-size: 10px;
font-weight: 650;
white-space: nowrap;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 2px;
transform: rotate(45deg);
}
.controls {
position: absolute;
right: 29px;
bottom: 26px;
width: 306px;
padding: 16px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
backdrop-filter: blur(15px);
pointer-events: auto;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.27);
}
.control-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
margin-bottom: 13px;
}
.control-kicker {
color: #78908e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.coverage {
margin-top: 3px;
color: var(--ink);
font-size: 17px;
font-weight: 750;
letter-spacing: -0.03em;
}
.coverage small {
color: #78908e;
font-size: 11px;
font-weight: 550;
}
.radius-value {
padding: 5px 8px;
border-radius: 7px;
background: rgba(124, 247, 200, 0.1);
color: var(--mint);
font-size: 10px;
font-weight: 800;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 4px 0 10px;
accent-color: var(--mint);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: -6px;
color: #617775;
font-size: 9px;
}
.control-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.toggle {
display: flex;
align-items: center;
gap: 7px;
color: #91a7a5;
font-size: 10px;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 13px;
height: 13px;
margin: 0;
accent-color: var(--mint);
}
button {
border: 1px solid rgba(124, 247, 200, 0.22);
border-radius: 8px;
padding: 7px 10px;
background: rgba(124, 247, 200, 0.08);
color: var(--mint);
font: inherit;
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(124, 247, 200, 0.15);
}
.hint {
position: absolute;
left: 50%;
bottom: 31px;
transform: translateX(-50%);
color: rgba(188, 207, 203, 0.58);
font-size: 10px;
letter-spacing: 0.04em;
white-space: nowrap;
}
#tooltip {
position: fixed;
z-index: 20;
display: none;
width: 218px;
padding: 13px;
border: 1px solid rgba(153, 213, 202, 0.22);
border-radius: 11px;
background: rgba(8, 18, 21, 0.94);
backdrop-filter: blur(14px);
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.38);
pointer-events: none;
}
.tt-head {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 4px;
}
.tt-dot {
width: 9px;
height: 9px;
border-radius: 3px;
transform: rotate(45deg);
}
#tt-id {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.03em;
}
#tt-type {
color: #8ea4a1;
font-size: 10px;
}
.tt-grid {
display: grid;
grid-template-columns: 1fr auto;
gap: 5px 12px;
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid rgba(153, 213, 202, 0.12);
color: #78908e;
font-size: 9px;
}
.tt-grid b {
color: #d8e5e1;
font-weight: 650;
}
#tt-status {
margin-top: 10px;
color: var(--mint);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
@media (max-width: 900px) {
.heading {
left: 20px;
top: 20px;
max-width: calc(100vw - 40px);
}
.constraint {
top: auto;
right: 18px;
bottom: 182px;
}
.legend {
left: 18px;
bottom: 18px;
max-width: calc(100vw - 36px);
}
.legend-items {
gap: 7px 11px;
}
.controls {
right: 18px;
bottom: 18px;
width: 286px;
}
.hint {
display: none;
}
}
@media (max-width: 620px) {
.heading {
top: 16px;
}
h1 {
font-size: 25px;
}
.subtitle {
font-size: 11px;
}
.constraint {
right: 14px;
bottom: 202px;
padding: 8px 10px;
font-size: 9px;
}
.legend {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
}
}
</style>
</head>
<body>
<div class="hud">
<header class="heading">
<div class="eyebrow">Spatial sampling laboratory</div>
<h1>Population grid <span>→ reachable sample</span></h1>
<p class="subtitle">96 synthetic households · fixed southwest access · Manhattan-grid reachability</p>
</header>
<div class="constraint"><i></i> Access fixed · southwest only</div>
<section class="legend" aria-label="Household type legend">
<div class="legend-title">Household marker</div>
<div class="legend-items" id="legend-items"></div>
</section>
<section class="controls">
<div class="control-top">
<div>
<div class="control-kicker">Sample coverage</div>
<div class="coverage"><span id="coverage-count">43</span> <small>/ 96 households</small></div>
</div>
<div class="radius-value" id="radius-value">8 grid steps</div>
</div>
<input id="radius" type="range" min="0" max="18" value="8" aria-label="Sampling reach radius">
<div class="range-labels"><span>Tool only</span><span>Full frame</span></div>
<div class="control-footer">
<label class="toggle">
<input id="show-routes" type="checkbox" checked>
Show reach paths
</label>
<button id="reset-view" type="button">Reset view</button>
</div>
</section>
<div class="hint">Drag to orbit · scroll to zoom · hover a household</div>
</div>
<div id="tooltip" role="status">
<div class="tt-head"><span class="tt-dot" id="tt-dot"></span><span id="tt-id"></span></div>
<div id="tt-type"></div>
<div class="tt-grid">
<span>Household size</span><b id="tt-size"></b>
<span>Income band</span><b id="tt-income"></b>
<span>Tenure</span><b id="tt-tenure"></b>
<span>Grid position</span><b id="tt-position"></b>
</div>
<div id="tt-status"></div>
</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 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;
renderer.domElement.setAttribute("aria-label", "Interactive low-poly population sampling scene");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101a1d);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.1, 0);
const cameraState = {
azimuth: 0.22,
polar: 0.94,
zoom: 1.06
};
const hemisphere = new THREE.HemisphereLight(0xc8eeea, 0x182326, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd1, 3.2);
keyLight.position.set(-7, 14, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x79d8cf, 1.4);
rimLight.position.set(10, 7, -7);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({ color: 0x0d1719, roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.34;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Configuration and synthetic households
// -------------------------------------------------------------------------
const COLS = 12;
const ROWS = 8;
const CELL = 0.68;
const GRID_WIDTH = COLS * CELL;
const GRID_DEPTH = ROWS * CELL;
const PANEL_WIDTH = 9.05;
const PANEL_DEPTH = 6.3;
const PANEL_OFFSET = 5.0;
const TYPE_INFO = [
{ name: "Family", full: "Family household", color: 0xff7b6b, css: "#ff7b6b", sizes: [3, 6] },
{ name: "Apartment", full: "Apartment household", color: 0xffc857, css: "#ffc857", sizes: [2, 12] },
{ name: "Shared", full: "Shared household", color: 0x63d6be, css: "#63d6be", sizes: [2, 8] },
{ name: "Studio", full: "Single-person studio", color: 0x72a9f7, css: "#72a9f7", sizes: [1, 2] },
{ name: "Row", full: "Row household", color: 0xb39aee, css: "#b39aee", sizes: [2, 4] }
];
function mulberry32(seed) {
return function() {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const random = mulberry32(24051987);
const households = [];
const distances = [];
const incomeBands = ["Lower", "Middle", "Upper"];
const tenureTypes = ["Owner", "Renter", "Shared"];
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const index = row * COLS + col;
const type = (row * 3 + col * 5 + Math.floor(random() * 3)) % TYPE_INFO.length;
const info = TYPE_INFO[type];
const size = info.sizes[0] + Math.floor(random() * (info.sizes[1] - info.sizes[0] + 1));
households.push({
index,
id: `HH-${String(index + 1).padStart(3, "0")}`,
type,
typeName: info.full,
color: info.css,
size,
income: incomeBands[Math.floor(random() * incomeBands.length)],
tenure: tenureTypes[Math.floor(random() * tenureTypes.length)],
tone: Math.floor(random() * 3),
row,
col
});
// Southwest origin: column distance + north-to-south distance.
distances.push(col + (ROWS - 1 - row));
}
}
const legendRoot = document.getElementById("legend-items");
for (const type of TYPE_INFO) {
const item = document.createElement("div");
item.className = "legend-item";
item.innerHTML = `<span class="swatch" style="background:${type.css}"></span><span>${type.name}</span>`;
legendRoot.appendChild(item);
}
// -------------------------------------------------------------------------
// Shared materials and low-poly assets
// -------------------------------------------------------------------------
const bodyMaterials = [
new THREE.MeshStandardMaterial({ color: 0xeee1c8, roughness: 0.9, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0xd8dfd5, roughness: 0.9, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0xe7cfb9, roughness: 0.9, flatShading: true })
];
const typeMaterials = TYPE_INFO.map(info =>
new THREE.MeshStandardMaterial({
color: info.color,
roughness: 0.72,
flatShading: true
})
);
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x29414a,
roughness: 0.45,
metalness: 0.08
});
const doorMaterial = new THREE.MeshStandardMaterial({
color: 0x4e4942,
roughness: 0.9
});
const flagPoleMaterial = new THREE.MeshStandardMaterial({
color: 0xe4e9dd,
roughness: 0.8
});
const platformBaseMaterial = new THREE.MeshStandardMaterial({
color: 0x132326,
roughness: 0.95,
flatShading: true
});
const platformTopMaterial = new THREE.MeshStandardMaterial({
color: 0x213438,
roughness: 0.92,
flatShading: true
});
const platformEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x69817e,
transparent: true,
opacity: 0.22
});
const leftTileMaterials = [
new THREE.MeshStandardMaterial({ color: 0x2b4143, roughness: 0.95 }),
new THREE.MeshStandardMaterial({ color: 0x273b3e, roughness: 0.95 })
];
const selectedTileMaterial = new THREE.MeshStandardMaterial({
color: 0x2c5c50,
roughness: 0.9
});
const excludedTileMaterial = new THREE.MeshStandardMaterial({
color: 0x1b292c,
roughness: 0.98
});
const footprintMaterial = new THREE.MeshStandardMaterial({
color: 0x101b1e,
roughness: 1
});
const footprintCrossMaterial = new THREE.MeshBasicMaterial({
color: 0x526361,
transparent: true,
opacity: 0.35
});
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x7cf7c8,
transparent: true,
opacity: 0.46,
side: THREE.DoubleSide,
depthWrite: false
});
const tileGeometry = new THREE.PlaneGeometry(CELL * 0.82, CELL * 0.82);
const haloGeometry = new THREE.RingGeometry(0.28, 0.325, 16);
const footprintGeometry = new THREE.BoxGeometry(0.37, 0.018, 0.31);
const footprintCrossGeometry = new THREE.BoxGeometry(0.28, 0.018, 0.025);
const doorGeometry = new THREE.BoxGeometry(0.065, 0.115, 0.018);
const windowGeometry = new THREE.BoxGeometry(0.095, 0.075, 0.018);
const flagPoleGeometry = new THREE.BoxGeometry(0.018, 0.18, 0.018);
const flagGeometry = new THREE.BoxGeometry(0.095, 0.052, 0.016);
function createGableGeometry(width, depth, height) {
const w = width / 2;
const d = depth / 2;
const positions = new Float32Array([
-w, 0, -d, w, 0, -d, 0, height, -d,
-w, 0, d, w, 0, d, 0, height, d
]);
const indices = [
0, 2, 1,
3, 4, 5,
0, 3, 5, 0, 5, 2,
1, 2, 5, 1, 5, 4,
0, 1, 4, 0, 4, 3
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const houseAssets = [
{
body: new THREE.BoxGeometry(0.45, 0.28, 0.37),
roof: new THREE.ConeGeometry(0.37, 0.24, 4),
roofRotation: Math.PI / 4
},
{
body: new THREE.BoxGeometry(0.40, 0.49, 0.35),
roof: new THREE.BoxGeometry(0.46, 0.065, 0.41),
roofRotation: 0
},
{
body: new THREE.BoxGeometry(0.43, 0.27, 0.38),
roof: new THREE.ConeGeometry(0.34, 0.22, 4),
roofRotation: Math.PI / 4
},
{
body: new THREE.BoxGeometry(0.29, 0.22, 0.28),
roof: new THREE.ConeGeometry(0.26, 0.19, 4),
roofRotation: Math.PI / 4
},
{
body: new THREE.BoxGeometry(0.47, 0.28, 0.37),
roof: createGableGeometry(0.53, 0.43, 0.23),
roofRotation: 0
}
];
function createHouse(household) {
const group = new THREE.Group();
const asset = houseAssets[household.type];
const material = typeMaterials[household.type];
const bodyMaterial = bodyMaterials[household.tone];
const bodyHeight = asset.body.parameters.height;
const bodyDepth = asset.body.parameters.depth;
const bodyWidth = asset.body.parameters.width;
const body = new THREE.Mesh(asset.body, bodyMaterial);
body.position.y = 0.02 + bodyHeight / 2;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
if (household.type === 2) {
const annex = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.21, 0.32),
bodyMaterial
);
annex.position.set(0.27, 0.125, 0.015);
annex.castShadow = true;
group.add(annex);
}
const roof = new THREE.Mesh(asset.roof, material);
roof.rotation.y = asset.roofRotation;
if (household.type === 4) {
roof.position.y = 0.02 + bodyHeight;
} else {
roof.position.y = 0.02 + bodyHeight + asset.roof.parameters.height / 2;
}
roof.castShadow = true;
group.add(roof);
const door = new THREE.Mesh(doorGeometry, doorMaterial);
door.position.set(bodyWidth * 0.19, 0.078, bodyDepth / 2 + 0.012);
group.add(door);
const windowMesh = new THREE.Mesh(windowGeometry, windowMaterial);
windowMesh.position.set(-bodyWidth * 0.12, bodyHeight * 0.58, bodyDepth / 2 + 0.012);
group.add(windowMesh);
if (household.type === 1) {
const balcony = new THREE.Mesh(
new THREE.BoxGeometry(0.29, 0.045, 0.075),
material
);
balcony.position.set(0, bodyHeight * 0.48, bodyDepth / 2 + 0.035);
group.add(balcony);
}
if (household.type === 3) {
const chimney = new THREE.Mesh(
new THREE.BoxGeometry(0.055, 0.12, 0.055),
doorMaterial
);
chimney.position.set(0.065, 0.40, 0.025);
chimney.castShadow = true;
group.add(chimney);
}
// Type-colored identity marker at the front of every home.
const pole = new THREE.Mesh(flagPoleGeometry, flagPoleMaterial);
const flagX = -bodyWidth * 0.30;
pole.position.set(flagX, 0.13, bodyDepth / 2 + 0.035);
group.add(pole);
const flag = new THREE.Mesh(flagGeometry, material);
flag.position.set(flagX + 0.048, 0.195, bodyDepth / 2 + 0.035);
group.add(flag);
return group;
}
// -------------------------------------------------------------------------
// Canvas-sprite labels
// -------------------------------------------------------------------------
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function createLabel(title, subtitle, accent = "#ffc857", width = 3.9) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 180;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 180 / 768, 1);
sprite.renderOrder = 30;
function update(nextTitle, nextSubtitle) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(ctx, 8, 12, 752, 150, 24);
ctx.fillStyle = "rgba(9, 20, 23, 0.88)";
ctx.fill();
ctx.fillStyle = accent;
roundedRectPath(ctx, 18, 24, 7, 126, 4);
ctx.fill();
ctx.textBaseline = "middle";
ctx.fillStyle = "#edf6f2";
ctx.font = "800 42px system-ui, sans-serif";
ctx.fillText(nextTitle, 48, 66);
ctx.fillStyle = "#91a7a5";
ctx.font = "650 24px system-ui, sans-serif";
ctx.fillText(nextSubtitle, 49, 122);
texture.needsUpdate = true;
}
update(title, subtitle);
return { sprite, update };
}
// -------------------------------------------------------------------------
// Panels and grid
// -------------------------------------------------------------------------
const interactiveRoots = [];
const fullVisuals = [];
const sampleVisuals = [];
function gridX(col) {
return (col - (COLS - 1) / 2) * CELL;
}
function gridZ(row) {
return (row - (ROWS - 1) / 2) * CELL;
}
function createPanel(isSample) {
const group = new THREE.Group();
group.position.x = isSample ? PANEL_OFFSET : -PANEL_OFFSET;
const base = new THREE.Mesh(
new THREE.BoxGeometry(PANEL_WIDTH, 0.24, PANEL_DEPTH),
platformBaseMaterial
);
base.position.y = -0.16;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const top = new THREE.Mesh(
new THREE.BoxGeometry(PANEL_WIDTH - 0.13, 0.08, PANEL_DEPTH - 0.13),
platformTopMaterial
);
top.position.y = 0;
top.receiveShadow = true;
group.add(top);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(top.geometry),
platformEdgeMaterial
);
edges.position.copy(top.position);
group.add(edges);
const frontAccent = new THREE.Mesh(
new THREE.BoxGeometry(PANEL_WIDTH - 0.45, 0.035, 0.055),
new THREE.MeshBasicMaterial({ color: isSample ? 0x7cf7c8 : 0xffc857 })
);
frontAccent.position.set(0, 0.065, PANEL_DEPTH / 2 - 0.015);
group.add(frontAccent);
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const index = row * COLS + col;
const household = households[index];
const tile = new THREE.Mesh(
tileGeometry,
isSample ? excludedTileMaterial : leftTileMaterials[(row + col) % 2]
);
tile.rotation.x = -Math.PI / 2;
tile.position.set(gridX(col), 0.044, gridZ(row));
tile.receiveShadow = true;
group.add(tile);
const root = new THREE.Group();
root.position.set(gridX(col), 0.047, gridZ(row));
root.userData.household = household;
root.userData.panel = isSample ? "sample" : "full";
root.userData.selected = false;
const house = createHouse(household);
root.add(house);
let halo = null;
let footprint = null;
if (isSample) {
halo = new THREE.Mesh(haloGeometry, haloMaterial);
halo.rotation.x = -Math.PI / 2;
halo.position.y = 0.012;
root.add(halo);
footprint = new THREE.Group();
const pad = new THREE.Mesh(footprintGeometry, footprintMaterial);
pad.position.y = 0.012;
footprint.add(pad);
const crossA = new THREE.Mesh(footprintCrossGeometry, footprintCrossMaterial);
crossA.position.y = 0.025;
crossA.rotation.y = Math.PI / 4;
footprint.add(crossA);
const crossB = new THREE.Mesh(footprintCrossGeometry, footprintCrossMaterial);
crossB.position.y = 0.026;
crossB.rotation.y = -Math.PI / 4;
footprint.add(crossB);
root.add(footprint);
sampleVisuals.push({ root, house, halo, footprint, tile, household, distance: distances[index] });
} else {
fullVisuals.push({ root, house, household });
}
group.add(root);
interactiveRoots.push(root);
}
}
const label = createLabel(
isSample ? "REACHABLE SAMPLE" : "FULL POPULATION",
isSample ? "0 / 96 HOUSEHOLDS REACHED" : "96 SYNTHETIC HOUSEHOLDS",
isSample ? "#7cf7c8" : "#ffc857",
isSample ? 3.75 : 3.55
);
label.sprite.position.set(0, 0.92, -PANEL_DEPTH / 2 + 0.10);
group.add(label.sprite);
scene.add(group);
return { group, label };
}
const fullPanel = createPanel(false);
const samplePanel = createPanel(true);
// -------------------------------------------------------------------------
// Corner-access hardware
// -------------------------------------------------------------------------
const blockedMaterial = new THREE.MeshStandardMaterial({
color: 0x4b5958,
roughness: 0.9,
transparent: true,
opacity: 0.62
});
function createBlockedCorner(x, z) {
const group = new THREE.Group();
group.position.set(x, 0.07, z);
const disc = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.21, 0.045, 10),
blockedMaterial
);
group.add(disc);
const barGeometry = new THREE.BoxGeometry(0.25, 0.025, 0.035);
const barA = new THREE.Mesh(barGeometry, blockedMaterial);
barA.rotation.y = Math.PI / 4;
barA.position.y = 0.03;
group.add(barA);
const barB = new THREE.Mesh(barGeometry, blockedMaterial);
barB.rotation.y = -Math.PI / 4;
barB.position.y = 0.031;
group.add(barB);
return group;
}
const cornerX = PANEL_WIDTH / 2 - 0.28;
const cornerZ = PANEL_DEPTH / 2 - 0.28;
fullPanel.group.add(createBlockedCorner(-cornerX, -cornerZ));
fullPanel.group.add(createBlockedCorner(cornerX, -cornerZ));
fullPanel.group.add(createBlockedCorner(cornerX, cornerZ));
const mintMaterial = new THREE.MeshStandardMaterial({
color: 0x7cf7c8,
emissive: 0x1c9a6f,
emissiveIntensity: 1.3,
roughness: 0.45,
flatShading: true
});
const toolDarkMaterial = new THREE.MeshStandardMaterial({
color: 0x24363a,
roughness: 0.78,
flatShading: true
});
const beamMaterial = new THREE.MeshBasicMaterial({
color: 0x7cf7c8,
transparent: true,
opacity: 0.09,
side: THREE.DoubleSide,
depthWrite: false
});
function createSampleTool() {
const group = new THREE.Group();
group.position.set(-cornerX, 0.07, cornerZ);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.29, 0.34, 0.13, 12),
toolDarkMaterial
);
base.castShadow = true;
group.add(base);
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.11, 0.57, 0.11),
toolDarkMaterial
);
post.position.y = 0.33;
post.castShadow = true;
group.add(post);
const arm = new THREE.Mesh(
new THREE.BoxGeometry(0.59, 0.085, 0.11),
toolDarkMaterial
);
arm.position.set(0.25, 0.60, -0.12);
arm.rotation.y = -0.43;
arm.castShadow = true;
group.add(arm);
const head = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.12, 0.19, 7),
toolDarkMaterial
);
head.position.set(0.51, 0.60, -0.25);
head.rotation.z = -Math.PI / 2;
head.rotation.y = -0.43;
head.castShadow = true;
group.add(head);
const lens = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.085, 0),
mintMaterial
);
lens.position.set(0.59, 0.60, -0.28);
group.add(lens);
const beam = new THREE.Mesh(
new THREE.ConeGeometry(0.34, 0.68, 16, 1, true),
beamMaterial
);
beam.position.set(0.59, 0.27, -0.28);
group.add(beam);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.38, 0.027, 6, 28),
mintMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.09;
group.add(ring);
const badge = createLabel("SAMPLE TOOL", "SW ACCESS ONLY", "#7cf7c8", 1.55);
badge.sprite.position.set(-0.02, 1.15, -0.02);
group.add(badge.sprite);
return { group, ring, beam };
}
const sampleTool = createSampleTool();
fullPanel.group.add(sampleTool.group);
function createSourceBeacon() {
const group = new THREE.Group();
group.position.set(-cornerX, 0.07, cornerZ);
const disc = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.29, 0.08, 10),
toolDarkMaterial
);
group.add(disc);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.35, 0.028, 6, 24),
mintMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.09;
group.add(ring);
const pin = new THREE.Mesh(
new THREE.OctahedronGeometry(0.09, 0),
mintMaterial
);
pin.position.y = 0.30;
group.add(pin);
const badge = createLabel("ORIGIN", "SW", "#7cf7c8", 0.82);
badge.sprite.position.set(0, 0.75, 0);
group.add(badge.sprite);
return { group, ring, pin };
}
const sourceBeacon = createSourceBeacon();
samplePanel.group.add(sourceBeacon.group);
// -------------------------------------------------------------------------
// Reach paths and sampling logic
// -------------------------------------------------------------------------
const routeMaterial = new THREE.LineBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.27,
depthWrite: false
});
const frontierMaterial = new THREE.LineBasicMaterial({
color: 0x91ffe0,
transparent: true,
opacity: 0.8,
depthWrite: false
});
const routeLines = new THREE.LineSegments(new THREE.BufferGeometry(), routeMaterial);
const frontierLines = new THREE.LineSegments(new THREE.BufferGeometry(), frontierMaterial);
routeLines.renderOrder = 2;
frontierLines.renderOrder = 3;
samplePanel.group.add(routeLines, frontierLines);
let currentRadius = 8;
function updateSample() {
let selectedCount = 0;
const selected = new Set();
for (const visual of sampleVisuals) {
const isSelected = visual.distance <= currentRadius;
visual.root.userData.selected = isSelected;
visual.house.visible = isSelected;
visual.halo.visible = isSelected;
visual.footprint.visible = !isSelected;
visual.tile.material = isSelected ? selectedTileMaterial : excludedTileMaterial;
if (isSelected) {
selectedCount++;
selected.add(visual.household.index);
}
}
const routePositions = [];
const frontierPositions = [];
function addSegment(target, ax, az, bx, bz) {
target.push(ax, 0.067, az, bx, 0.067, bz);
}
for (const visual of sampleVisuals) {
if (!selected.has(visual.household.index)) continue;
const { row, col } = visual.household;
const x = gridX(col);
const z = gridZ(row);
if (col + 1 < COLS && selected.has(row * COLS + col + 1)) {
addSegment(routePositions, x, z, x + CELL, z);
}
if (row + 1 < ROWS && selected.has((row + 1) * COLS + col)) {
addSegment(routePositions, x, z, x, z + CELL);
}
if (visual.distance === currentRadius) {
const half = CELL * 0.37;
addSegment(frontierPositions, x - half, z - half, x + half, z - half);
addSegment(frontierPositions, x + half, z - half, x + half, z + half);
}
}
if (selected.has((ROWS - 1) * COLS)) {
addSegment(
routePositions,
-cornerX,
cornerZ,
gridX(0),
gridZ(ROWS - 1)
);
}
routeLines.geometry.dispose();
routeLines.geometry = new THREE.BufferGeometry();
routeLines.geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(routePositions, 3)
);
frontierLines.geometry.dispose();
frontierLines.geometry = new THREE.BufferGeometry();
frontierLines.geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(frontierPositions, 3)
);
samplePanel.label.update(
"REACHABLE SAMPLE",
`${selectedCount} / 96 HOUSEHOLDS REACHED`
);
document.getElementById("coverage-count").textContent = selectedCount;
document.getElementById("radius-value").textContent =
`${currentRadius} grid step${currentRadius === 1 ? "" : "s"}`;
}
const radiusInput = document.getElementById("radius");
radiusInput.addEventListener("input", () => {
currentRadius = Number(radiusInput.value);
updateSample();
hideTooltip();
});
document.getElementById("show-routes").addEventListener("change", event => {
routeLines.visible = event.target.checked;
frontierLines.visible = event.target.checked;
});
updateSample();
// -------------------------------------------------------------------------
// Direct camera controls
// -------------------------------------------------------------------------
function updateCamera() {
const { azimuth, polar } = cameraState;
const radius = 28;
camera.position.set(
cameraTarget.x + radius * Math.sin(polar) * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraState.azimuth = 0.22;
cameraState.polar = 0.94;
cameraState.zoom = 1.06;
updateCamera();
}
document.getElementById("reset-view").addEventListener("click", resetCamera);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const halfWidth = 10.65;
const halfHeight = halfWidth / aspect;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.left = -halfWidth;
camera.right = halfWidth;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// -------------------------------------------------------------------------
// Hover inspection
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const tooltip = document.getElementById("tooltip");
function hitTest(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(interactiveRoots, true);
for (const intersection of intersections) {
let object = intersection.object;
while (object && !object.userData.household) {
object = object.parent;
}
if (object?.userData.household) return object;
}
return null;
}
function showTooltip(root, clientX, clientY) {
const household = root.userData.household;
const type = TYPE_INFO[household.type];
document.getElementById("tt-dot").style.background = type.css;
document.getElementById("tt-id").textContent = household.id;
document.getElementById("tt-type").textContent = household.typeName;
document.getElementById("tt-size").textContent = `${household.size} people`;
document.getElementById("tt-income").textContent = household.income;
document.getElementById("tt-tenure").textContent = household.tenure;
document.getElementById("tt-position").textContent =
`R${String(household.row + 1).padStart(2, "0")} · C${String(household.col + 1).padStart(2, "0")}`;
const status = document.getElementById("tt-status");
if (root.userData.panel === "full") {
status.textContent = "In full population frame";
status.style.color = "#ffc857";
} else if (root.userData.selected) {
status.textContent = "Reachable · sampled";
status.style.color = "#7cf7c8";
} else {
status.textContent = "Outside reachable area";
status.style.color = "#829491";
}
tooltip.style.display = "block";
const tooltipWidth = tooltip.offsetWidth;
const tooltipHeight = tooltip.offsetHeight;
const left = Math.min(clientX + 16, window.innerWidth - tooltipWidth - 12);
const top = Math.min(clientY + 16, window.innerHeight - tooltipHeight - 12);
tooltip.style.left = `${Math.max(10, left)}px`;
tooltip.style.top = `${Math.max(10, top)}px`;
}
function hideTooltip() {
tooltip.style.display = "none";
}
// -------------------------------------------------------------------------
// Pointer orbiting and picking
// -------------------------------------------------------------------------
let pointerDown = false;
let dragging = false;
let previousX = 0;
let previousY = 0;
let activePointerId = null;
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = true;
dragging = false;
activePointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
hideTooltip();
});
renderer.domElement.addEventListener("pointermove", event => {
if (pointerDown) {
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 4) {
dragging = true;
}
if (dragging) {
cameraState.azimuth -= deltaX * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + deltaY * 0.005,
0.56,
1.30
);
updateCamera();
}
previousX = event.clientX;
previousY = event.clientY;
} else {
const root = hitTest(event.clientX, event.clientY);
if (root) {
showTooltip(root, event.clientX, event.clientY);
} else {
hideTooltip();
}
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (!dragging) {
const root = hitTest(event.clientX, event.clientY);
if (root) showTooltip(root, event.clientX, event.clientY);
else hideTooltip();
}
pointerDown = false;
dragging = false;
if (activePointerId !== null) {
renderer.domElement.releasePointerCapture(activePointerId);
activePointerId = null;
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
dragging = false;
activePointerId = null;
hideTooltip();
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) hideTooltip();
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom - event.deltaY * 0.0008,
0.78,
1.65
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLInputElement) return;
if (event.key.toLowerCase() === "r") resetCamera();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
const pulse = 1 + Math.sin(time * 2.4) * 0.055;
sampleTool.ring.scale.setScalar(pulse);
sourceBeacon.ring.scale.setScalar(1 + Math.sin(time * 2.1 + 1.2) * 0.055);
sourceBeacon.pin.position.y = 0.30 + Math.sin(time * 2.6) * 0.035;
sampleTool.beam.material.opacity = 0.075 + Math.sin(time * 3.0) * 0.018;
frontierMaterial.opacity = 0.68 + Math.sin(time * 2.5) * 0.16;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```