Prompt specification
Build or customize the Three.js 3D scene "School paths around a fenced field" 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: School paths around a fenced field
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/school-paths-around-a-fenced-field-x1282
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring service catchment areas. Build a school, housing row, and fenced field with two gates. Trace actual walking paths and contrast accessible travel distance with direct geometric distance. Low-poly.
Working Three.js Starter (school-paths-around-a-fenced-field-x1282):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Walking Catchments — Low-Poly Accessibility Study</title>
<style>
:root {
--ink: #173331;
--muted: #607775;
--panel: rgba(250, 252, 248, 0.94);
--line: rgba(23, 51, 49, 0.13);
--walk: #13bfd1;
--walk-dark: #087f91;
--direct: #ff7168;
--school: #23b6a8;
--field: #e7aa3d;
--shadow: 0 16px 50px rgba(35, 62, 56, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e2;
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%;
touch-action: none;
}
.brand {
position: fixed;
top: 20px;
left: 22px;
z-index: 5;
max-width: 420px;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255,255,255,.6);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--walk-dark);
font-size: 10px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 23px;
height: 3px;
border-radius: 4px;
background: var(--walk);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.02;
letter-spacing: -.045em;
font-weight: 780;
}
.brand p {
max-width: 370px;
margin: 10px 0 0;
color: #49625f;
font-size: 13px;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 6;
top: 18px;
right: 18px;
width: min(340px, calc(100vw - 36px));
padding: 18px;
border: 1px solid rgba(255,255,255,.78);
border-radius: 20px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.panel-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
}
.panel-title strong {
font-size: 13px;
letter-spacing: -.01em;
}
.live-dot {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: .08em;
text-transform: uppercase;
}
.live-dot::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: #38b877;
box-shadow: 0 0 0 4px rgba(56,184,119,.12);
}
.control-label {
display: block;
margin: 14px 0 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .11em;
text-transform: uppercase;
}
.origin-buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
button, select {
font: inherit;
}
.origin-buttons button {
min-height: 39px;
border: 1px solid var(--line);
border-radius: 10px;
background: #fff;
color: var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: .18s ease;
}
.origin-buttons button:hover {
border-color: rgba(19,191,209,.55);
transform: translateY(-1px);
}
.origin-buttons button.active {
border-color: var(--walk);
background: var(--walk);
color: #fff;
box-shadow: 0 5px 12px rgba(19,191,209,.23);
}
select {
width: 100%;
height: 42px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 10px;
outline: none;
background: #fff;
color: var(--ink);
font-size: 12px;
font-weight: 650;
cursor: pointer;
}
select:focus, button:focus-visible {
outline: 3px solid rgba(19,191,209,.25);
outline-offset: 2px;
}
.toggles {
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
margin: 15px 0 17px;
padding: 12px 0;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
}
.toggle {
display: flex;
align-items: center;
gap: 6px;
color: #47605d;
cursor: pointer;
font-size: 11px;
font-weight: 650;
}
.toggle input {
width: 14px;
height: 14px;
margin: 0;
accent-color: var(--walk);
}
.route-title {
margin-bottom: 9px;
font-size: 14px;
font-weight: 780;
letter-spacing: -.02em;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
min-height: 88px;
padding: 12px;
border-radius: 13px;
background: rgba(23,51,49,.045);
}
.metric.walking {
background: rgba(19,191,209,.11);
}
.metric.direct {
background: rgba(255,113,104,.11);
}
.metric span {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .1em;
text-transform: uppercase;
}
.metric strong {
display: block;
font-size: 25px;
line-height: 1;
letter-spacing: -.04em;
}
.metric small {
display: block;
margin-top: 6px;
color: var(--muted);
font-size: 9px;
}
.bars {
margin-top: 13px;
}
.bar-row {
display: grid;
grid-template-columns: 48px 1fr 47px;
align-items: center;
gap: 8px;
margin: 6px 0;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.bar-track {
height: 6px;
overflow: hidden;
border-radius: 5px;
background: rgba(23,51,49,.08);
}
.bar-fill {
width: 0;
height: 100%;
border-radius: inherit;
transition: width .35s ease;
}
#walkBar { background: var(--walk); }
#directBar { background: var(--direct); }
.detour {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.detour span {
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.detour strong {
font-size: 15px;
}
.legend {
position: fixed;
z-index: 5;
left: 22px;
bottom: 22px;
width: 270px;
padding: 13px 15px;
border: 1px solid rgba(255,255,255,.72);
border-radius: 15px;
background: rgba(250,252,248,.88);
box-shadow: 0 10px 35px rgba(35,62,56,.11);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-title {
margin-bottom: 9px;
font-size: 9px;
font-weight: 850;
letter-spacing: .13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 7px 0;
color: #526865;
font-size: 10px;
}
.swatch {
width: 29px;
height: 4px;
flex: 0 0 auto;
border-radius: 5px;
}
.swatch.walk { background: var(--walk); }
.swatch.direct {
height: 0;
border-top: 2px dashed var(--direct);
border-radius: 0;
}
.swatch.school { background: rgba(35,182,168,.5); }
.swatch.field { background: rgba(231,170,61,.55); }
.hint {
position: fixed;
z-index: 4;
right: 22px;
bottom: 19px;
color: rgba(23,51,49,.65);
font-size: 10px;
font-weight: 650;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #dce8e2;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
transition: opacity .35s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 780px) {
.brand {
top: 14px;
left: 15px;
max-width: calc(100vw - 30px);
}
.brand p { display: none; }
h1 { font-size: 26px; }
.panel {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
max-height: 48vh;
overflow-y: auto;
padding: 14px;
border-radius: 17px;
}
.legend, .hint { display: none; }
.panel-title { margin-bottom: 8px; }
.control-label { margin-top: 9px; }
.toggles { margin: 10px 0; padding: 9px 0; }
.metric { min-height: 73px; padding: 9px; }
.metric strong { font-size: 21px; }
.bars { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly map showing walking catchments around a school and field"></canvas>
<header class="brand">
<div class="eyebrow">Network accessibility study</div>
<h1>Walking catchments</h1>
<p>
A network-based view of school and field access from a low-poly housing row.
Solid routes follow usable paths; dashed routes show misleading straight-line distance.
</p>
</header>
<aside class="panel" aria-label="Walking route controls">
<div class="panel-title">
<strong>Compare a journey</strong>
<span class="live-dot">Live model</span>
</div>
<span class="control-label">Starting dwelling</span>
<div class="origin-buttons" role="group" aria-label="Choose starting dwelling">
<button type="button" data-origin="0">D1</button>
<button type="button" data-origin="1" class="active">D2</button>
<button type="button" data-origin="2">D3</button>
<button type="button" data-origin="3">D4</button>
</div>
<label class="control-label" for="destination">Destination</label>
<select id="destination">
<option value="ga">Field · Gate A</option>
<option value="gb">Field · Gate B</option>
<option value="school">School entrance</option>
</select>
<div class="toggles">
<label class="toggle">
<input id="showCatchments" type="checkbox" checked>
Catchments
</label>
<label class="toggle">
<input id="showDirect" type="checkbox" checked>
Direct line
</label>
<label class="toggle">
<input id="showLabels" type="checkbox" checked>
Labels
</label>
</div>
<div id="routeSummary" aria-live="polite">
<div class="route-title" id="routeTitle">Dwelling 2 → Field Gate A</div>
<div class="metrics">
<div class="metric walking">
<span>Walkable route</span>
<strong id="walkDistance">—</strong>
<small>following mapped paths</small>
</div>
<div class="metric direct">
<span>Direct distance</span>
<strong id="directDistance">—</strong>
<small>geometric straight line</small>
</div>
</div>
<div class="bars" aria-hidden="true">
<div class="bar-row">
<span>WALK</span>
<div class="bar-track"><div class="bar-fill" id="walkBar"></div></div>
<span id="walkBarValue"></span>
</div>
<div class="bar-row">
<span>DIRECT</span>
<div class="bar-track"><div class="bar-fill" id="directBar"></div></div>
<span id="directBarValue"></span>
</div>
</div>
<div class="detour">
<span>Extra distance imposed by the path network</span>
<strong id="detourDistance">—</strong>
</div>
</div>
</aside>
<div class="legend" aria-hidden="true">
<div class="legend-title">Map legend</div>
<div class="legend-row"><span class="swatch walk"></span>Accessible walking route</div>
<div class="legend-row"><span class="swatch direct"></span>Direct geometric distance</div>
<div class="legend-row"><span class="swatch school"></span>School · 110 m walking catchment</div>
<div class="legend-row"><span class="swatch field"></span>Field · 150 m walking catchment</div>
</div>
<div class="hint">Drag to orbit · Shift/right-drag to pan · Scroll to zoom · Click a building or gate</div>
<div id="loading">Building walking network…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8e2);
scene.fog = new THREE.Fog(0xdce8e2, 72, 125);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
180
);
const cameraTarget = new THREE.Vector3(-1, 0, 3);
const spherical = new THREE.Spherical(76, 0.86, 0.68);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
const hemi = new THREE.HemisphereLight(0xf7fff9, 0x8aa39a, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.25);
sun.position.set(-25, 48, 34);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 115;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9d9df, 0.75);
fill.position.set(40, 18, -30);
scene.add(fill);
const worldUnitsToMeters = 2;
const schoolCatchmentMeters = 110;
const fieldCatchmentMeters = 150;
// -------------------------------------------------------------------------
// Scene groups
// -------------------------------------------------------------------------
const siteGroup = new THREE.Group();
const pathGroup = new THREE.Group();
const accessRouteGroup = new THREE.Group();
const catchmentGroup = new THREE.Group();
const staticLabelGroup = new THREE.Group();
const dynamicRouteGroup = new THREE.Group();
const interactiveGroup = new THREE.Group();
scene.add(
siteGroup,
pathGroup,
accessRouteGroup,
catchmentGroup,
staticLabelGroup,
dynamicRouteGroup,
interactiveGroup
);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
function box(
width, height, depth, color,
x = 0, y = 0, z = 0,
parent = siteGroup,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
standardMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function outline(mesh, color = 0x36504c, opacity = 0.28) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 30);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
lines.position.copy(mesh.position);
lines.rotation.copy(mesh.rotation);
lines.scale.copy(mesh.scale);
mesh.parent.add(lines);
return lines;
}
function cylinder(
radiusTop, radiusBottom, height, radialSegments,
color, x, y, z, parent = siteGroup
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
radialSegments
),
standardMaterial(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function gableGeometry(width, depth, roofHeight) {
const w = width / 2;
const d = depth / 2;
const positions = new Float32Array([
-w, 0, -d,
-w, 0, d,
-w, roofHeight, 0,
w, 0, -d,
w, 0, d,
w, roofHeight, 0
]);
const indices = [
0, 1, 2,
3, 5, 4,
0, 2, 5, 0, 5, 3,
1, 4, 5, 1, 5, 2,
0, 3, 4, 0, 4, 1
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function addGableRoof(width, depth, height, color, x, y, z, parent) {
const roof = new THREE.Mesh(
gableGeometry(width, depth, height),
standardMaterial(color, { roughness: 0.8 })
);
roof.position.set(x, y, z);
roof.castShadow = true;
roof.receiveShadow = true;
parent.add(roof);
return roof;
}
function makeLabel(text, options = {}) {
const {
color = "#173331",
background = "rgba(250,252,248,0.95)",
border = "rgba(23,51,49,0.16)",
width = 640,
height = 128,
worldWidth = 7,
worldHeight = 1.4
} = options;
const c = document.createElement("canvas");
c.width = width;
c.height = height;
const ctx = c.getContext("2d");
const radius = 24;
const x = 4;
const y = 4;
const w = width - 8;
const h = height - 8;
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + w - radius, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + radius);
ctx.lineTo(x + w, y + h - radius);
ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
ctx.lineTo(x + radius, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = border;
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "700 42px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, width / 2, height / 2 + 2);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldHeight, 1);
sprite.renderOrder = 30;
return sprite;
}
function clearGroup(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach(material => {
if (material.map) material.map.dispose();
material.dispose();
});
}
});
}
}
// -------------------------------------------------------------------------
// Ground and landscape
// -------------------------------------------------------------------------
const ground = box(
82, 0.45, 62, 0xd8e2d6,
0, -0.24, 1,
siteGroup,
{ roughness: 1 }
);
outline(ground, 0x6f877e, 0.18);
// Subtle grass patches.
box(27, 0.035, 20, 0xc9d9bd, -22, 0.025, -9, siteGroup);
box(29, 0.035, 12, 0xd1ddc9, -17, 0.025, 18.5, siteGroup);
box(28, 0.035, 20, 0xcbd9c1, 19, 0.025, -10, siteGroup);
// -------------------------------------------------------------------------
// Walking graph
// -------------------------------------------------------------------------
const houseCenters = [-28, -21, -14, -7];
const houseNodes = ["h0", "h1", "h2", "h3"];
const spineNodes = ["p0", "p1", "p2", "p3", "p4", "p5", "p6"];
const nodes = {};
const edges = [];
houseCenters.forEach((centerX, index) => {
const doorX = centerX + 3.4;
nodes[houseNodes[index]] = { x: doorX, z: 18.5 };
nodes[spineNodes[index]] = { x: doorX, z: 8 };
edges.push([houseNodes[index], spineNodes[index]]);
});
nodes.p4 = { x: 0, z: 8 };
nodes.p5 = { x: 11, z: 8 };
nodes.p6 = { x: 25, z: 8 };
nodes.schoolCorner = { x: -13.2, z: 8 };
nodes.school = { x: -13.2, z: -10 };
nodes.ga = { x: 11, z: -1.2 };
nodes.gb = { x: 25, z: -1.2 };
for (let i = 0; i < spineNodes.length - 1; i++) {
edges.push([spineNodes[i], spineNodes[i + 1]]);
}
edges.push(["p2", "schoolCorner"]);
edges.push(["schoolCorner", "school"]);
edges.push(["p5", "ga"]);
edges.push(["p6", "gb"]);
const adjacency = Object.fromEntries(Object.keys(nodes).map(id => [id, []]));
edges.forEach(([a, b]) => {
const distance = Math.hypot(
nodes[b].x - nodes[a].x,
nodes[b].z - nodes[a].z
);
adjacency[a].push({ id: b, distance });
adjacency[b].push({ id: a, distance });
});
function dijkstra(start) {
const distances = {};
const previous = {};
const remaining = new Set(Object.keys(nodes));
Object.keys(nodes).forEach(id => {
distances[id] = Infinity;
previous[id] = null;
});
distances[start] = 0;
while (remaining.size) {
let current = null;
let currentDistance = Infinity;
remaining.forEach(id => {
if (distances[id] < currentDistance) {
current = id;
currentDistance = distances[id];
}
});
if (current === null) break;
remaining.delete(current);
adjacency[current].forEach(neighbor => {
const candidate = distances[current] + neighbor.distance;
if (candidate < distances[neighbor.id]) {
distances[neighbor.id] = candidate;
previous[neighbor.id] = current;
}
});
}
return { distances, previous };
}
function shortestPath(start, end) {
const result = dijkstra(start);
const path = [];
let current = end;
while (current !== null) {
path.unshift(current);
if (current === start) break;
current = result.previous[current];
}
return path.length ? path : [start, end];
}
function routeLength(path) {
let total = 0;
for (let i = 0; i < path.length - 1; i++) {
total += Math.hypot(
nodes[path[i + 1]].x - nodes[path[i]].x,
nodes[path[i + 1]].z - nodes[path[i]].z
);
}
return total;
}
function addPathSegment(a, b, width = 1.55) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const path = box(
length + 0.12, 0.075, width, 0xe8dfcc,
(a.x + b.x) / 2,
0.105,
(a.z + b.z) / 2,
pathGroup,
{ roughness: 1 }
);
path.rotation.y = -Math.atan2(dz, dx);
path.castShadow = false;
}
edges.forEach(([a, b]) => addPathSegment(nodes[a], nodes[b]));
// -------------------------------------------------------------------------
// School
// -------------------------------------------------------------------------
const schoolGroup = new THREE.Group();
schoolGroup.position.set(-23, 0, -10);
schoolGroup.userData.destination = "school";
interactiveGroup.add(schoolGroup);
const schoolBody = box(
16, 3.8, 10, 0xf0e2c3,
0, 1.9, 0,
schoolGroup
);
outline(schoolBody);
addGableRoof(17.1, 11.2, 2.25, 0xad5b43, 0, 3.8, 0, schoolGroup);
// Windows on both long elevations.
[-6.1, -3.1, 0, 3.1, 6.1].forEach(x => {
box(1.75, 1.35, 0.12, 0x436d73, x, 2.05, 5.06, schoolGroup);
box(1.75, 1.35, 0.12, 0x436d73, x, 2.05, -5.06, schoolGroup);
});
// East-facing entrance and canopy, aligned with the walking network.
box(0.15, 2.5, 1.8, 0x234d57, 8.08, 1.25, 0, schoolGroup);
box(2.7, 0.18, 3.1, 0x2d7b84, 9.05, 3.05, 0, schoolGroup);
box(0.18, 3.05, 0.18, 0x2d7b84, 10.25, 1.52, 1.35, schoolGroup);
box(0.18, 3.05, 0.18, 0x2d7b84, 10.25, 1.52, -1.35, schoolGroup);
// School clock.
const clock = cylinder(
0.63, 0.63, 0.12, 12,
0xf7f0db, 0, 4.0, 5.12, schoolGroup
);
clock.rotation.x = Math.PI / 2;
const schoolLabel = makeLabel("COMMUNITY SCHOOL", {
color: "#116d68",
worldWidth: 8.4,
worldHeight: 1.55
});
schoolLabel.position.set(-23, 7.6, -8);
staticLabelGroup.add(schoolLabel);
// -------------------------------------------------------------------------
// Housing row
// -------------------------------------------------------------------------
const houseColors = [0xe7d6b7, 0xd9c3a6, 0xefdfc3, 0xd6bfa2];
const roofColors = [0x9b5747, 0x455f67, 0x9b5747, 0x536d63];
houseCenters.forEach((centerX, index) => {
const group = new THREE.Group();
group.position.set(centerX, 0, 18.5);
group.userData.origin = index;
interactiveGroup.add(group);
const body = box(
5.5, 3.25, 6.5, houseColors[index],
0, 1.625, 0,
group
);
outline(body);
addGableRoof(
6.25, 7.25, 1.65,
roofColors[index],
0, 3.25, 0,
group
);
// East-facing entrance visible from the default camera.
box(0.14, 2.05, 1.2, 0x386a68, 2.82, 1.025, 0, group);
box(0.16, 0.12, 1.45, 0xf2e5ca, 2.9, 2.13, 0, group);
// South windows.
[-1.55, 1.55].forEach(x => {
box(1.25, 1.15, 0.12, 0x4c7274, x, 1.85, -3.31, group);
});
// Small front step and path marker.
box(1.15, 0.18, 1.35, 0xd9cfbb, 3.25, 0.09, 0, group);
const numberLabel = makeLabel(`D${index + 1}`, {
width: 220,
height: 110,
worldWidth: 2.05,
worldHeight: 1.03,
color: "#35524e"
});
numberLabel.position.set(centerX + 3.5, 3.7, 18.5);
staticLabelGroup.add(numberLabel);
});
const housingLabel = makeLabel("HOUSING ROW", {
color: "#5e5748",
worldWidth: 5.6,
worldHeight: 1.15
});
housingLabel.position.set(-18, 7.3, 19.3);
staticLabelGroup.add(housingLabel);
// -------------------------------------------------------------------------
// Fenced field and two gates
// -------------------------------------------------------------------------
const fieldGroup = new THREE.Group();
siteGroup.add(fieldGroup);
const fieldSurface = box(
22, 0.07, 16, 0x87ad69,
18, 0.055, -10,
fieldGroup,
{ roughness: 1 }
);
fieldSurface.castShadow = false;
outline(fieldSurface, 0x58774e, 0.25);
// Alternating mowing bands.
for (let i = 0; i < 4; i++) {
const stripe = box(
5.45, 0.012, 15.5,
i % 2 ? 0x8fb473 : 0x82a966,
10.25 + i * 5.5,
0.095,
-10,
fieldGroup,
{ roughness: 1 }
);
stripe.castShadow = false;
}
function fenceSegment(x1, z1, x2, z2, parent = fieldGroup) {
const dx = x2 - x1;
const dz = z2 - z1;
const length = Math.hypot(dx, dz);
const angle = -Math.atan2(dz, dx);
const count = Math.max(1, Math.ceil(length / 2.25));
for (let i = 0; i <= count; i++) {
const t = i / count;
const post = box(
0.13, 2.3, 0.13, 0x50635f,
THREE.MathUtils.lerp(x1, x2, t),
1.15,
THREE.MathUtils.lerp(z1, z2, t),
parent
);
post.rotation.y = angle;
}
[0.65, 1.3, 1.95].forEach(y => {
const rail = box(
length, 0.075, 0.075, 0x60716d,
(x1 + x2) / 2,
y,
(z1 + z2) / 2,
parent
);
rail.rotation.y = angle;
rail.castShadow = false;
});
}
// North, east and west fence lines.
fenceSegment(7, -18, 29, -18);
fenceSegment(7, -18, 7, -2);
fenceSegment(29, -18, 29, -2);
// South fence has two explicit openings.
fenceSegment(7, -2, 9.4, -2);
fenceSegment(12.6, -2, 23.4, -2);
fenceSegment(26.6, -2, 29, -2);
function addOpenGate(hingeX, destination, letter) {
const group = new THREE.Group();
group.position.set(hingeX, 0, -2);
group.rotation.y = 1.05;
group.userData.destination = destination;
interactiveGroup.add(group);
[0.55, 1.15, 1.75].forEach(y => {
box(3.05, 0.09, 0.09, 0x3e8175, 1.55, y, 0, group);
});
box(0.12, 2.05, 0.12, 0x3e8175, 3.05, 1.02, 0, group);
box(0.12, 2.05, 0.12, 0x3e8175, 0.08, 1.02, 0, group);
const label = makeLabel(`GATE ${letter}`, {
color: "#146c62",
worldWidth: 3.25,
worldHeight: .8
});
label.position.set(hingeX + 1.6, 3.1, -1.7);
staticLabelGroup.add(label);
}
addOpenGate(9.4, "ga", "A");
addOpenGate(23.4, "gb", "B");
// Field markings.
const lineColor = 0xe7eee0;
const fieldLineGroup = new THREE.Group();
fieldGroup.add(fieldLineGroup);
box(19.5, 0.025, 0.10, lineColor, 18, 0.13, -16.7, fieldLineGroup);
box(19.5, 0.025, 0.10, lineColor, 18, 0.13, -3.3, fieldLineGroup);
box(0.10, 0.025, 13.4, lineColor, 8.25, 0.13, -10, fieldLineGroup);
box(0.10, 0.025, 13.4, lineColor, 27.75, 0.13, -10, fieldLineGroup);
box(0.10, 0.025, 13.4, lineColor, 18, 0.13, -10, fieldLineGroup);
// Small goals.
[-13.1, -6.9].forEach(z => {
box(0.13, 1.7, 0.13, 0xf1f3e9, 8.8, 0.95, z, fieldGroup);
box(0.13, 1.7, 0.13, 0xf1f3e9, 27.2, 0.95, z, fieldGroup);
});
box(0.13, 0.13, 6.35, 0xf1f3e9, 8.8, 1.8, -10, fieldGroup);
box(0.13, 0.13, 6.35, 0xf1f3e9, 27.2, 1.8, -10, fieldGroup);
const fieldLabel = makeLabel("FENCED FIELD", {
color: "#7a5312",
worldWidth: 6.3,
worldHeight: 1.25
});
fieldLabel.position.set(18, 4.1, -11.5);
staticLabelGroup.add(fieldLabel);
// -------------------------------------------------------------------------
// Trees, lamps and small urban details
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1, color = 0x4f8060) {
const group = new THREE.Group();
group.position.set(x, 0, z);
siteGroup.add(group);
cylinder(
0.18 * scale, 0.25 * scale, 2.1 * scale, 5,
0x80654d, 0, 1.05 * scale, 0, group
);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.45 * scale, 0),
standardMaterial(color, { flatShading: true })
);
crown.position.y = 3.05 * scale;
crown.scale.set(1, 1.15, 0.9);
crown.rotation.y = x * 0.37;
crown.castShadow = true;
crown.receiveShadow = true;
group.add(crown);
}
[
[-35, -18, 1.05], [-34, -3, .85], [-33, 13, 1],
[-35, 25, .9], [-5, 25, .9], [5, 25, 1.1],
[34, 24, .95], [35, 10, 1.05], [34, -23, .9],
[3, -23, 1], [-16, -23, .9], [21, 13, .8]
].forEach(args => addTree(...args));
function addLamp(x, z) {
cylinder(0.07, 0.1, 3.3, 6, 0x435b59, x, 1.65, z);
box(0.75, 0.13, 0.32, 0x435b59, x + 0.28, 3.25, z);
box(0.54, 0.08, 0.25, 0xffedb3, x + 0.3, 3.14, z);
}
addLamp(-24.6, 6.9);
addLamp(-3.6, 6.9);
addLamp(11, 6.9);
addLamp(25, 6.9);
// Benches along the housing walk.
[-24.6, -10.6].forEach(x => {
box(2.2, .16, .65, 0x987557, x + 1.25, .72, 6.2);
box(.14, .75, .5, 0x50615d, x + .35, .37, 6.2);
box(.14, .75, .5, 0x50615d, x + 2.15, .37, 6.2);
});
// -------------------------------------------------------------------------
// Route drawing helpers
// -------------------------------------------------------------------------
function pathPoints(path) {
return path.map(id => new THREE.Vector3(
nodes[id].x,
0.40,
nodes[id].z
));
}
function drawTube(path, color, radius, opacity, parent, renderOrder = 4) {
const points = pathPoints(path);
const curve = new THREE.CurvePath();
for (let i = 0; i < points.length - 1; i++) {
curve.add(new THREE.LineCurve3(points[i], points[i + 1]));
}
const tube = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
Math.max(20, points.length * 9),
radius,
6,
false
),
new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity,
depthWrite: false
})
);
tube.renderOrder = renderOrder;
parent.add(tube);
return tube;
}
// Faint access traces from every dwelling to every service.
houseNodes.forEach(origin => {
drawTube(
shortestPath(origin, "school"),
0x22b7aa,
0.085,
0.30,
accessRouteGroup,
2
);
drawTube(
shortestPath(origin, "ga"),
0xe2a43a,
0.075,
0.22,
accessRouteGroup,
2
);
drawTube(
shortestPath(origin, "gb"),
0xe2a43a,
0.075,
0.18,
accessRouteGroup,
2
);
});
// -------------------------------------------------------------------------
// Network-derived service catchments
//
// For each sample point, the model finds its least costly connection to the
// pedestrian network and then follows the graph to the service entrance.
// -------------------------------------------------------------------------
function distanceFieldToService(serviceDistances, x, z) {
let best = Infinity;
Object.keys(nodes).forEach(id => {
const connector = Math.hypot(x - nodes[id].x, z - nodes[id].z);
best = Math.min(
best,
serviceDistances[id] + connector
);
});
return best;
}
function createCatchmentLayer(name, selected, color, boundaryColor) {
const positions = [];
const step = 2;
const minX = -36;
const maxX = 36;
const minZ = -24;
const maxZ = 28;
const columns = Math.round((maxX - minX) / step);
const rows = Math.round((maxZ - minZ) / step);
const y = 0.165;
selected.forEach(index => {
const col = index % columns;
const row = Math.floor(index / columns);
const x0 = minX + col * step;
const z0 = minZ + row * step;
const x1 = x0 + step;
const z1 = z0 + step;
positions.push(
x0, y, z0, x1, y, z0, x1, y, z1,
x0, y, z0, x1, y, z1, x0, y, z1
);
});
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const fill = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: name === "school" ? 0.20 : 0.17,
depthWrite: false,
side: THREE.DoubleSide
})
);
fill.renderOrder = 1;
catchmentGroup.add(fill);
// Pixelated but deliberate low-poly catchment boundaries.
const boundaryPositions = [];
selected.forEach(index => {
const col = index % columns;
const row = Math.floor(index / columns);
const x0 = minX + col * step;
const z0 = minZ + row * step;
const x1 = x0 + step;
const z1 = z0 + step;
const has = (c, r) => selected.has(r * columns + c);
if (!has(col, row - 1)) {
boundaryPositions.push(x0, y + .025, z0, x1, y + .025, z0);
}
if (!has(col, row + 1)) {
boundaryPositions.push(x0, y + .025, z1, x1, y + .025, z1);
}
if (!has(col - 1, row)) {
boundaryPositions.push(x0, y + .025, z0, x0, y + .025, z1);
}
if (!has(col + 1, row)) {
boundaryPositions.push(x1, y + .025, z0, x1, y + .025, z1);
}
});
const boundaryGeometry = new THREE.BufferGeometry();
boundaryGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(boundaryPositions, 3)
);
const boundary = new THREE.LineSegments(
boundaryGeometry,
new THREE.LineBasicMaterial({
color: boundaryColor,
transparent: true,
opacity: 0.72
})
);
boundary.renderOrder = 3;
catchmentGroup.add(boundary);
}
const schoolDistances = dijkstra("school").distances;
const gateADistances = dijkstra("ga").distances;
const gateBDistances = dijkstra("gb").distances;
const schoolCells = new Set();
const fieldCells = new Set();
const gridStep = 2;
const gridMinX = -36;
const gridMaxX = 36;
const gridMinZ = -24;
const gridMaxZ = 28;
const gridColumns = Math.round((gridMaxX - gridMinX) / gridStep);
const gridRows = Math.round((gridMaxZ - gridMinZ) / gridStep);
for (let row = 0; row < gridRows; row++) {
for (let col = 0; col < gridColumns; col++) {
const x = gridMinX + (col + .5) * gridStep;
const z = gridMinZ + (row + .5) * gridStep;
const index = row * gridColumns + col;
const schoolDistance = distanceFieldToService(schoolDistances, x, z);
const fieldDistance = Math.min(
distanceFieldToService(gateADistances, x, z),
distanceFieldToService(gateBDistances, x, z)
);
const schoolReach =
schoolDistance * worldUnitsToMeters <= schoolCatchmentMeters;
const fieldReach =
fieldDistance * worldUnitsToMeters <= fieldCatchmentMeters;
if (schoolReach && fieldReach) {
if (schoolDistance <= fieldDistance) schoolCells.add(index);
else fieldCells.add(index);
} else if (schoolReach) {
schoolCells.add(index);
} else if (fieldReach) {
fieldCells.add(index);
}
}
}
createCatchmentLayer("school", schoolCells, 0x2fc4b4, 0x188f87);
createCatchmentLayer("field", fieldCells, 0xf0b84e, 0xb77c1c);
// -------------------------------------------------------------------------
// Interactive route state
// -------------------------------------------------------------------------
let selectedOrigin = "h1";
let selectedDestination = "ga";
let directLine = null;
let routeLabels = [];
const destinationNames = {
ga: "Field Gate A",
gb: "Field Gate B",
school: "School entrance"
};
const originNames = {
h0: "Dwelling 1",
h1: "Dwelling 2",
h2: "Dwelling 3",
h3: "Dwelling 4"
};
function markerRing(x, z, color) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(.72, .11, 7, 24),
new THREE.MeshBasicMaterial({
color,
depthTest: false,
depthWrite: false
})
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, .48, z);
ring.renderOrder = 20;
dynamicRouteGroup.add(ring);
return ring;
}
function addRouteArrow(path, fraction, color) {
const points = pathPoints(path);
const index = Math.min(
points.length - 2,
Math.max(0, Math.floor((points.length - 1) * fraction))
);
const from = points[index];
const to = points[index + 1];
const direction = new THREE.Vector3().subVectors(to, from).normalize();
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(.38, 1.05, 3),
new THREE.MeshBasicMaterial({
color,
depthTest: false,
depthWrite: false
})
);
arrow.position.copy(from).lerp(to, .58);
arrow.position.y = .52;
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction
);
arrow.renderOrder = 21;
dynamicRouteGroup.add(arrow);
}
function updateRoute() {
clearGroup(dynamicRouteGroup);
routeLabels = [];
const path = shortestPath(selectedOrigin, selectedDestination);
const walkDistance = routeLength(path);
const start = nodes[selectedOrigin];
const end = nodes[selectedDestination];
const directDistance = Math.hypot(
end.x - start.x,
end.z - start.z
);
const walkMeters = Math.round(walkDistance * worldUnitsToMeters);
const directMeters = Math.round(directDistance * worldUnitsToMeters);
const extraMeters = Math.max(0, walkMeters - directMeters);
const extraPercent = directMeters
? Math.round((walkMeters / directMeters - 1) * 100)
: 0;
// Bright actual walking route.
drawTube(path, 0x13bfd1, .19, 1, dynamicRouteGroup, 12);
addRouteArrow(path, .32, 0xffffff);
addRouteArrow(path, .68, 0xffffff);
// Direct geometric comparison.
const directGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(start.x, .47, start.z),
new THREE.Vector3(end.x, .47, end.z)
]);
directLine = new THREE.Line(
directGeometry,
new THREE.LineDashedMaterial({
color: 0xff7168,
dashSize: 1.15,
gapSize: .62,
transparent: true,
opacity: .95,
depthTest: false
})
);
directLine.computeLineDistances();
directLine.renderOrder = 15;
dynamicRouteGroup.add(directLine);
markerRing(start.x, start.z, 0x13bfd1);
markerRing(end.x, end.z, 0x13bfd1);
const routeMid = pathPoints(path)[Math.floor(path.length / 2)];
const walkLabel = makeLabel(`WALK ${walkMeters} m`, {
color: "#087f91",
worldWidth: 4.5,
worldHeight: .92
});
walkLabel.position.set(routeMid.x, 1.55, routeMid.z);
dynamicRouteGroup.add(walkLabel);
routeLabels.push(walkLabel);
const directLabel = makeLabel(`DIRECT ${directMeters} m`, {
color: "#bd4f49",
worldWidth: 4.8,
worldHeight: .92
});
directLabel.position.set(
(start.x + end.x) / 2,
1.15,
(start.z + end.z) / 2
);
dynamicRouteGroup.add(directLabel);
routeLabels.push(directLabel);
document.querySelector("#routeTitle").textContent =
`${originNames[selectedOrigin]} → ${destinationNames[selectedDestination]}`;
document.querySelector("#walkDistance").textContent = `${walkMeters} m`;
document.querySelector("#directDistance").textContent = `${directMeters} m`;
document.querySelector("#detourDistance").textContent =
`+${extraMeters} m · ${extraPercent}%`;
const maxDistance = Math.max(walkMeters, directMeters, 1);
document.querySelector("#walkBar").style.width =
`${walkMeters / maxDistance * 100}%`;
document.querySelector("#directBar").style.width =
`${directMeters / maxDistance * 100}%`;
document.querySelector("#walkBarValue").textContent = `${walkMeters} m`;
document.querySelector("#directBarValue").textContent = `${directMeters} m`;
applyVisibility();
}
function applyVisibility() {
catchmentGroup.visible = document.querySelector("#showCatchments").checked;
staticLabelGroup.visible = document.querySelector("#showLabels").checked;
if (directLine) {
directLine.visible = document.querySelector("#showDirect").checked;
}
routeLabels.forEach(label => {
label.visible = document.querySelector("#showLabels").checked;
});
}
document.querySelectorAll("[data-origin]").forEach(button => {
button.addEventListener("click", () => {
selectedOrigin = `h${button.dataset.origin}`;
document.querySelectorAll("[data-origin]").forEach(other => {
other.classList.toggle("active", other === button);
});
updateRoute();
});
});
document.querySelector("#destination").addEventListener("change", event => {
selectedDestination = event.target.value;
updateRoute();
});
document.querySelector("#showCatchments").addEventListener("change", applyVisibility);
document.querySelector("#showDirect").addEventListener("change", applyVisibility);
document.querySelector("#showLabels").addEventListener("change", applyVisibility);
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
let pointerDown = false;
let dragButton = 0;
let lastX = 0;
let lastY = 0;
let travel = 0;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
pointerDown = true;
dragButton = event.button;
lastX = event.clientX;
lastY = event.clientY;
travel = 0;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!pointerDown) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
travel += Math.abs(dx) + Math.abs(dy);
const panMode = dragButton === 2 || event.shiftKey;
if (panMode) {
camera.updateMatrixWorld();
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0)
.setY(0)
.normalize();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const scale = spherical.radius * .0015;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(forward, -dy * scale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -35, 35);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -25, 30);
} else {
spherical.theta -= dx * .005;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - dy * .004,
.3,
1.35
);
}
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
});
canvas.addEventListener("pointerup", event => {
pointerDown = false;
if (travel < 7) {
pickScene(event);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * .001),
40,
110
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}, { passive: false });
// -------------------------------------------------------------------------
// Clickable buildings and gates
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function findUserData(object, key) {
let current = object;
while (current) {
if (current.userData && current.userData[key] !== undefined) {
return current.userData[key];
}
current = current.parent;
}
return undefined;
}
function pickScene(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(interactiveGroup.children, true);
if (!hits.length) return;
const origin = findUserData(hits[0].object, "origin");
const destination = findUserData(hits[0].object, "destination");
if (origin !== undefined) {
selectedOrigin = `h${origin}`;
document.querySelectorAll("[data-origin]").forEach(button => {
button.classList.toggle("active", Number(button.dataset.origin) === origin);
});
updateRoute();
} else if (destination) {
selectedDestination = destination;
document.querySelector("#destination").value = destination;
updateRoute();
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateRoute();
const animationClock = new THREE.Clock();
function animate() {
const elapsed = animationClock.getElapsedTime();
// Gentle pulse on the destination ring.
dynamicRouteGroup.children.forEach(object => {
if (object.geometry && object.geometry.type === "TorusGeometry") {
const pulse = 1 + Math.sin(elapsed * 2.4) * .055;
object.scale.setScalar(pulse);
}
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```