Prompt specification
Build or customize the Three.js 3D scene "Compare administrative and river-basin boundaries" 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: Compare administrative and river-basin boundaries
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-administrative-and-river-basin-boundaries-x1332
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts nested geographic regions. Overlay a fictional municipality and watershed on the same landscape. Selecting either system highlights different memberships, demonstrating that one place can belong to both kinds of region. Voxel style.
Working Three.js Starter (compare-administrative-and-river-basin-boundaries-x1332):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Confluence Atlas — Voxel Geography</title>
<style>
:root {
--ink: #17342e;
--muted: #60766f;
--paper: rgba(249, 252, 246, 0.93);
--line: rgba(23, 52, 46, 0.13);
--municipal: #4ed7c4;
--watershed: #ffb95d;
--shared: #fff0a6;
--shadow: 0 18px 50px rgba(29, 59, 51, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7eee8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel,
.title-card,
.legend,
.help,
.reset-button {
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.title-card {
position: fixed;
top: 22px;
left: 22px;
z-index: 10;
width: min(360px, calc(100vw - 44px));
padding: 18px 20px 17px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: rgba(248, 252, 246, 0.9);
box-shadow: var(--shadow);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #557169;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--municipal);
box-shadow: 12px 0 0 var(--watershed);
content: "";
margin-right: 4px;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 36px);
font-weight: 780;
letter-spacing: -0.055em;
line-height: 1;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.panel {
position: fixed;
top: 22px;
right: 22px;
z-index: 12;
width: 326px;
max-height: calc(100vh - 44px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
scrollbar-width: thin;
}
.panel-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.fictional-tag {
padding: 4px 7px;
border: 1px solid var(--line);
border-radius: 999px;
color: #73847f;
font-size: 8px;
letter-spacing: 0.12em;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border-radius: 12px;
background: rgba(29, 65, 56, 0.07);
}
.system-button {
min-height: 38px;
border: 0;
border-radius: 9px;
background: transparent;
color: #61766f;
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: 160ms ease;
}
.system-button:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.55);
}
.system-button.active {
color: #14352e;
background: #fff;
box-shadow: 0 3px 12px rgba(35, 67, 58, 0.11);
}
#system-municipality.active {
box-shadow: inset 0 -3px 0 var(--municipal), 0 3px 12px rgba(35, 67, 58, 0.11);
}
#system-watershed.active {
box-shadow: inset 0 -3px 0 var(--watershed), 0 3px 12px rgba(35, 67, 58, 0.11);
}
.region-list {
display: grid;
gap: 7px;
margin: 13px 0 14px;
}
.region-button {
display: grid;
grid-template-columns: 18px 1fr auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
text-align: left;
cursor: pointer;
transition: 150ms ease;
}
.region-button:hover {
transform: translateX(2px);
background: #fff;
}
.region-button.active {
border-color: rgba(23, 52, 46, 0.32);
background: #fff;
box-shadow: 0 6px 18px rgba(35, 67, 58, 0.08);
}
.region-name {
display: block;
font-size: 12px;
font-weight: 760;
}
.region-meta {
display: block;
margin-top: 2px;
color: #75857f;
font-size: 9px;
}
.cell-count {
color: #6e807a;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.swatch {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 3px;
background: var(--swatch);
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.7);
}
.swatch.watershed {
border-radius: 2px;
transform: rotate(45deg);
width: 10px;
height: 10px;
}
.status-card,
.shared-card,
.hierarchy {
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.52);
}
.status-card {
padding: 12px;
}
.status-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.status-label {
color: #71827c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.overlap-count {
font-size: 18px;
font-weight: 780;
letter-spacing: -0.04em;
}
.status-copy {
margin: 5px 0 0;
color: #687c75;
font-size: 10px;
line-height: 1.45;
}
.shared-card {
display: grid;
grid-template-columns: 32px 1fr;
gap: 10px;
margin-top: 9px;
padding: 11px;
background: rgba(255, 240, 166, 0.42);
border-color: rgba(159, 124, 31, 0.16);
}
.shared-icon {
display: grid;
place-items: center;
width: 30px;
height: 30px;
border-radius: 9px;
background: #fff1a7;
color: #8a6512;
font-size: 20px;
font-weight: 500;
box-shadow: inset 0 0 0 1px rgba(138, 101, 18, 0.11);
}
.shared-card small {
display: block;
margin-bottom: 2px;
color: #91711e;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.13em;
}
.shared-card strong {
display: block;
font-size: 11px;
}
.shared-card p {
margin: 3px 0 0;
color: #786b47;
font-size: 9px;
line-height: 1.4;
}
.hierarchy {
margin-top: 9px;
padding: 11px;
}
.hierarchy-title {
margin-bottom: 8px;
color: #71827c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.hierarchy-row {
display: flex;
align-items: center;
gap: 6px;
flex-wrap: wrap;
color: #425d55;
font-size: 9px;
line-height: 1.6;
}
.hierarchy-row b {
color: #1c3d34;
font-weight: 750;
}
.hierarchy-arrow {
color: #9aaa9f;
}
.panel-note {
margin: 11px 2px 0;
color: #7b8c86;
font-size: 9px;
line-height: 1.45;
}
.legend {
position: fixed;
left: 50%;
bottom: 21px;
z-index: 9;
display: flex;
align-items: center;
gap: 17px;
padding: 10px 15px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 13px;
background: rgba(248, 252, 246, 0.88);
box-shadow: 0 9px 28px rgba(29, 59, 51, 0.12);
transform: translateX(-50%);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #60766f;
font-size: 9px;
white-space: nowrap;
}
.legend-symbol {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 3px;
background: var(--symbol);
box-shadow: inset 0 0 0 1px rgba(23, 52, 46, 0.16);
}
.legend-symbol.diamond {
width: 10px;
height: 10px;
border-radius: 2px;
transform: rotate(45deg);
}
.legend-symbol.cross {
display: grid;
place-items: center;
width: 14px;
height: 14px;
border-radius: 4px;
background: #fff1a7;
color: #8a6512;
font-size: 13px;
line-height: 1;
}
.help {
position: fixed;
right: 22px;
bottom: 21px;
z-index: 9;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 12px;
background: rgba(248, 252, 246, 0.86);
color: #60766f;
font-size: 9px;
box-shadow: 0 8px 24px rgba(29, 59, 51, 0.1);
pointer-events: none;
}
.reset-button {
position: fixed;
left: 22px;
bottom: 21px;
z-index: 11;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 11px;
background: rgba(248, 252, 246, 0.88);
color: var(--ink);
box-shadow: 0 8px 24px rgba(29, 59, 51, 0.1);
cursor: pointer;
font-size: 10px;
font-weight: 750;
}
.reset-button:hover {
background: #fff;
}
#map-labels {
position: fixed;
inset: 0;
z-index: 6;
overflow: hidden;
pointer-events: none;
}
.map-label {
position: absolute;
display: flex;
align-items: center;
gap: 5px;
padding: 4px 7px;
border: 1px solid rgba(255, 255, 255, 0.88);
border-radius: 7px;
background: rgba(249, 252, 246, 0.87);
box-shadow: 0 4px 12px rgba(31, 58, 51, 0.1);
color: #29473f;
font-size: 8px;
font-weight: 780;
letter-spacing: 0.015em;
white-space: nowrap;
transform: translate(-50%, -50%);
transition: opacity 180ms ease;
}
.map-label .label-glyph {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--label-color);
}
.map-label.watershed .label-glyph {
transform: rotate(45deg);
}
.map-label.selected {
padding: 5px 8px;
background: #fff;
box-shadow: 0 5px 15px rgba(31, 58, 51, 0.16);
}
.map-label.landmark {
border-color: rgba(142, 105, 20, 0.25);
background: rgba(255, 241, 167, 0.95);
color: #725311;
}
#tooltip {
position: fixed;
z-index: 30;
display: none;
width: 230px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.88);
border-radius: 12px;
background: rgba(250, 253, 248, 0.96);
box-shadow: 0 13px 35px rgba(26, 52, 45, 0.2);
color: var(--ink);
pointer-events: none;
font-size: 10px;
line-height: 1.45;
}
.tip-kicker {
margin-bottom: 5px;
color: #81918b;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.tip-row {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 3px;
}
.tip-row span {
color: #72847e;
}
.tip-row b {
text-align: right;
font-weight: 720;
}
.tip-shared {
margin-top: 7px;
padding-top: 6px;
border-top: 1px solid var(--line);
color: #8a6512;
font-weight: 700;
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 61px;
width: min(310px, calc(100vw - 24px));
max-height: 49vh;
padding: 13px;
}
.legend {
left: 12px;
bottom: 12px;
gap: 8px;
padding: 8px 10px;
transform: none;
}
.legend-item {
font-size: 0;
gap: 0;
}
.help {
display: none;
}
.reset-button {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="map-labels"></div>
<header class="title-card">
<div class="eyebrow">Confluence Atlas · Fictional geography</div>
<h1>Two territories.<br />One shared place.</h1>
<p class="subtitle">
Explore nested districts beneath overlapping municipal and watershed boundaries.
</p>
</header>
<aside class="panel" aria-label="Boundary system controls">
<div class="panel-title">
<span>Boundary systems</span>
<span class="fictional-tag">Voxel atlas</span>
</div>
<div class="segmented" role="group" aria-label="Select a boundary system">
<button class="system-button active" id="system-municipality" type="button">
Municipalities
</button>
<button class="system-button" id="system-watershed" type="button">
Watersheds
</button>
</div>
<div class="region-list" id="region-list"></div>
<div class="status-card">
<div class="status-top">
<div>
<div class="status-label">Cross-system cells</div>
<div id="selected-region-name" style="margin-top: 2px; font-size: 11px; font-weight: 760;"></div>
</div>
<div class="overlap-count" id="overlap-count">0</div>
</div>
<p class="status-copy" id="status-copy"></p>
</div>
<div class="shared-card">
<div class="shared-icon">+</div>
<div>
<small>Shared place</small>
<strong>Confluence Market</strong>
<p id="landmark-copy"></p>
</div>
</div>
<div class="hierarchy">
<div class="hierarchy-title">Administrative nesting</div>
<div class="hierarchy-row">
<b id="hierarchy-province"></b>
<span class="hierarchy-arrow">›</span>
<b id="hierarchy-county"></b>
<span class="hierarchy-arrow">›</span>
<b id="hierarchy-district"></b>
</div>
</div>
<p class="panel-note">
Bright cells show the selected region. Small diamonds show watershed
membership. Cream markers identify places belonging to both systems.
</p>
</aside>
<div class="legend" aria-label="Map legend">
<div class="legend-item">
<span class="legend-symbol" style="--symbol:#8ba78d"></span>
District terrain
</div>
<div class="legend-item">
<span class="legend-symbol" style="--symbol:#4ed7c4"></span>
Municipality
</div>
<div class="legend-item">
<span class="legend-symbol diamond" style="--symbol:#ffb95d"></span>
Watershed
</div>
<div class="legend-item">
<span class="legend-symbol cross">+</span>
Shared membership
</div>
</div>
<div class="help">
Drag to orbit · Shift-drag to pan · Wheel to zoom · Click a region
</div>
<button class="reset-button" id="reset-view" type="button">Reset view</button>
<div id="tooltip" role="status"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Fictional geographic definitions
// -------------------------------------------------------------------------
const COLS = 26;
const ROWS = 20;
const SPACING = 1;
const VIEW_HEIGHT = 30;
const municipalityDefs = [
{
name: "Bellwether Municipality",
short: "Bellwether",
color: "#4ed7c4",
polygon: [
[2, 4], [6, 2], [10, 3], [12, 7],
[9, 11], [4, 10], [1, 7]
]
},
{
name: "Cedar Hollow Municipality",
short: "Cedar Hollow",
color: "#8b9cff",
polygon: [
[10, 3], [17, 1], [22, 4], [24, 9],
[20, 12], [14, 11], [12, 7]
]
},
{
name: "South Quay Municipality",
short: "South Quay",
color: "#d884e8",
polygon: [
[4, 10], [9, 11], [14, 11], [20, 12],
[25, 16], [23, 20], [3, 20], [1, 15]
]
}
];
const watershedDefs = [
{
name: "Silver Creek Watershed",
short: "Silver Creek",
color: "#ffb95d",
polygon: [
[1, 2], [8, 1], [13, 3], [15, 8],
[11, 12], [5, 11], [1, 7]
]
},
{
name: "Cedar Basin Watershed",
short: "Cedar Basin",
color: "#ff7b66",
polygon: [
[13, 3], [20, 2], [25, 5], [26, 10],
[21, 14], [13, 12], [15, 8]
]
},
{
name: "Moss Delta Watershed",
short: "Moss Delta",
color: "#9bdc65",
polygon: [
[1, 7], [5, 11], [11, 12], [13, 12],
[21, 14], [26, 16], [26, 21], [1, 21]
]
}
];
const districts = {
alder: {
name: "Alder District",
color: "#82a98a"
},
crown: {
name: "Crown District",
color: "#a2ad79"
},
ferry: {
name: "Ferry District",
color: "#9db98b"
},
lowbank: {
name: "Lowbank District",
color: "#739b87"
}
};
const state = {
system: "municipality",
selected: {
municipality: 0,
watershed: 0
},
hover: null,
pinned: null
};
function pointInPolygon(x, z, polygon) {
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const xi = polygon[i][0];
const zi = polygon[i][1];
const xj = polygon[j][0];
const zj = polygon[j][1];
const intersects =
zi > z !== zj > z &&
x < ((xj - xi) * (z - zi)) / (zj - zi) + xi;
if (intersects) inside = !inside;
}
return inside;
}
function assignedRegion(definitions, x, z) {
const px = x + 0.5;
const pz = z + 0.5;
for (let i = 0; i < definitions.length; i++) {
if (pointInPolygon(px, pz, definitions[i].polygon)) return i;
}
return -1;
}
function hash2(x, z) {
let n = (x * 374761393 + z * 668265263) | 0;
n = (n ^ (n >>> 13)) * 1274126177;
return ((n ^ (n >>> 16)) >>> 0) / 4294967295;
}
function gridToWorld(value, total) {
return (value - (total - 1) / 2) * SPACING;
}
function riverAt(x, z) {
if (z < 7) return false;
const center = 7 + 5 * Math.sin((z - 6) * 0.38);
return Math.abs(x - center) < 0.85;
}
function districtAt(x, z) {
if (z < 9) {
return x < 12 ? "alder" : "crown";
}
return x < 10 ? "ferry" : "lowbank";
}
function countyAt(z) {
return z < 9 ? "Northmere County" : "Southmere County";
}
const cells = [];
for (let z = 0; z < ROWS; z++) {
for (let x = 0; x < COLS; x++) {
const district = districtAt(x, z);
const random = hash2(x, z);
const river = riverAt(x, z);
let height =
0.78 +
random * 1.55 +
Math.sin(x * 0.31 + z * 0.18) * 0.18 +
Math.max(0, z - 10) * 0.025;
if (river) height = 0.24;
cells.push({
x,
z,
index: cells.length,
worldX: gridToWorld(x, COLS),
worldZ: gridToWorld(z, ROWS),
height,
district,
districtName: districts[district].name,
county: countyAt(z),
province: "Northvale Province",
municipality: assignedRegion(municipalityDefs, x, z),
watershed: assignedRegion(watershedDefs, x, z),
river,
baseColor: new THREE.Color(
river ? "#4f91a0" : districts[district].color
)
});
}
}
municipalityDefs.forEach((region) => {
region.cells = cells.filter((cell) => cell.municipality === region.index ?? false);
});
// Assign masks after definitions receive an index.
municipalityDefs.forEach((region, index) => {
region.index = index;
region.cells = cells.filter((cell) => cell.municipality === index);
region.mask = new Set(region.cells.map((cell) => cell.index));
});
watershedDefs.forEach((region, index) => {
region.index = index;
region.cells = cells.filter((cell) => cell.watershed === index);
region.mask = new Set(region.cells.map((cell) => cell.index));
});
const landmarkCell =
cells.find(
(cell) => cell.municipality === 0 && cell.watershed === 0
) ||
cells.find(
(cell) => cell.municipality >= 0 && cell.watershed >= 0
);
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e7eee8");
scene.fog = new THREE.Fog("#e7eee8", 58, 92);
const camera = new THREE.OrthographicCamera(-20, 20, 15, -15, 0.1, 150);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel map of fictional municipalities and watersheds"
);
renderer.domElement.tabIndex = 0;
document.querySelector("#scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight("#f6fff7", "#71887c", 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff4d7", 3.1);
sun.position.set(-18, 30, 16);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -26;
sun.shadow.camera.right = 26;
sun.shadow.camera.top = 26;
sun.shadow.camera.bottom = -26;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight("#cce6ed", 1.2);
fill.position.set(22, 12, -18);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(130, 130),
new THREE.MeshStandardMaterial({ color: "#d9e3da", roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.2;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(COLS + 1.1, 0.48, ROWS + 1.1),
new THREE.MeshStandardMaterial({
color: "#b6c5b5",
roughness: 0.9
})
);
platform.position.y = -0.94;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const terrain = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: "#ffffff",
roughness: 0.94,
metalness: 0
}),
cells.length
);
terrain.castShadow = true;
terrain.receiveShadow = true;
scene.add(terrain);
const dummy = new THREE.Object3D();
for (const cell of cells) {
dummy.position.set(cell.worldX, (cell.height - 0.66) / 2, cell.worldZ);
dummy.scale.set(0.985, cell.height + 0.66, 0.985);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
terrain.setMatrixAt(cell.index, dummy.matrix);
}
terrain.instanceMatrix.needsUpdate = true;
// River surface.
const riverMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 0.035, 1),
new THREE.MeshStandardMaterial({
color: "#68b6c1",
roughness: 0.24,
metalness: 0.04
}),
cells.length
);
for (const cell of cells) {
dummy.position.set(cell.worldX, cell.height + 0.027, cell.worldZ);
dummy.scale.set(0.98, 1, 0.98);
dummy.updateMatrix();
riverMesh.setMatrixAt(cell.index, dummy.matrix);
}
riverMesh.instanceMatrix.needsUpdate = true;
scene.add(riverMesh);
// Municipality footprint: square tiles.
const municipalMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 0.055, 1),
new THREE.MeshStandardMaterial({
color: "#ffffff",
transparent: true,
opacity: 0.82,
roughness: 0.72,
depthWrite: false
}),
cells.length
);
municipalMesh.renderOrder = 2;
scene.add(municipalMesh);
// Watershed footprint: smaller diamond tiles.
const watershedMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 0.055, 1),
new THREE.MeshStandardMaterial({
color: "#ffffff",
transparent: true,
opacity: 0.88,
roughness: 0.65,
depthWrite: false
}),
cells.length
);
watershedMesh.renderOrder = 3;
scene.add(watershedMesh);
// Region boundaries represented by small voxel posts.
const boundaryRecords = [];
function addRegionEdges(region, system) {
const neighbors = [
[-1, 0], [1, 0], [0, -1], [0, 1]
];
for (const cell of region.cells) {
for (const [dx, dz] of neighbors) {
const neighborIndex =
cell.x + dx + (cell.z + dz) * COLS;
if (
neighborIndex < 0 ||
neighborIndex >= cells.length ||
!region.mask.has(neighborIndex)
) {
boundaryRecords.push({
system,
region: region.index,
x: cell.worldX + dx * 0.47,
z: cell.worldZ + dz * 0.47,
y: cell.height + 0.16
});
}
}
}
}
municipalityDefs.forEach((region) => addRegionEdges(region, "municipality"));
watershedDefs.forEach((region) => addRegionEdges(region, "watershed"));
const regionBoundaryMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.13, 0.18, 0.13),
new THREE.MeshStandardMaterial({
color: "#ffffff",
roughness: 0.7,
transparent: true,
opacity: 0.95
}),
boundaryRecords.length
);
regionBoundaryMesh.renderOrder = 4;
boundaryRecords.forEach((record, index) => {
dummy.position.set(record.x, record.y, record.z);
dummy.scale.set(1, 1, 1);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
regionBoundaryMesh.setMatrixAt(index, dummy.matrix);
});
regionBoundaryMesh.instanceMatrix.needsUpdate = true;
scene.add(regionBoundaryMesh);
// Administrative boundary: county and district divisions.
const adminSegments = [];
for (let x = 0; x < COLS; x++) {
const a = cells[8 * COLS + x];
const b = cells[9 * COLS + x];
adminSegments.push({
x: a.worldX,
z: (a.worldZ + b.worldZ) / 2,
y: Math.max(a.height, b.height) + 0.035,
sx: 0.91,
sy: 0.07,
sz: 0.1
});
}
for (let z = 0; z < 9; z++) {
const a = cells[z * COLS + 11];
const b = cells[z * COLS + 12];
adminSegments.push({
x: (a.worldX + b.worldX) / 2,
z: a.worldZ,
y: Math.max(a.height, b.height) + 0.035,
sx: 0.1,
sy: 0.07,
sz: 0.91
});
}
for (let z = 9; z < ROWS; z++) {
const a = cells[z * COLS + 9];
const b = cells[z * COLS + 10];
adminSegments.push({
x: (a.worldX + b.worldX) / 2,
z: a.worldZ,
y: Math.max(a.height, b.height) + 0.035,
sx: 0.1,
sy: 0.07,
sz: 0.91
});
}
const adminBoundaryMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: "#4f675b",
transparent: true,
opacity: 0.47,
roughness: 0.9
}),
adminSegments.length
);
adminSegments.forEach((segment, index) => {
dummy.position.set(segment.x, segment.y, segment.z);
dummy.scale.set(segment.sx, segment.sy, segment.sz);
dummy.updateMatrix();
adminBoundaryMesh.setMatrixAt(index, dummy.matrix);
});
adminBoundaryMesh.instanceMatrix.needsUpdate = true;
scene.add(adminBoundaryMesh);
// Outer province frame.
const frameSegments = [];
for (let x = 0; x < COLS; x++) {
frameSegments.push({
x: gridToWorld(x, COLS),
z: gridToWorld(0, ROWS) - 0.6,
sx: 0.94,
sz: 0.1
});
frameSegments.push({
x: gridToWorld(x, COLS),
z: gridToWorld(ROWS - 1, ROWS) + 0.6,
sx: 0.94,
sz: 0.1
});
}
for (let z = 0; z < ROWS; z++) {
frameSegments.push({
x: gridToWorld(0, COLS) - 0.6,
z: gridToWorld(z, ROWS),
sx: 0.1,
sz: 0.94
});
frameSegments.push({
x: gridToWorld(COLS - 1, COLS) + 0.6,
z: gridToWorld(z, ROWS),
sx: 0.1,
sz: 0.94
});
}
const provinceFrame = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 0.15, 1),
new THREE.MeshStandardMaterial({
color: "#71867a",
roughness: 0.88
}),
frameSegments.length
);
frameSegments.forEach((segment, index) => {
dummy.position.set(segment.x, 0.13, segment.z);
dummy.scale.set(segment.sx, 1, segment.sz);
dummy.updateMatrix();
provinceFrame.setMatrixAt(index, dummy.matrix);
});
provinceFrame.instanceMatrix.needsUpdate = true;
scene.add(provinceFrame);
// Cream cubes mark cells with membership in both boundary systems.
const dualMarkerMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.2, 0.15, 0.2),
new THREE.MeshStandardMaterial({
color: "#fff0a6",
emissive: "#7c5b11",
emissiveIntensity: 0.18,
roughness: 0.45
}),
cells.length
);
dualMarkerMesh.renderOrder = 5;
scene.add(dualMarkerMesh);
let dualIndices = [];
// Hover and pin outlines.
function makeOutline(color) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthTest: false
});
const horizontal = new THREE.BoxGeometry(0.35, 0.045, 0.065);
const vertical = new THREE.BoxGeometry(0.065, 0.045, 0.35);
const north = new THREE.Mesh(horizontal, material);
const south = new THREE.Mesh(horizontal, material);
const east = new THREE.Mesh(vertical, material);
const west = new THREE.Mesh(vertical, material);
north.position.z = -0.48;
south.position.z = 0.48;
east.position.x = 0.48;
west.position.x = -0.48;
group.add(north, south, east, west);
group.visible = false;
group.renderOrder = 10;
scene.add(group);
return group;
}
const hoverOutline = makeOutline("#ffffff");
const pinOutline = makeOutline("#fff0a6");
// -------------------------------------------------------------------------
// Voxel trees, buildings, and the shared-place landmark
// -------------------------------------------------------------------------
function addTree(cell, size = 1) {
const trunk = new THREE.Mesh(
new THREE.BoxGeometry(0.16 * size, 0.52 * size, 0.16 * size),
new THREE.MeshStandardMaterial({ color: "#73583f", roughness: 1 })
);
trunk.position.set(
cell.worldX,
cell.height + 0.26 * size,
cell.worldZ
);
trunk.castShadow = true;
scene.add(trunk);
const crownColors = ["#3d745d", "#4b8062", "#5b8c67"];
const crown = new THREE.Mesh(
new THREE.BoxGeometry(0.52 * size, 0.48 * size, 0.52 * size),
new THREE.MeshStandardMaterial({
color: crownColors[Math.floor(hash2(cell.x, cell.z) * 3)],
roughness: 1
})
);
crown.position.set(
cell.worldX,
cell.height + 0.7 * size,
cell.worldZ
);
crown.castShadow = true;
scene.add(crown);
const top = new THREE.Mesh(
new THREE.BoxGeometry(0.34 * size, 0.28 * size, 0.34 * size),
crown.material
);
top.position.set(
cell.worldX,
cell.height + 1.03 * size,
cell.worldZ
);
top.castShadow = true;
scene.add(top);
}
for (const cell of cells) {
const chance = hash2(cell.x + 11, cell.z + 37);
const peripheral =
cell.x < 2 || cell.x > COLS - 3 || cell.z < 2 || cell.z > ROWS - 3;
if (
!cell.river &&
cell !== landmarkCell &&
chance < (peripheral ? 0.13 : 0.055)
) {
addTree(cell, 0.8 + hash2(cell.z, cell.x) * 0.35);
}
}
function addHouse(cell, wallColor = "#e6d7bb") {
const walls = new THREE.Mesh(
new THREE.BoxGeometry(0.5, 0.45, 0.5),
new THREE.MeshStandardMaterial({
color: wallColor,
roughness: 0.85
})
);
walls.position.set(cell.worldX, cell.height + 0.26, cell.worldZ);
walls.castShadow = true;
walls.receiveShadow = true;
scene.add(walls);
const roof = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.17, 0.62),
new THREE.MeshStandardMaterial({
color: "#9c6254",
roughness: 0.88
})
);
roof.position.set(cell.worldX, cell.height + 0.57, cell.worldZ);
roof.castShadow = true;
scene.add(roof);
}
const houseCells = [
cells[6 * COLS + 5],
cells[7 * COLS + 8],
cells[5 * COLS + 16],
cells[7 * COLS + 20],
cells[10 * COLS + 13],
cells[13 * COLS + 15],
cells[16 * COLS + 6],
cells[17 * COLS + 21]
].filter(Boolean);
houseCells.forEach((cell, index) => {
addHouse(cell, index % 2 ? "#d7c8a9" : "#eee0c1");
});
const landmarkBeacon = new THREE.Group();
if (landmarkCell) {
const stem = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.92, 0.12),
new THREE.MeshStandardMaterial({
color: "#a57b1c",
emissive: "#6f4d0d",
emissiveIntensity: 0.15
})
);
stem.position.y = 0.46;
stem.castShadow = true;
const flag = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.27, 0.12),
new THREE.MeshStandardMaterial({
color: "#fff0a6",
emissive: "#8e6713",
emissiveIntensity: 0.25
})
);
flag.position.set(0.12, 0.84, 0);
flag.castShadow = true;
landmarkBeacon.add(stem, flag);
landmarkBeacon.position.set(
landmarkCell.worldX,
landmarkCell.height + 0.12,
landmarkCell.worldZ
);
scene.add(landmarkBeacon);
}
// -------------------------------------------------------------------------
// HTML map labels
// -------------------------------------------------------------------------
const labelElements = [];
function regionLabelPosition(region) {
if (!region.cells.length) {
return new THREE.Vector3(0, 1, 0);
}
let x = 0;
let z = 0;
let y = 0;
for (const cell of region.cells) {
x += cell.worldX;
z += cell.worldZ;
y += cell.height;
}
return new THREE.Vector3(
x / region.cells.length,
y / region.cells.length + 0.8,
z / region.cells.length
);
}
function createRegionLabels(region, system) {
const element = document.createElement("div");
element.className = `map-label ${system}`;
element.style.setProperty("--label-color", region.color);
element.innerHTML =
`<span class="label-glyph"></span><span>${region.short}</span>`;
document.querySelector("#map-labels").appendChild(element);
labelElements.push({
element,
system,
region,
position: regionLabelPosition(region)
});
}
municipalityDefs.forEach((region) => createRegionLabels(region, "municipality"));
watershedDefs.forEach((region) => createRegionLabels(region, "watershed"));
let landmarkLabel = null;
if (landmarkCell) {
landmarkLabel = document.createElement("div");
landmarkLabel.className = "map-label landmark";
landmarkLabel.innerHTML = "<span>◆ +</span><span>Confluence Market</span>";
document.querySelector("#map-labels").appendChild(landmarkLabel);
labelElements.push({
element: landmarkLabel,
system: "landmark",
region: null,
position: new THREE.Vector3(
landmarkCell.worldX,
landmarkCell.height + 1.55,
landmarkCell.worldZ
)
});
}
// -------------------------------------------------------------------------
// Selection and color updates
// -------------------------------------------------------------------------
const shadowColor = new THREE.Color("#60766c");
function selectedDefinitions() {
return state.system === "municipality"
? municipalityDefs
: watershedDefs;
}
function oppositeProperty() {
return state.system === "municipality" ? "watershed" : "municipality";
}
function currentSelectedDefinition() {
return selectedDefinitions()[state.selected[state.system]];
}
function updateSceneColors() {
const selected = currentSelectedDefinition();
const selectedColor = new THREE.Color(selected.color);
const selectedMask = selected.mask;
// Terrain tint.
cells.forEach((cell) => {
const color = cell.baseColor.clone();
if (selectedMask.has(cell.index)) {
color.lerp(selectedColor, 0.43);
color.multiplyScalar(1.05);
} else {
color.lerp(shadowColor, 0.48);
color.multiplyScalar(0.82);
}
terrain.setColorAt(cell.index, color);
});
terrain.instanceColor.needsUpdate = true;
// Municipality squares.
cells.forEach((cell) => {
if (cell.municipality < 0) {
dummy.position.set(cell.worldX, cell.height + 0.08, cell.worldZ);
dummy.scale.set(0, 0, 0);
dummy.rotation.set(0, 0, 0);
} else {
const definition = municipalityDefs[cell.municipality];
const color = new THREE.Color(definition.color);
let scale = 0.8;
if (state.system === "municipality") {
if (cell.municipality === state.selected.municipality) {
scale = 0.91;
} else {
color.multiplyScalar(0.38);
}
} else {
const oppositeRegion = watershedDefs[state.selected.watershed];
if (cell.watershed === state.selected.watershed) {
color.multiplyScalar(0.5);
} else {
color.multiplyScalar(0.28);
}
if (oppositeRegion.mask.has(cell.index)) scale = 0.84;
}
dummy.position.set(cell.worldX, cell.height + 0.08, cell.worldZ);
dummy.scale.set(scale, 1, scale);
dummy.rotation.set(0, 0, 0);
municipalMesh.setColorAt(cell.index, color);
}
dummy.updateMatrix();
municipalMesh.setMatrixAt(cell.index, dummy.matrix);
});
municipalMesh.instanceMatrix.needsUpdate = true;
municipalMesh.instanceColor.needsUpdate = true;
// Watershed diamonds.
cells.forEach((cell) => {
if (cell.watershed < 0) {
dummy.position.set(cell.worldX, cell.height + 0.13, cell.worldZ);
dummy.scale.set(0, 0, 0);
dummy.rotation.set(0, 0, 0);
} else {
const definition = watershedDefs[cell.watershed];
const color = new THREE.Color(definition.color);
let scale = 0.44;
if (state.system === "watershed") {
if (cell.watershed === state.selected.watershed) {
scale = 0.83;
} else {
color.multiplyScalar(0.42);
scale = 0.5;
}
} else {
if (cell.municipality === state.selected.municipality) {
scale = 0.64;
} else {
color.multiplyScalar(0.37);
scale = 0.4;
}
}
dummy.position.set(cell.worldX, cell.height + 0.13, cell.worldZ);
dummy.scale.set(scale, 1, scale);
dummy.rotation.set(0, Math.PI / 4, 0);
watershedMesh.setColorAt(cell.index, color);
}
dummy.updateMatrix();
watershedMesh.setMatrixAt(cell.index, dummy.matrix);
});
watershedMesh.instanceMatrix.needsUpdate = true;
watershedMesh.instanceColor.needsUpdate = true;
// Region edge colors.
boundaryRecords.forEach((record, index) => {
const definitions =
record.system === "municipality"
? municipalityDefs
: watershedDefs;
const color = new THREE.Color(definitions[record.region].color);
if (record.system === state.system) {
if (record.region === state.selected[state.system]) {
color.multiplyScalar(1.12);
} else {
color.multiplyScalar(0.33);
}
} else {
color.multiplyScalar(0.37);
}
regionBoundaryMesh.setColorAt(index, color);
});
regionBoundaryMesh.instanceColor.needsUpdate = true;
// Cross-system markers.
const opposite = oppositeProperty();
dualIndices = selected.cells
.filter((cell) => cell[opposite] >= 0)
.map((cell) => cell.index);
dualMarkerMesh.count = dualIndices.length;
updateLabelStyles();
updatePinOutline();
}
function updateLabelStyles() {
for (const label of labelElements) {
if (label.system === "landmark") {
label.element.style.opacity = "0.95";
continue;
}
const selected =
state.system === label.system &&
state.selected[label.system] === label.region.index;
label.element.classList.toggle("selected", selected);
if (selected) {
label.element.style.opacity = "1";
} else if (label.system === state.system) {
label.element.style.opacity = "0.38";
} else {
label.element.style.opacity = "0.2";
}
}
}
function updatePinOutline() {
const cell = state.pinned !== null ? cells[state.pinned] : null;
if (!cell) {
pinOutline.visible = false;
return;
}
const color =
state.system === "municipality"
? municipalityDefs[state.selected.municipality].color
: watershedDefs[state.selected.watershed].color;
pinOutline.children.forEach((mesh) => {
mesh.material.color.set(color);
});
pinOutline.position.set(
cell.worldX,
cell.height + 0.22,
cell.worldZ
);
pinOutline.visible = true;
}
function renderRegionList() {
const list = document.querySelector("#region-list");
list.innerHTML = "";
selectedDefinitions().forEach((region) => {
const button = document.createElement("button");
button.type = "button";
button.className = "region-button";
button.classList.toggle(
"active",
state.selected[state.system] === region.index
);
button.innerHTML = `
<span class="swatch ${
state.system === "watershed" ? "watershed" : ""
}" style="--swatch:${region.color}"></span>
<span>
<span class="region-name">${region.name}</span>
<span class="region-meta">${
state.system === "municipality"
? "Municipal government region"
: "Drainage and catchment region"
}</span>
</span>
<span class="cell-count">${region.cells.length}</span>
`;
button.addEventListener("click", () => {
state.selected[state.system] = region.index;
updateAll();
});
list.appendChild(button);
});
}
function representativeCell(region) {
if (!region.cells.length) return cells[0];
let x = 0;
let z = 0;
for (const cell of region.cells) {
x += cell.worldX;
z += cell.worldZ;
}
x /= region.cells.length;
z /= region.cells.length;
return region.cells.reduce((nearest, cell) => {
const distance = Math.hypot(cell.worldX - x, cell.worldZ - z);
const nearestDistance = Math.hypot(
nearest.worldX - x,
nearest.worldZ - z
);
return distance < nearestDistance ? cell : nearest;
}, region.cells[0]);
}
function updateStatus() {
const region = currentSelectedDefinition();
const opposite = oppositeProperty();
const oppositeDefinitions =
opposite === "watershed" ? watershedDefs : municipalityDefs;
const overlapCells = region.cells.filter(
(cell) => cell[opposite] >= 0
);
const names = [
...new Set(
overlapCells.map((cell) => oppositeDefinitions[cell[opposite]].short)
)
];
document.querySelector("#selected-region-name").textContent =
region.name;
document.querySelector("#overlap-count").textContent =
overlapCells.length;
document.querySelector("#status-copy").textContent = names.length
? `${overlapCells.length} selected cells also belong to ${
names.join(" and ")
}. Cream markers identify these shared places.`
: "This region has no cells in the other boundary system.";
const hierarchyCell =
state.pinned !== null &&
region.mask.has(state.pinned)
? cells[state.pinned]
: representativeCell(region);
document.querySelector("#hierarchy-province").textContent =
hierarchyCell.province;
document.querySelector("#hierarchy-county").textContent =
hierarchyCell.county;
document.querySelector("#hierarchy-district").textContent =
hierarchyCell.districtName;
}
function updateAll() {
updateSceneColors();
renderRegionList();
updateStatus();
}
document.querySelector("#system-municipality").addEventListener(
"click",
() => {
state.system = "municipality";
document
.querySelector("#system-municipality")
.classList.add("active");
document
.querySelector("#system-watershed")
.classList.remove("active");
updateAll();
}
);
document.querySelector("#system-watershed").addEventListener(
"click",
() => {
state.system = "watershed";
document
.querySelector("#system-watershed")
.classList.add("active");
document
.querySelector("#system-municipality")
.classList.remove("active");
updateAll();
}
);
if (landmarkCell) {
const municipalityName =
municipalityDefs[landmarkCell.municipality].name;
const watershedName =
watershedDefs[landmarkCell.watershed].name;
document.querySelector("#landmark-copy").textContent =
`${municipalityName} + ${watershedName}. One civic home, one drainage basin.`;
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.3, 0);
const initialTarget = cameraTarget.clone();
let azimuth = 0.68;
let polar = 0.91;
const radius = 48;
function updateCameraTransform() {
const sinPolar = Math.sin(polar);
camera.position.set(
cameraTarget.x + radius * sinPolar * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * sinPolar * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function updateProjection() {
const aspect = window.innerWidth / window.innerHeight;
camera.left = (-VIEW_HEIGHT * aspect) / 2;
camera.right = (VIEW_HEIGHT * aspect) / 2;
camera.top = VIEW_HEIGHT / 2;
camera.bottom = -VIEW_HEIGHT / 2;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraTarget.copy(initialTarget);
azimuth = 0.68;
polar = 0.91;
camera.zoom = 1;
updateCameraTransform();
updateProjection();
}
document.querySelector("#reset-view").addEventListener(
"click",
resetCamera
);
let drag = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
drag = {
id: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: 0,
mode:
event.shiftKey || event.button === 1 || event.button === 2
? "pan"
: "orbit"
};
document.querySelector("#tooltip").style.display = "none";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (drag && drag.id === event.pointerId) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
drag.moved += Math.hypot(dx, dy);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
if (drag.mode === "orbit") {
azimuth -= dx * 0.006;
polar = THREE.MathUtils.clamp(polar + dy * 0.005, 0.42, 1.28);
updateCameraTransform();
} else {
const scale =
VIEW_HEIGHT / renderer.domElement.clientHeight / camera.zoom;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 2);
forward.y = 0;
forward.normalize();
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(forward, -dy * scale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -12, 12);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -12, 12);
updateCameraTransform();
}
return;
}
const cell = pickCell(event.clientX, event.clientY);
state.hover = cell ? cell.index : null;
updateHover(cell, event.clientX, event.clientY);
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!drag || drag.id !== event.pointerId) return;
if (drag.moved < 6 && event.button === 0) {
const cell = pickCell(event.clientX, event.clientY);
if (cell) {
state.pinned = cell.index;
const membership =
state.system === "municipality"
? cell.municipality
: cell.watershed;
if (membership >= 0) {
state.selected[state.system] = membership;
}
updateAll();
}
}
drag = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
drag = null;
});
renderer.domElement.addEventListener("pointerleave", () => {
state.hover = null;
hoverOutline.visible = false;
document.querySelector("#tooltip").style.display = "none";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.4
);
updateProjection();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function pickCell(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
const pointer = new THREE.Vector2(
((clientX - rect.left) / rect.width) * 2 - 1,
-((clientY - rect.top) / rect.height) * 2 + 1
);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(terrain, false);
if (!hits.length || hits[0].instanceId === undefined) return null;
return cells[hits[0].instanceId];
}
const raycaster = new THREE.Raycaster();
function updateHover(cell, clientX, clientY) {
const tooltip = document.querySelector("#tooltip");
if (!cell) {
hoverOutline.visible = false;
tooltip.style.display = "none";
return;
}
hoverOutline.position.set(
cell.worldX,
cell.height + 0.2,
cell.worldZ
);
hoverOutline.visible = true;
const municipality =
cell.municipality >= 0
? municipalityDefs[cell.municipality].name
: "Unincorporated uplands";
const watershed =
cell.watershed >= 0
? watershedDefs[cell.watershed].name
: "Outside mapped basin";
const shared =
cell.municipality >= 0 && cell.watershed >= 0;
tooltip.innerHTML = `
<div class="tip-kicker">Grid ${String(cell.x + 1).padStart(2, "0")}.${String(
cell.z + 1
).padStart(2, "0")}</div>
<div class="tip-row"><span>Municipality</span><b>${municipality}</b></div>
<div class="tip-row"><span>Watershed</span><b>${watershed}</b></div>
<div class="tip-row"><span>District</span><b>${cell.districtName}</b></div>
<div class="tip-row"><span>County</span><b>${cell.county}</b></div>
<div class="tip-row"><span>Province</span><b>${cell.province}</b></div>
${
shared
? '<div class="tip-shared">+ Shared civic and watershed membership</div>'
: ""
}
`;
tooltip.style.display = "block";
const width = 230;
const height = tooltip.offsetHeight || 170;
const left = Math.min(clientX + 16, window.innerWidth - width - 10);
const top = Math.min(clientY + 16, window.innerHeight - height - 10);
tooltip.style.left = `${Math.max(8, left)}px`;
tooltip.style.top = `${Math.max(8, top)}px`;
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateProjection();
});
function positionLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labelElements) {
const projected = label.position.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible =
projected.z > -1 &&
projected.z < 1 &&
x > -50 &&
x < width + 50 &&
y > -30 &&
y < height + 30;
label.element.style.display = visible ? "flex" : "none";
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
function animate(time) {
requestAnimationFrame(animate);
const pulse = 1 + Math.sin(time * 0.0045) * 0.12;
dualIndices.forEach((index, instanceIndex) => {
const cell = cells[index];
dummy.position.set(
cell.worldX,
cell.height + 0.25 + Math.sin(time * 0.0045 + index) * 0.025,
cell.worldZ
);
dummy.scale.set(pulse, pulse, pulse);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
dualMarkerMesh.setMatrixAt(instanceIndex, dummy.matrix);
});
dualMarkerMesh.instanceMatrix.needsUpdate = true;
if (landmarkBeacon) {
landmarkBeacon.scale.setScalar(1 + Math.sin(time * 0.0035) * 0.035);
}
positionLabels();
renderer.render(scene, camera);
}
resetCamera();
updateAll();
requestAnimationFrame(animate);
</script>
</body>
</html>
```