Prompt specification
Build or customize the Three.js 3D scene "Small pond inside a grid cell" 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: Small pond inside a grid cell
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-pond-inside-a-grid-cell-x1301
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring spatial map resolution. Place a small pond within a larger terrain tile beside its coarse grid representation. Show how one cell cannot preserve the pond's detailed boundary shape. Voxel style.
Working Three.js Starter (small-pond-inside-a-grid-cell-x1301):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Spatial Map Resolution — Voxel Study</title>
<style>
:root {
color-scheme: dark;
--bg: #07111b;
--panel: rgba(10, 24, 35, 0.88);
--panel-border: rgba(126, 196, 216, 0.22);
--text: #e6f2f5;
--muted: #91aeb8;
--water: #29a6d1;
--water-light: #61d3ed;
--land: #527c52;
--land-light: #739563;
--shore: #a18452;
--accent: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas.webgl {
position: fixed;
inset: 0;
display: block;
z-index: 0;
cursor: grab;
touch-action: none;
}
canvas.webgl:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--panel-border);
border-radius: 14px;
background: var(--panel);
box-shadow:
0 16px 45px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-panel {
top: 18px;
left: 18px;
width: min(360px, calc(100vw - 36px));
padding: 17px 18px 15px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--water-light);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.3vw, 31px);
line-height: 1.06;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 13px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 9px 14px;
color: #b8cbd1;
font-size: 10px;
letter-spacing: 0.03em;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.14);
}
.swatch.water {
background: var(--water);
}
.swatch.land {
background: var(--land);
}
.swatch.shore {
background: var(--shore);
}
.swatch.selection {
background: transparent;
border: 2px solid var(--accent);
}
#resolution-panel {
top: 18px;
right: 18px;
width: 245px;
padding: 14px;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
color: #c5d8de;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-title span:last-child {
color: var(--accent);
}
.resolution-buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 5px;
}
.resolution-buttons button {
min-width: 0;
padding: 8px 4px;
border: 1px solid rgba(126, 196, 216, 0.18);
border-radius: 7px;
background: rgba(255, 255, 255, 0.035);
color: #a9c0c8;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
color 140ms ease,
transform 140ms ease;
}
.resolution-buttons button:hover {
border-color: rgba(97, 211, 237, 0.48);
color: white;
transform: translateY(-1px);
}
.resolution-buttons button.active {
border-color: var(--water-light);
background: rgba(41, 166, 209, 0.2);
color: #d9f8ff;
}
.control-note {
margin: 10px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
#inspector {
left: 50%;
bottom: 20px;
width: min(470px, calc(100vw - 36px));
padding: 14px 16px 15px;
transform: translateX(-50%);
}
.inspector-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.inspector-head strong {
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#cell-coordinate {
color: var(--accent);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.comparison {
display: grid;
grid-template-columns: minmax(110px, 1fr) 32px minmax(110px, 0.82fr);
align-items: center;
gap: 10px;
}
.comparison-label {
margin-bottom: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#source-grid {
display: grid;
width: 100%;
max-width: 190px;
aspect-ratio: 1;
gap: 2px;
padding: 3px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 6px;
background: rgba(0, 0, 0, 0.22);
}
.source-cell {
min-width: 0;
min-height: 0;
border-radius: 1px;
background: var(--land);
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.055);
}
.source-cell.water {
background: var(--water);
box-shadow:
inset 0 0 0 1px rgba(255, 255, 255, 0.12),
inset 0 5px 10px rgba(97, 211, 237, 0.15);
}
.source-cell.shore {
background: var(--shore);
}
.arrow {
color: var(--accent);
font-size: 25px;
text-align: center;
}
.result-wrap {
display: flex;
flex-direction: column;
align-items: center;
}
#coarse-result {
width: min(92px, 100%);
aspect-ratio: 1;
border-radius: 5px;
background: var(--land);
box-shadow:
inset 0 0 0 1px rgba(255, 255, 255, 0.12),
0 8px 20px rgba(0, 0, 0, 0.22);
}
#coarse-result.water {
background:
linear-gradient(135deg, rgba(255, 255, 255, 0.11), transparent 45%),
var(--water);
}
#coarse-class {
margin-top: 7px;
color: var(--water-light);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
}
#inspect-text {
min-height: 32px;
margin: 11px 0 0;
color: #aec3ca;
font-size: 11px;
line-height: 1.48;
}
#hint {
position: fixed;
left: 20px;
bottom: 20px;
z-index: 4;
max-width: 240px;
color: rgba(199, 220, 227, 0.65);
font-size: 10px;
line-height: 1.55;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 3;
top: 0;
left: 0;
padding: 7px 10px;
border: 1px solid rgba(129, 198, 216, 0.2);
border-radius: 8px;
background: rgba(6, 17, 26, 0.72);
color: #cce5eb;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.scene-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 600;
letter-spacing: 0.06em;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #07111b;
color: #9fc4cf;
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
@media (max-width: 760px) {
#title-panel {
width: 245px;
padding: 13px 14px;
}
.subtitle,
.legend {
display: none;
}
#resolution-panel {
width: 210px;
}
#hint {
display: none;
}
#inspector {
bottom: 10px;
padding: 11px 13px 12px;
}
.comparison {
grid-template-columns: minmax(95px, 1fr) 24px minmax(95px, 0.85fr);
gap: 7px;
}
#source-grid {
max-width: 145px;
}
}
@media (max-width: 540px) {
#title-panel {
top: 10px;
left: 10px;
width: 175px;
}
#resolution-panel {
top: 10px;
right: 10px;
width: 175px;
padding: 10px;
}
.control-note {
display: none;
}
.scene-label {
font-size: 8px;
}
#inspector {
width: calc(100vw - 20px);
}
}
</style>
</head>
<body>
<div id="loading">Loading voxel terrain…</div>
<section id="title-panel" class="panel">
<div class="eyebrow">Spatial data / resolution study</div>
<h1>One cell cannot hold<br />an irregular shoreline.</h1>
<p class="subtitle">
The detailed terrain preserves a stepped pond boundary. The coarse raster
replaces that boundary with one square classification per cell.
</p>
<div class="legend">
<span><i class="swatch water"></i> Water</span>
<span><i class="swatch land"></i> Land</span>
<span><i class="swatch shore"></i> Shore</span>
<span><i class="swatch selection"></i> Selected footprint</span>
</div>
</section>
<section id="resolution-panel" class="panel">
<div class="control-title">
<span>Map resolution</span>
<span id="resolution-value">8 × 8</span>
</div>
<div class="resolution-buttons">
<button type="button" data-resolution="4">4×4</button>
<button type="button" data-resolution="6">6×6</button>
<button type="button" data-resolution="8" class="active">8×8</button>
<button type="button" data-resolution="12">12×12</button>
</div>
<p class="control-note">
Click a coarse cell. Its exact source footprint is outlined on the detailed tile.
</p>
</section>
<div id="detail-label" class="scene-label">
DETAILED TERRAIN
<span>24 × 24 source voxels</span>
</div>
<div id="coarse-label" class="scene-label">
COARSE MAP
<span id="coarse-label-value">8 × 8 map cells</span>
</div>
<section id="inspector" class="panel">
<div class="inspector-head">
<strong>Cell inspector</strong>
<span id="cell-coordinate">CELL —</span>
</div>
<div class="comparison">
<div>
<div class="comparison-label" id="source-label">3 × 3 source voxels</div>
<div id="source-grid"></div>
</div>
<div class="arrow">→</div>
<div>
<div class="comparison-label">One map cell</div>
<div class="result-wrap">
<div id="coarse-result"></div>
<div id="coarse-class">WATER</div>
</div>
</div>
</div>
<p id="inspect-text"></p>
</section>
<div id="hint">
Drag to orbit · Scroll to zoom · Click a coarse cell<br />
Double-click to reset the camera.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const DETAIL_RES = 24;
const TILE_WIDTH = 7.2;
const DETAIL_X = -4.15;
const COARSE_X = 4.15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111b);
scene.fog = new THREE.Fog(0x07111b, 28, 52);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.domElement.className = "webgl";
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;
document.body.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.1, 0);
const controls = {
yaw: -0.13,
pitch: 0.72,
targetYaw: -0.13,
targetPitch: 0.72,
zoom: 1,
targetZoom: 1,
radius: 30
};
function updateCamera() {
const horizontal = controls.radius * Math.cos(controls.pitch);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(controls.yaw),
cameraTarget.y + controls.radius * Math.sin(controls.pitch),
cameraTarget.z + horizontal * Math.cos(controls.yaw)
);
camera.lookAt(cameraTarget);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(11.2, 17.8 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0x8bc9dc, 0x172118, 1.35));
const sun = new THREE.DirectionalLight(0xfff1d5, 3.2);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0006;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x39bde2, 1.6);
rim.position.set(10, 5, -8);
scene.add(rim);
// Floor and grid
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 34),
new THREE.MeshStandardMaterial({
color: 0x08131c,
roughness: 0.94,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.44;
floor.receiveShadow = true;
scene.add(floor);
const groundGrid = new THREE.GridHelper(
32,
32,
0x245164,
0x143341
);
groundGrid.position.y = -0.425;
groundGrid.material.transparent = true;
groundGrid.material.opacity = 0.32;
scene.add(groundGrid);
// Shared unit geometry
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function makePedestal(x) {
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(TILE_WIDTH + 0.2, 0.4, TILE_WIDTH + 0.2),
new THREE.MeshStandardMaterial({
color: 0x17262d,
roughness: 0.82,
metalness: 0.08
})
);
pedestal.position.set(x, -0.22, 0);
pedestal.castShadow = true;
pedestal.receiveShadow = true;
scene.add(pedestal);
const trim = new THREE.LineSegments(
new THREE.EdgesGeometry(pedestal.geometry),
new THREE.LineBasicMaterial({
color: 0x2a5361,
transparent: true,
opacity: 0.7
})
);
trim.position.copy(pedestal.position);
scene.add(trim);
}
makePedestal(DETAIL_X);
makePedestal(COARSE_X);
// Deterministic value noise
function hash(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return value - Math.floor(value);
}
function cellKey(x, z) {
return `${x},${z}`;
}
// Detailed pond mask. Its irregular boundary is intentionally crossed by
// several coarse cells.
const waterSet = new Set();
for (let z = 0; z < DETAIL_RES; z++) {
for (let x = 0; x < DETAIL_RES; x++) {
const px = (x - 11.7) / 4.7;
const pz = (z - 11.3) / 4.0;
const radius = Math.hypot(px, pz);
const angle = Math.atan2(pz, px);
const edge =
0.76 +
0.11 * Math.sin(angle * 3 + 0.45) +
0.065 * Math.cos(angle * 7 - 0.35);
if (radius < edge) {
waterSet.add(cellKey(x, z));
}
}
}
function isWater(x, z) {
return waterSet.has(cellKey(x, z));
}
function isShore(x, z) {
if (x < 0 || z < 0 || x >= DETAIL_RES || z >= DETAIL_RES) {
return false;
}
if (isWater(x, z)) return false;
return (
isWater(x + 1, z) ||
isWater(x - 1, z) ||
isWater(x, z + 1) ||
isWater(x, z - 1)
);
}
// Build the detailed voxel terrain.
const detailCellSize = TILE_WIDTH / DETAIL_RES;
const landEntries = [];
const waterEntries = [];
for (let z = 0; z < DETAIL_RES; z++) {
for (let x = 0; x < DETAIL_RES; x++) {
const worldX =
DETAIL_X - TILE_WIDTH / 2 + (x + 0.5) * detailCellSize;
const worldZ =
-TILE_WIDTH / 2 + (z + 0.5) * detailCellSize;
if (isWater(x, z)) {
waterEntries.push({
x: worldX,
z: worldZ,
height: 0.14
});
} else {
const shore = isShore(x, z);
const rolling =
0.5 +
0.5 *
Math.sin(x * 0.57) *
Math.cos(z * 0.43 + 0.8);
const height = shore
? 0.27 + hash(x, z) * 0.035
: 0.4 + rolling * 0.22 + hash(x, z) * 0.07;
landEntries.push({
x: worldX,
z: worldZ,
height,
shore,
variation: hash(x, z)
});
}
}
}
const landMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0.01
});
const detailedLand = new THREE.InstancedMesh(
unitBox,
landMaterial,
landEntries.length
);
detailedLand.castShadow = true;
detailedLand.receiveShadow = true;
const dummy = new THREE.Object3D();
const color = new THREE.Color();
landEntries.forEach((entry, index) => {
dummy.position.set(entry.x, entry.height / 2, entry.z);
dummy.scale.set(
detailCellSize * 0.91,
entry.height,
detailCellSize * 0.91
);
dummy.updateMatrix();
detailedLand.setMatrixAt(index, dummy.matrix);
if (entry.shore) {
color.setHSL(
0.105,
0.32,
0.34 + entry.variation * 0.08
);
} else {
color.setHSL(
0.285 + entry.variation * 0.025,
0.25 + entry.variation * 0.1,
0.24 + entry.variation * 0.1
);
}
detailedLand.setColorAt(index, color);
});
detailedLand.instanceMatrix.needsUpdate = true;
detailedLand.instanceColor.needsUpdate = true;
detailedLand.computeBoundingSphere();
scene.add(detailedLand);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x27a6d0,
roughness: 0.28,
metalness: 0.06,
emissive: 0x073849,
emissiveIntensity: 0.22
});
const detailedWater = new THREE.InstancedMesh(
unitBox,
waterMaterial,
waterEntries.length
);
detailedWater.castShadow = true;
detailedWater.receiveShadow = true;
waterEntries.forEach((entry, index) => {
dummy.position.set(entry.x, entry.height / 2, entry.z);
dummy.scale.set(
detailCellSize * 0.94,
entry.height,
detailCellSize * 0.94
);
dummy.updateMatrix();
detailedWater.setMatrixAt(index, dummy.matrix);
});
detailedWater.instanceMatrix.needsUpdate = true;
detailedWater.computeBoundingSphere();
scene.add(detailedWater);
// Coarse map and selections
let coarseResolution = 8;
let coarseMesh = null;
let coarseCells = [];
let selectedCell = null;
const coarseMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86,
metalness: 0.02
});
const coarseSelection = new THREE.LineSegments(
new THREE.EdgesGeometry(unitBox),
new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.96,
depthTest: false
})
);
coarseSelection.renderOrder = 10;
scene.add(coarseSelection);
const detailSelection = new THREE.LineSegments(
new THREE.EdgesGeometry(unitBox),
new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.84,
depthTest: false
})
);
detailSelection.renderOrder = 9;
scene.add(detailSelection);
const detailFill = new THREE.Mesh(
new THREE.PlaneGeometry(1, 1),
new THREE.MeshBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
})
);
detailFill.rotation.x = -Math.PI / 2;
detailFill.renderOrder = 8;
scene.add(detailFill);
const selectionMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.15, 0.15),
new THREE.MeshBasicMaterial({ color: 0xffd166 })
);
selectionMarker.renderOrder = 11;
scene.add(selectionMarker);
function disposeHierarchy(object) {
object.traverse((child) => {
if (child.geometry && child.geometry !== unitBox) {
child.geometry.dispose();
}
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
materials.forEach((material) => material.dispose());
}
});
}
function buildCoarseMap(resolution) {
if (coarseMesh) {
scene.remove(coarseMesh);
disposeHierarchy(coarseMesh);
}
coarseResolution = resolution;
coarseCells = [];
const cellSize = TILE_WIDTH / resolution;
const count = resolution * resolution;
coarseMesh = new THREE.InstancedMesh(
unitBox,
coarseMaterial,
count
);
coarseMesh.castShadow = true;
coarseMesh.receiveShadow = true;
let instance = 0;
for (let cz = 0; cz < resolution; cz++) {
for (let cx = 0; cx < resolution; cx++) {
const startX = Math.floor((cx * DETAIL_RES) / resolution);
const endX = Math.floor(((cx + 1) * DETAIL_RES) / resolution);
const startZ = Math.floor((cz * DETAIL_RES) / resolution);
const endZ = Math.floor(((cz + 1) * DETAIL_RES) / resolution);
let waterCount = 0;
let total = 0;
for (let z = startZ; z < endZ; z++) {
for (let x = startX; x < endX; x++) {
total++;
if (isWater(x, z)) waterCount++;
}
}
// Demonstration rule: any source water makes the entire coarse
// cell WATER. This exposes area inflation and boundary loss.
const classification = waterCount > 0 ? "WATER" : "LAND";
const height = classification === "WATER" ? 0.18 : 0.54;
const localX = -TILE_WIDTH / 2 + (cx + 0.5) * cellSize;
const localZ = -TILE_WIDTH / 2 + (cz + 0.5) * cellSize;
const worldX = COARSE_X + localX;
const worldZ = localZ;
dummy.position.set(worldX, height / 2, worldZ);
dummy.scale.set(
cellSize * 0.92,
height,
cellSize * 0.92
);
dummy.updateMatrix();
coarseMesh.setMatrixAt(instance, dummy.matrix);
if (classification === "WATER") {
color.set(0x27a6d0);
} else {
color.setHSL(
0.29,
0.23,
0.29 + hash(cx, cz) * 0.055
);
}
coarseMesh.setColorAt(instance, color);
coarseCells.push({
cx,
cz,
startX,
endX,
startZ,
endZ,
waterCount,
total,
classification,
height,
size: cellSize,
localX,
localZ,
worldX,
worldZ
});
instance++;
}
}
coarseMesh.instanceMatrix.needsUpdate = true;
coarseMesh.instanceColor.needsUpdate = true;
coarseMesh.computeBoundingSphere();
scene.add(coarseMesh);
document.getElementById("resolution-value").textContent =
`${resolution} × ${resolution}`;
document.getElementById("coarse-label-value").textContent =
`${resolution} × ${resolution} map cells`;
document.querySelectorAll("[data-resolution]").forEach((button) => {
button.classList.toggle(
"active",
Number(button.dataset.resolution) === resolution
);
});
selectDefaultCell();
}
function selectDefaultCell() {
const mixedCells = coarseCells
.map((cell, index) => ({ cell, index }))
.filter(({ cell }) => {
return (
cell.waterCount > 0 &&
cell.waterCount < cell.total
);
});
let chosen = mixedCells[0];
if (mixedCells.length > 1) {
mixedCells.sort((a, b) => {
const scoreA =
Math.abs(a.cell.waterCount / a.cell.total - 0.38) +
Math.abs(a.cell.cx - (coarseResolution - 1) / 2) * 0.015;
const scoreB =
Math.abs(b.cell.waterCount / b.cell.total - 0.38) +
Math.abs(b.cell.cx - (coarseResolution - 1) / 2) * 0.015;
return scoreA - scoreB;
});
chosen = mixedCells[0];
}
if (!chosen) {
chosen = coarseCells
.map((cell, index) => ({ cell, index }))
.find(({ cell }) => cell.waterCount > 0);
}
if (chosen) {
selectCell(chosen.index);
}
}
function selectCell(index) {
const cell = coarseCells[index];
if (!cell) return;
selectedCell = cell;
const coarseBoxHeight = cell.height + 0.15;
coarseSelection.scale.set(
cell.size * 1.025,
coarseBoxHeight,
cell.size * 1.025
);
coarseSelection.position.set(
cell.worldX,
coarseBoxHeight / 2,
cell.worldZ
);
detailSelection.scale.set(cell.size, 1.03, cell.size);
detailSelection.position.set(
DETAIL_X + cell.localX,
0.515,
cell.localZ
);
detailFill.scale.set(cell.size, cell.size, 1);
detailFill.position.set(
DETAIL_X + cell.localX,
1.045,
cell.localZ
);
selectionMarker.position.set(
cell.worldX,
cell.height + 0.21,
cell.worldZ
);
updateInspector(cell);
}
const sourceGrid = document.getElementById("source-grid");
const coarseResult = document.getElementById("coarse-result");
const coarseClass = document.getElementById("coarse-class");
const inspectText = document.getElementById("inspect-text");
const cellCoordinate = document.getElementById("cell-coordinate");
const sourceLabel = document.getElementById("source-label");
function updateInspector(cell) {
const sourceWidth = cell.endX - cell.startX;
const sourceHeight = cell.endZ - cell.startZ;
sourceGrid.style.gridTemplateColumns =
`repeat(${sourceWidth}, 1fr)`;
sourceGrid.replaceChildren();
for (let z = cell.startZ; z < cell.endZ; z++) {
for (let x = cell.startX; x < cell.endX; x++) {
const sourceCell = document.createElement("div");
sourceCell.className = "source-cell";
if (isWater(x, z)) {
sourceCell.classList.add("water");
} else if (isShore(x, z)) {
sourceCell.classList.add("shore");
}
sourceGrid.appendChild(sourceCell);
}
}
sourceLabel.textContent =
`${sourceWidth} × ${sourceHeight} source voxels`;
coarseResult.className =
cell.classification === "WATER" ? "water" : "";
coarseClass.textContent = cell.classification;
coarseClass.style.color =
cell.classification === "WATER"
? "var(--water-light)"
: "#a9c891";
cellCoordinate.textContent =
`CELL ${cell.cx}, ${cell.cz}`;
if (cell.waterCount > 0 && cell.waterCount < cell.total) {
inspectText.textContent =
`Only ${cell.waterCount} of ${cell.total} source voxels are water, ` +
`yet the raster stores the entire cell as WATER. The stepped shoreline ` +
`is replaced by a square, losing both its shape and its exact area.`;
} else if (cell.waterCount === cell.total) {
inspectText.textContent =
`All ${cell.total} source voxels in this footprint are water. ` +
`The coarse cell happens to agree here, but it still stores only one ` +
`value instead of the source boundary geometry.`;
} else {
inspectText.textContent =
`None of the ${cell.total} source voxels in this footprint contain water. ` +
`Select a shoreline cell to see how a mixed footprint is quantized into ` +
`a single map value.`;
}
}
document.querySelectorAll("[data-resolution]").forEach((button) => {
button.addEventListener("click", () => {
buildCoarseMap(Number(button.dataset.resolution));
});
});
// Raycasting for coarse-cell selection
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function getPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function intersectCoarse(event) {
if (!coarseMesh) return null;
getPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(coarseMesh, false);
return hits.length ? hits[0] : null;
}
// Custom orbit and zoom controls: no addons are used.
let pointerDown = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (Math.abs(dx) + Math.abs(dy) > 2) {
pointerMoved = true;
}
controls.targetYaw -= dx * 0.006;
controls.targetPitch = THREE.MathUtils.clamp(
controls.targetPitch + dy * 0.005,
0.34,
1.18
);
previousX = event.clientX;
previousY = event.clientY;
} else {
const hit = intersectCoarse(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
const hit = intersectCoarse(event);
if (hit && Number.isInteger(hit.instanceId)) {
selectCell(hit.instanceId);
}
}
pointerDown = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.targetZoom = THREE.MathUtils.clamp(
controls.targetZoom * Math.exp(-event.deltaY * 0.001),
0.7,
2.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
controls.targetYaw = -0.13;
controls.targetPitch = 0.72;
controls.targetZoom = 1;
});
// Project DOM labels from world positions.
const detailLabel = document.getElementById("detail-label");
const coarseLabel = document.getElementById("coarse-label");
const detailLabelPosition = new THREE.Vector3(
DETAIL_X,
1.25,
-TILE_WIDTH / 2 + 0.25
);
const coarseLabelPosition = new THREE.Vector3(
COARSE_X,
1.15,
-TILE_WIDTH / 2 + 0.25
);
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
buildCoarseMap(8);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
controls.yaw +=
(controls.targetYaw - controls.yaw) * 0.12;
controls.pitch +=
(controls.targetPitch - controls.pitch) * 0.12;
controls.zoom +=
(controls.targetZoom - controls.zoom) * 0.12;
updateCamera();
if (selectedCell) {
selectionMarker.position.y =
selectedCell.height +
0.21 +
Math.sin(elapsed * 3.2) * 0.035;
selectionMarker.rotation.y = elapsed * 0.8;
detailFill.material.opacity =
0.065 + Math.sin(elapsed * 2.4) * 0.018;
}
placeLabel(detailLabel, detailLabelPosition);
placeLabel(coarseLabel, coarseLabelPosition);
renderer.render(scene, camera);
}
animate();
const loading = document.getElementById("loading");
loading.remove();
</script>
</body>
</html>
```