Prompt specification
Build or customize the Three.js 3D scene "Block within a neighborhood model" 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: Block within a neighborhood model
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/block-within-a-neighborhood-model-x1330
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows nested geographic regions. Build three street blocks inside one neighborhood outline. Select a building to highlight its containing block and neighborhood without suggesting that all boundaries follow visible streets. Low-poly.
Working Three.js Starter (block-within-a-neighborhood-model-x1330):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Millriver Nested District</title>
<style>
:root {
--ink: #233431;
--muted: #65736f;
--paper: rgba(250, 248, 240, 0.9);
--paper-solid: #faf8f0;
--line: rgba(35, 52, 49, 0.15);
--building: #efad45;
--block: #45ad9f;
--neighborhood: #dc654f;
--street: #667174;
--shadow: 0 12px 35px rgba(45, 54, 48, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e5e0d4;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.map-label {
position: absolute;
transform: translate(-50%, -50%);
padding: 6px 9px 5px;
border: 1px solid rgba(38, 64, 58, 0.2);
border-radius: 3px;
background: rgba(250, 248, 239, 0.88);
box-shadow: 0 3px 12px rgba(45, 54, 48, 0.1);
color: #3d514b;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
}
.map-label.block-label.selected {
border-color: rgba(17, 111, 101, 0.48);
background: rgba(220, 246, 239, 0.95);
color: #12695f;
}
.map-label.neighborhood-label {
border-color: rgba(191, 83, 63, 0.36);
background: rgba(255, 239, 222, 0.9);
color: #9d4939;
}
.map-label.street-label {
padding: 3px 6px;
border: 0;
background: rgba(245, 240, 225, 0.72);
box-shadow: none;
color: rgba(241, 237, 219, 0.92);
font-size: 8px;
font-style: italic;
font-weight: 700;
letter-spacing: 0.12em;
}
.hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.card {
border: 1px solid rgba(49, 66, 61, 0.13);
border-radius: 8px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
width: 350px;
padding: 20px 21px 17px;
pointer-events: auto;
}
.eyebrow {
margin-bottom: 9px;
color: #7b6a52;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3vw, 39px);
font-weight: 500;
letter-spacing: -0.035em;
line-height: 0.98;
}
.intro {
max-width: 290px;
margin: 13px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.button-row {
display: flex;
gap: 8px;
}
button {
min-height: 32px;
padding: 7px 11px;
border: 1px solid rgba(37, 64, 57, 0.2);
border-radius: 4px;
background: rgba(255, 255, 255, 0.62);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--block);
outline-offset: 2px;
}
.legend-card {
position: absolute;
top: 22px;
right: 22px;
width: 286px;
padding: 17px 18px;
pointer-events: auto;
}
.section-title {
margin: 0 0 12px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-item {
display: grid;
grid-template-columns: 24px 1fr;
gap: 9px;
align-items: center;
margin: 9px 0;
font-size: 11px;
line-height: 1.3;
}
.swatch {
position: relative;
width: 24px;
height: 11px;
border-radius: 2px;
}
.swatch.building {
background: var(--building);
}
.swatch.block {
background: var(--block);
}
.swatch.neighborhood {
background: transparent;
border-top: 3px dashed var(--neighborhood);
height: 8px;
}
.swatch.street {
height: 7px;
background: var(--street);
}
.legend-item small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.boundary-note {
margin-top: 14px;
padding: 11px 12px;
border-left: 3px solid var(--neighborhood);
background: rgba(221, 101, 79, 0.07);
color: #6f5b52;
font-size: 10px;
line-height: 1.5;
}
.boundary-note strong {
color: #8e4739;
}
.controls {
position: absolute;
bottom: 22px;
left: 22px;
padding: 10px 12px;
border: 1px solid rgba(49, 66, 61, 0.12);
border-radius: 5px;
background: rgba(250, 248, 240, 0.82);
color: #53645f;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.025em;
box-shadow: 0 7px 22px rgba(45, 54, 48, 0.09);
}
.controls span {
color: #a04e3d;
}
.selection-panel {
position: absolute;
right: 22px;
bottom: 22px;
width: 318px;
padding: 17px 18px;
pointer-events: auto;
}
.selection-empty {
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.selection-details {
display: none;
}
.selection-kicker {
margin-bottom: 12px;
color: #8c6449;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.selection-title {
margin: 0 0 13px;
font-family: Georgia, "Times New Roman", serif;
font-size: 23px;
font-weight: 500;
letter-spacing: -0.02em;
}
.hierarchy {
display: grid;
gap: 7px;
}
.hierarchy-row {
display: grid;
grid-template-columns: 8px 78px 1fr;
gap: 9px;
align-items: center;
font-size: 10px;
}
.hierarchy-row i {
width: 8px;
height: 8px;
border-radius: 50%;
}
.hierarchy-row span {
color: var(--muted);
}
.hierarchy-row strong {
font-weight: 750;
}
.clear-button {
display: none;
margin-top: 13px;
}
#tooltip {
position: fixed;
z-index: 20;
display: none;
min-width: 155px;
max-width: 220px;
padding: 9px 11px;
border: 1px solid rgba(38, 55, 51, 0.15);
border-radius: 5px;
background: rgba(32, 46, 43, 0.93);
box-shadow: 0 8px 24px rgba(24, 33, 31, 0.2);
color: #f7f3e9;
pointer-events: none;
}
#tooltip strong,
#tooltip span {
display: block;
}
#tooltip strong {
margin-bottom: 3px;
font-size: 11px;
}
#tooltip span {
color: #c8d2cd;
font-size: 9px;
letter-spacing: 0.04em;
}
.corner-note {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
color: rgba(48, 63, 59, 0.55);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.title-card {
top: 14px;
left: 14px;
width: min(340px, calc(100vw - 28px));
padding: 16px;
}
h1 {
font-size: 29px;
}
.intro {
margin: 10px 0 12px;
}
.legend-card {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
left: 14px;
text-align: center;
}
.selection-panel {
right: 14px;
bottom: 62px;
left: 14px;
width: auto;
}
.corner-note {
display: none;
}
}
</style>
</head>
<body>
<div id="labels" aria-hidden="true"></div>
<div class="hud">
<header class="card title-card">
<div class="eyebrow">Nested geographic regions</div>
<h1>Millriver<br>District Atlas</h1>
<p class="intro">
Three street blocks sit inside one aggregate neighborhood.
Select a building to trace its containing geography.
</p>
<div class="button-row">
<button id="resetView" type="button">Reset view</button>
</div>
</header>
<aside class="card legend-card" aria-label="Map legend">
<h2 class="section-title">Selection hierarchy</h2>
<div class="legend-item">
<div class="swatch building"></div>
<div>
Selected building
<small>Individual property</small>
</div>
</div>
<div class="legend-item">
<div class="swatch block"></div>
<div>
Containing block
<small>Street-block geography</small>
</div>
</div>
<div class="legend-item">
<div class="swatch neighborhood"></div>
<div>
Neighborhood
<small>Aggregate geographic boundary</small>
</div>
</div>
<div class="legend-item">
<div class="swatch street"></div>
<div>
Visible street
<small>Transport context, not a region layer</small>
</div>
</div>
<div class="boundary-note">
<strong>Boundaries are independent.</strong>
The dashed neighborhood outline crosses streets at oblique
angles, while block edges are inset from the visible curbs.
</div>
</aside>
<div class="controls">
<span>Drag</span> orbit ·
<span>Shift-drag / right-drag</span> pan ·
<span>Scroll</span> zoom ·
<span>Click</span> a building
</div>
<section class="card selection-panel" aria-live="polite">
<div id="selectionEmpty" class="selection-empty">
No building selected. Click any low-poly building to highlight
its block and the surrounding neighborhood.
</div>
<div id="selectionDetails" class="selection-details">
<div class="selection-kicker">Selected hierarchy</div>
<h2 id="buildingName" class="selection-title"></h2>
<div class="hierarchy">
<div class="hierarchy-row">
<i style="background:#efad45"></i>
<span>Building</span>
<strong id="selectedBuilding"></strong>
</div>
<div class="hierarchy-row">
<i style="background:#45ad9f"></i>
<span>Block</span>
<strong id="selectedBlock"></strong>
</div>
<div class="hierarchy-row">
<i style="background:#dc654f"></i>
<span>Neighborhood</span>
<strong id="selectedNeighborhood"></strong>
</div>
</div>
<button id="clearSelection" class="clear-button" type="button">
Clear selection
</button>
</div>
</section>
<div class="corner-note">Schematic geography · not to scale</div>
</div>
<div id="tooltip"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
// ---------------------------------------------------------------------
// Renderer, scene, and camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.id = "scene";
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly map of the Millriver neighborhood"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e5e0d4");
scene.fog = new THREE.Fog("#e5e0d4", 90, 165);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
240
);
const cameraState = {
target: new THREE.Vector3(0, 0, 0),
radius: 64,
theta: 0.72,
phi: 0.94
};
const defaultCamera = { ...cameraState };
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.copy(defaultCamera.target);
cameraState.radius = defaultCamera.radius;
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
updateCamera();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight("#fff9e8", "#819085", 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff2cf", 3.0);
sun.position.set(-28, 46, 31);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 48;
sun.shadow.camera.bottom = -48;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 115;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fillLight = new THREE.DirectionalLight("#c9e0df", 0.75);
fillLight.position.set(30, 18, -26);
scene.add(fillLight);
// ---------------------------------------------------------------------
// Geometry helpers
// ---------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function makeBox(
width,
height,
depth,
x,
y,
z,
material,
castShadow = false,
receiveShadow = false
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
scene.add(mesh);
return mesh;
}
function polygonShape(points) {
const shape = new THREE.Shape();
points.forEach(([x, z], index) => {
if (index === 0) shape.moveTo(x, -z);
else shape.lineTo(x, -z);
});
shape.closePath();
return shape;
}
function createPolygonMesh(points, material, y) {
const geometry = new THREE.ShapeGeometry(polygonShape(points));
geometry.rotateX(-Math.PI / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y = y;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function createDashedOutline(
points,
color,
width,
dashLength,
gapLength,
y
) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({ color });
for (let i = 0; i < points.length; i++) {
const [x1, z1] = points[i];
const [x2, z2] = points[(i + 1) % points.length];
const dx = x2 - x1;
const dz = z2 - z1;
const length = Math.hypot(dx, dz);
const angle = -Math.atan2(dz, dx);
let distance = 0;
while (distance < length) {
const segmentLength = Math.min(dashLength, length - distance);
if (segmentLength < 0.12) break;
const middle = distance + segmentLength / 2;
const dash = new THREE.Mesh(
new THREE.BoxGeometry(segmentLength, 0.055, width),
material
);
dash.position.set(
x1 + (dx * middle) / length,
y,
z1 + (dz * middle) / length
);
dash.rotation.y = angle;
dash.renderOrder = 8;
group.add(dash);
distance += dashLength + gapLength;
}
}
scene.add(group);
return { group, material };
}
function makeSegment(width, color, a, b, y = 0.1) {
const dx = b[0] - a[0];
const dz = b[1] - a[1];
const length = Math.hypot(dx, dz);
const angle = -Math.atan2(dz, dx);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 1,
metalness: 0
});
const segment = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.055, width),
material
);
segment.position.set(
(a[0] + b[0]) / 2,
y,
(a[1] + b[1]) / 2
);
segment.rotation.y = angle;
segment.receiveShadow = true;
scene.add(segment);
return segment;
}
function createGableGeometry(width, depth, roofHeight) {
const w = width / 2;
const d = depth / 2;
const positions = [
-w, 0, -d,
w, 0, -d,
0, roofHeight, -d,
-w, 0, d,
w, 0, d,
0, roofHeight, 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
];
let geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry = geometry.toNonIndexed();
geometry.computeVertexNormals();
return geometry;
}
// ---------------------------------------------------------------------
// Ground, neighborhood, streets, blocks, and park
// ---------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: "#ded8ca",
roughness: 1,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(180, 180),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.2;
ground.receiveShadow = true;
scene.add(ground);
const neighborhoodPoints = [
[-26, -13],
[-22, -19],
[-10, -21],
[1, -24],
[14, -23],
[24, -17],
[27, -5],
[25, 7],
[20, 17],
[9, 22],
[-4, 24],
[-17, 20],
[-25, 11],
[-28, 0]
];
const neighborhoodFillMaterial = new THREE.MeshStandardMaterial({
color: "#cbd4bd",
roughness: 1,
metalness: 0,
side: THREE.DoubleSide
});
createPolygonMesh(
neighborhoodPoints,
neighborhoodFillMaterial,
-0.035
);
const neighborhoodOutline = createDashedOutline(
neighborhoodPoints,
"#b58443",
0.34,
1.65,
0.8,
0.19
);
const sidewalkMaterial = new THREE.MeshStandardMaterial({
color: "#d8d0bb",
roughness: 1
});
const roadMaterial = new THREE.MeshStandardMaterial({
color: "#626d70",
roughness: 0.96
});
const markingMaterial = new THREE.MeshStandardMaterial({
color: "#e8dbaa",
roughness: 0.85,
emissive: "#5d512c",
emissiveIntensity: 0.05
});
// Visible roads continue beyond the neighborhood and cross its outline.
makeBox(59, 0.12, 5.5, 0, 0.01, -1, sidewalkMaterial, false, true);
makeBox(59, 0.08, 3.8, 0, 0.065, -1, roadMaterial, false, true);
makeBox(5.5, 0.12, 48, -4, 0.014, 0, sidewalkMaterial, false, true);
makeBox(3.8, 0.08, 48, -4, 0.069, 0, roadMaterial, false, true);
// Lane dashes.
for (let x = -27; x <= 27; x += 3.2) {
if (Math.abs(x + 4) < 4.2) continue;
makeBox(1.35, 0.025, 0.10, x, 0.12, -1, markingMaterial);
}
for (let z = -22; z <= 22; z += 3.2) {
if (Math.abs(z + 1) < 4.2) continue;
makeBox(0.10, 0.025, 1.35, -4, 0.125, z, markingMaterial);
}
// Crosswalks around the intersection.
for (let i = 0; i < 6; i++) {
makeBox(
0.48,
0.025,
3.2,
-6.5 + i * 0.72,
0.128,
-1,
markingMaterial
);
}
for (let i = 0; i < 6; i++) {
makeBox(
3.2,
0.025,
0.48,
-4,
0.133,
-4.0 + i * 0.72,
markingMaterial
);
}
const blockDefinitions = {
market: {
name: "Market Block",
points: [
[-22, -16],
[-15, -17],
[-9, -15],
[-8, -11],
[-8, -4],
[-22, -4]
],
baseFill: "#d9cda9",
baseOutline: "#738b78",
labelAnchor: [-15, 0.25, -9.3]
},
foundry: {
name: "Foundry Block",
points: [
[0, -17],
[9, -18],
[17, -15],
[22, -10],
[21, -3],
[0, -3]
],
baseFill: "#d6c9aa",
baseOutline: "#738b78",
labelAnchor: [10, 0.25, -9.4]
},
orchard: {
name: "Orchard Block",
points: [
[-22, 4],
[-12, 3],
[-8, 7],
[-8, 17],
[-18, 18],
[-23, 12]
],
baseFill: "#d6cbaa",
baseOutline: "#738b78",
labelAnchor: [-15, 0.25, 10.5]
}
};
const blocks = {};
for (const [key, definition] of Object.entries(blockDefinitions)) {
const fillMaterial = new THREE.MeshStandardMaterial({
color: definition.baseFill,
roughness: 1,
metalness: 0,
side: THREE.DoubleSide
});
const fill = createPolygonMesh(
definition.points,
fillMaterial,
0.025
);
const outline = createDashedOutline(
definition.points,
definition.baseOutline,
0.15,
0.8,
0.42,
0.145
);
blocks[key] = {
...definition,
fill,
fillMaterial,
outline
};
}
const parkPoints = [
[1, 5],
[10, 3],
[19, 6],
[18, 17],
[8, 20],
[0, 16]
];
const parkMaterial = new THREE.MeshStandardMaterial({
color: "#aec49a",
roughness: 1,
side: THREE.DoubleSide
});
createPolygonMesh(parkPoints, parkMaterial, 0.018);
makeSegment(1.15, "#d7c7a3", [1, 16], [7, 12]);
makeSegment(1.15, "#d7c7a3", [7, 12], [11, 7]);
makeSegment(1.15, "#d7c7a3", [11, 7], [18, 6]);
makeSegment(0.9, "#d7c7a3", [7, 12], [12, 16]);
// ---------------------------------------------------------------------
// Trees, lamps, and benches
// ---------------------------------------------------------------------
const trunkMaterial = new THREE.MeshStandardMaterial({
color: "#755d42",
roughness: 1,
flatShading: true
});
const foliageMaterial = new THREE.MeshStandardMaterial({
color: "#54785d",
roughness: 1,
flatShading: true
});
const trunkGeometry = new THREE.CylinderGeometry(0.17, 0.24, 1.6, 5);
const foliageGeometry = new THREE.IcosahedronGeometry(1.2, 0);
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.04, z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.y = 0.8;
trunk.castShadow = true;
const foliage = new THREE.Mesh(foliageGeometry, foliageMaterial);
foliage.position.y = 2.05;
foliage.rotation.set(0.2, 0.5, 0.1);
foliage.castShadow = true;
group.add(trunk, foliage);
scene.add(group);
}
[
[3.0, 14.2, 1.05],
[4.8, 8.1, 0.86],
[9.4, 16.8, 1.15],
[12.8, 11.0, 0.95],
[17.2, 14.0, 1.12],
[8.3, 5.6, 0.82],
[17.1, 8.4, 0.9],
[-21.0, 8.0, 0.8],
[-6.4, -18.3, 0.85],
[22.1, -10.5, 0.9]
].forEach(([x, z, scale]) => addTree(x, z, scale));
const lampPostMaterial = new THREE.MeshStandardMaterial({
color: "#3f4c49",
roughness: 0.65,
metalness: 0.2
});
const lampGlowMaterial = new THREE.MeshStandardMaterial({
color: "#ffe2a0",
emissive: "#ffc967",
emissiveIntensity: 0.65,
roughness: 0.5
});
function addLamp(x, z) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.08, 3.2, 6),
lampPostMaterial
);
post.position.set(x, 1.6, z);
post.castShadow = true;
const light = new THREE.Mesh(
new THREE.OctahedronGeometry(0.19, 0),
lampGlowMaterial
);
light.position.set(x, 3.25, z);
scene.add(post, light);
}
addLamp(-7.3, -2.5);
addLamp(-0.7, -2.5);
addLamp(-7.3, 0.5);
addLamp(-0.7, 0.5);
addLamp(-6.3, 11.5);
const benchMaterial = new THREE.MeshStandardMaterial({
color: "#8c6546",
roughness: 0.95
});
function addBench(x, z, rotation = 0) {
const seat = new THREE.Mesh(
new THREE.BoxGeometry(1.8, 0.18, 0.45),
benchMaterial
);
seat.position.set(x, 0.55, z);
seat.rotation.y = rotation;
seat.castShadow = true;
const legMaterial = lampPostMaterial;
const legA = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.5, 0.32),
legMaterial
);
legA.position.set(x - 0.6, 0.25, z);
legA.rotation.y = rotation;
const legB = legA.clone();
legB.position.x = x + 0.6;
scene.add(seat, legA, legB);
}
addBench(6.2, 13.1, -0.65);
addBench(14.0, 10.0, 0.3);
// ---------------------------------------------------------------------
// Low-poly buildings
// ---------------------------------------------------------------------
const windowMaterial = new THREE.MeshStandardMaterial({
color: "#30454a",
roughness: 0.35,
metalness: 0.06,
emissive: "#15272b",
emissiveIntensity: 0.12
});
const doorMaterial = new THREE.MeshStandardMaterial({
color: "#4e4338",
roughness: 0.9
});
const buildingSpecs = [
// Market Block
{
name: "Alder House", block: "market",
x: -19.3, z: -12.2, w: 4.2, d: 3.2, h: 5.2,
color: "#c56f52", roof: "#70403b", roofType: "gable"
},
{
name: "Copper Works", block: "market",
x: -13.7, z: -12.8, w: 3.8, d: 4.2, h: 7.2,
color: "#d1a45e", roof: "#66574a", roofType: "flat"
},
{
name: "Lantern Shop", block: "market",
x: -19.4, z: -7.1, w: 4.2, d: 3.2, h: 4.2,
color: "#799489", roof: "#435b55", roofType: "gable"
},
{
name: "Mercer Studio", block: "market",
x: -13.5, z: -7.2, w: 3.7, d: 3.4, h: 5.8,
color: "#e2c9a4", roof: "#72594c", roofType: "gable"
},
{
name: "North House", block: "market",
x: -9.8, z: -11.5, w: 2.2, d: 3.6, h: 4.7,
color: "#829ca7", roof: "#4b5865", roofType: "gable"
},
// Foundry Block
{
name: "Signal House", block: "foundry",
x: 2.3, z: -12.3, w: 3.2, d: 3.6, h: 6.1,
color: "#b76852", roof: "#593c38", roofType: "gable"
},
{
name: "Brick Hall", block: "foundry",
x: 7.2, z: -13.1, w: 6.4, d: 4.1, h: 5.3,
color: "#a95443", roof: "#6b3e36", roofType: "flat"
},
{
name: "Tall Loom", block: "foundry",
x: 15.2, z: -13.3, w: 4.0, d: 4.0, h: 8.3,
color: "#d0a763", roof: "#69584a", roofType: "flat"
},
{
name: "East Works", block: "foundry",
x: 18.5, z: -7.5, w: 3.4, d: 4.4, h: 5.7,
color: "#6f8c82", roof: "#43564f", roofType: "gable"
},
{
name: "Rail Office", block: "foundry",
x: 7.1, z: -7.0, w: 4.6, d: 3.2, h: 4.5,
color: "#dfc49c", roof: "#6b5142", roofType: "flat"
},
{
name: "Thread House", block: "foundry",
x: 2.7, z: -6.7, w: 3.0, d: 3.0, h: 5.3,
color: "#c27b61", roof: "#704139", roofType: "gable"
},
// Orchard Block
{
name: "Orchard Hall", block: "orchard",
x: -18.5, z: 8.8, w: 5.0, d: 4.0, h: 6.2,
color: "#d7b270", roof: "#6c5545", roofType: "flat"
},
{
name: "Juniper House", block: "orchard",
x: -12.4, z: 7.3, w: 3.3, d: 3.4, h: 5.1,
color: "#6d8d82", roof: "#3f574f", roofType: "gable"
},
{
name: "Orchard Press", block: "orchard",
x: -12.1, z: 13.2, w: 4.0, d: 3.5, h: 7.0,
color: "#b55d4c", roof: "#633d38", roofType: "gable"
},
{
name: "Grove House", block: "orchard",
x: -19.1, z: 14.0, w: 3.0, d: 3.0, h: 4.5,
color: "#e0c59f", roof: "#725548", roofType: "gable"
},
{
name: "Cedar Annex", block: "orchard",
x: -10.2, z: 15.1, w: 2.5, d: 2.7, h: 4.2,
color: "#839ca8", roof: "#485964", roofType: "gable"
},
{
name: "Willow Court", block: "orchard",
x: -10.4, z: 10.6, w: 3.8, d: 3.2, h: 4.3,
color: "#b88461", roof: "#68483d", roofType: "flat"
}
];
const buildings = [];
const pickMeshes = [];
function addBuilding(spec) {
const record = {
...spec,
baseColor: spec.color,
baseRoofColor: spec.roof
};
const group = new THREE.Group();
group.position.set(spec.x, 0.12, spec.z);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: spec.color,
roughness: 0.88,
metalness: 0,
flatShading: true
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: spec.roof,
roughness: 0.94,
metalness: 0,
flatShading: true
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(spec.w, spec.h, spec.d),
bodyMaterial
);
body.position.y = spec.h / 2;
body.castShadow = true;
body.receiveShadow = true;
const podium = new THREE.Mesh(
new THREE.BoxGeometry(spec.w + 0.28, 0.18, spec.d + 0.28),
new THREE.MeshStandardMaterial({
color: "#a79d87",
roughness: 1
})
);
podium.position.y = 0.09;
podium.receiveShadow = true;
let roof;
if (spec.roofType === "gable") {
roof = new THREE.Mesh(
createGableGeometry(spec.w + 0.35, spec.d + 0.35, 1.15),
roofMaterial
);
} else {
roof = new THREE.Mesh(
new THREE.BoxGeometry(spec.w + 0.3, 0.25, spec.d + 0.3),
roofMaterial
);
roof.position.y = 0.12;
}
roof.position.y += spec.h;
roof.castShadow = true;
body.userData.building = record;
roof.userData.building = record;
group.add(podium, body, roof);
scene.add(group);
// Simple facade windows.
const rows = Math.max(1, Math.min(3, Math.floor((spec.h - 0.8) / 1.55)));
const columns = Math.max(1, Math.floor((spec.w - 0.6) / 1.25));
for (let row = 0; row < rows; row++) {
const y = 1.15 + row * 1.55;
if (y + 0.28 > spec.h - 0.15) continue;
for (let column = 0; column < columns; column++) {
const x =
-spec.w / 2 +
0.72 +
column * ((spec.w - 1.44) / Math.max(1, columns - 1));
const windowMesh = new THREE.Mesh(unitBox, windowMaterial);
windowMesh.scale.set(0.48, 0.58, 0.065);
windowMesh.position.set(x, y, spec.d / 2 + 0.04);
group.add(windowMesh);
}
}
// One window on the east-facing wall.
for (let row = 0; row < Math.min(2, rows); row++) {
const y = 1.25 + row * 1.6;
if (y + 0.28 > spec.h - 0.15) continue;
const sideWindow = new THREE.Mesh(unitBox, windowMaterial);
sideWindow.scale.set(0.065, 0.55, 0.46);
sideWindow.position.set(spec.w / 2 + 0.04, y, 0.25);
group.add(sideWindow);
}
const door = new THREE.Mesh(unitBox, doorMaterial);
door.scale.set(0.72, 1.15, 0.08);
door.position.set(-spec.w * 0.18, 0.58, spec.d / 2 + 0.045);
group.add(door);
record.body = body;
record.roof = roof;
record.bodyMaterial = bodyMaterial;
record.roofMaterial = roofMaterial;
buildings.push(record);
pickMeshes.push(body, roof);
}
buildingSpecs.forEach(addBuilding);
// ---------------------------------------------------------------------
// Labels
// ---------------------------------------------------------------------
const mapLabels = [];
function addMapLabel(text, className, anchor) {
const element = document.createElement("div");
element.className = `map-label ${className}`;
element.textContent = text;
$("labels").appendChild(element);
const label = {
element,
anchor: new THREE.Vector3(...anchor)
};
mapLabels.push(label);
return label;
}
for (const [key, block] of Object.entries(blocks)) {
block.labelElement = addMapLabel(
block.name,
"block-label",
block.labelAnchor
).element;
}
addMapLabel(
"Millriver Neighborhood",
"neighborhood-label",
[10.6, 0.3, 11.0]
);
addMapLabel(
"Common Street",
"street-label",
[-17.5, 0.28, -1]
);
addMapLabel(
"North Lane",
"street-label",
[-4, 0.28, 13.5]
);
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of mapLabels) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.08 &&
projected.x < 1.08 &&
projected.y > -1.08 &&
projected.y < 1.08;
label.element.style.display = visible ? "block" : "none";
if (visible) {
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
// ---------------------------------------------------------------------
// Selection and highlighting
// ---------------------------------------------------------------------
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(1, 0.055, 6, 44),
new THREE.MeshBasicMaterial({
color: "#efad45",
transparent: true,
opacity: 0.9,
depthWrite: false
})
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.visible = false;
selectionRing.renderOrder = 10;
scene.add(selectionRing);
let selectedBuilding = null;
function resetRegionColors() {
for (const block of Object.values(blocks)) {
block.fillMaterial.color.set(block.baseFill);
block.outline.material.color.set(block.baseOutline);
block.labelElement.classList.remove("selected");
}
neighborhoodFillMaterial.color.set("#cbd4bd");
neighborhoodOutline.material.color.set("#b58443");
}
function restoreBuilding(record) {
if (!record) return;
record.bodyMaterial.color.set(record.baseColor);
record.bodyMaterial.emissive.set("#000000");
record.bodyMaterial.emissiveIntensity = 0;
record.roofMaterial.color.set(record.baseRoofColor);
record.roofMaterial.emissive.set("#000000");
record.roofMaterial.emissiveIntensity = 0;
}
function selectBuilding(record) {
if (!record) return;
restoreBuilding(selectedBuilding);
resetRegionColors();
selectedBuilding = record;
record.bodyMaterial.color.set("#efad45");
record.bodyMaterial.emissive.set("#8b4d0c");
record.bodyMaterial.emissiveIntensity = 0.24;
record.roofMaterial.color.set("#f7cf74");
record.roofMaterial.emissive.set("#8b4d0c");
record.roofMaterial.emissiveIntensity = 0.12;
const block = blocks[record.block];
block.fillMaterial.color.set("#52b6a5");
block.outline.material.color.set("#176f66");
block.labelElement.classList.add("selected");
neighborhoodFillMaterial.color.set("#e6cda2");
neighborhoodOutline.material.color.set("#dc654f");
selectionRing.visible = true;
selectionRing.position.set(record.x, 0.19, record.z);
selectionRing.userData.radius =
Math.max(record.w, record.d) * 0.75 + 0.65;
$("selectionEmpty").style.display = "none";
$("selectionDetails").style.display = "block";
$("buildingName").textContent = record.name;
$("selectedBuilding").textContent = record.name;
$("selectedBlock").textContent = block.name;
$("selectedNeighborhood").textContent = "Millriver";
$("clearSelection").style.display = "inline-block";
}
function clearSelection() {
restoreBuilding(selectedBuilding);
resetRegionColors();
selectedBuilding = null;
selectionRing.visible = false;
$("selectionEmpty").style.display = "block";
$("selectionDetails").style.display = "none";
$("clearSelection").style.display = "none";
}
// ---------------------------------------------------------------------
// Picking
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickBuilding(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 intersection = raycaster.intersectObjects(pickMeshes, false)[0];
return intersection ? intersection.object.userData.building : null;
}
const tooltip = $("tooltip");
function showTooltip(record, event) {
tooltip.innerHTML = `
<strong>${record.name}</strong>
<span>${blocks[record.block].name}</span>
`;
tooltip.style.display = "block";
const tooltipWidth = 180;
const left = Math.min(
event.clientX + 14,
window.innerWidth - tooltipWidth - 12
);
const top = Math.min(
event.clientY + 14,
window.innerHeight - 70
);
tooltip.style.left = `${Math.max(8, left)}px`;
tooltip.style.top = `${Math.max(8, top)}px`;
}
function hideTooltip() {
tooltip.style.display = "none";
}
// ---------------------------------------------------------------------
// Custom camera controls: orbit, pan, wheel zoom, and touch pinch
// ---------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
const gesture = {
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false,
multi: false,
pinchDistance: 0,
pinchCenterX: 0,
pinchCenterY: 0
};
function clampCameraTarget() {
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-23,
23
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-21,
21
);
cameraState.target.y = 0;
}
function panCamera(dx, dy) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const scale = cameraState.radius * 0.00145;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(forward, dy * scale);
clampCameraTarget();
}
function updatePinchState() {
const points = [...pointers.values()];
if (points.length < 2) return;
const [a, b] = points;
gesture.pinchDistance = Math.hypot(
b.x - a.x,
b.y - a.y
);
gesture.pinchCenterX = (a.x + b.x) / 2;
gesture.pinchCenterY = (a.y + b.y) / 2;
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.focus();
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
gesture.startX = event.clientX;
gesture.startY = event.clientY;
gesture.lastX = event.clientX;
gesture.lastY = event.clientY;
gesture.moved = false;
gesture.multi = false;
} else {
gesture.multi = true;
gesture.moved = true;
updatePinchState();
}
hideTooltip();
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) {
if (event.pointerType === "mouse") {
const record = pickBuilding(event.clientX, event.clientY);
if (record) {
canvas.style.cursor = "pointer";
showTooltip(record, event);
} else {
canvas.style.cursor = "grab";
hideTooltip();
}
}
return;
}
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - gesture.lastX;
const dy = event.clientY - gesture.lastY;
gesture.lastX = event.clientX;
gesture.lastY = event.clientY;
if (
Math.hypot(
event.clientX - gesture.startX,
event.clientY - gesture.startY
) > 5
) {
gesture.moved = true;
}
const wantsPan =
event.pointerType === "mouse" &&
(event.buttons === 2 || event.shiftKey);
if (wantsPan) {
panCamera(dx, dy);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.34,
1.36
);
}
} else {
const oldDistance = gesture.pinchDistance;
const oldCenterX = gesture.pinchCenterX;
const oldCenterY = gesture.pinchCenterY;
updatePinchState();
if (oldDistance > 0 && gesture.pinchDistance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius *
(oldDistance / gesture.pinchDistance),
28,
96
);
}
panCamera(
gesture.pinchCenterX - oldCenterX,
gesture.pinchCenterY - oldCenterY
);
}
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
const wasMulti = gesture.multi;
const shouldSelect =
pointers.size === 1 &&
!wasMulti &&
!gesture.moved &&
event.button === 0;
pointers.delete(event.pointerId);
if (shouldSelect) {
const record = pickBuilding(event.clientX, event.clientY);
if (record) selectBuilding(record);
}
if (pointers.size === 0) {
gesture.moved = false;
gesture.multi = false;
canvas.style.cursor = "grab";
} else {
const remaining = [...pointers.entries()][0];
gesture.lastX = remaining[1].x;
gesture.lastY = remaining[1].y;
gesture.startX = remaining[1].x;
gesture.startY = remaining[1].y;
gesture.moved = true;
}
});
canvas.addEventListener("pointercancel", (event) => {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
gesture.moved = false;
gesture.multi = false;
canvas.style.cursor = "grab";
}
});
canvas.addEventListener("pointerleave", () => {
if (pointers.size === 0) hideTooltip();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
28,
96
);
updateCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------
// UI controls and keyboard shortcuts
// ---------------------------------------------------------------------
$("resetView").addEventListener("click", resetCamera);
$("clearSelection").addEventListener("click", clearSelection);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
if (event.key === "Escape") {
clearSelection();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// Start with an example selection so the hierarchy is immediately legible.
selectBuilding(buildings[0]);
// ---------------------------------------------------------------------
// Render loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
if (selectionRing.visible) {
const pulse = 1 + Math.sin(clock.elapsedTime * 3.2) * 0.035;
const radius = selectionRing.userData.radius || 3;
selectionRing.scale.setScalar(radius * pulse);
selectionRing.material.opacity =
0.76 + Math.sin(clock.elapsedTime * 3.2) * 0.12;
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```