Prompt specification
Build or customize the Three.js 3D scene "Gentle slope beside a cliff" 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: Gentle slope beside a cliff
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/gentle-slope-beside-a-cliff-x1294
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: gentle slope beside a cliff. Place two slopes of equal total height beside matching contour maps. Show wider horizontal contour spacing on the gentle slope and tighter spacing on the steep side.
Working Three.js Starter (gentle-slope-beside-a-cliff-x1294):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Slopes and Contour Maps</title>
<style>
:root {
color-scheme: dark;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071019;
}
body {
touch-action: none;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
}
#ui {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.title-card {
position: absolute;
top: 22px;
left: 24px;
max-width: min(560px, calc(100vw - 48px));
padding: 16px 19px;
border: 1px solid rgba(160, 220, 235, 0.17);
border-radius: 16px;
background: linear-gradient(
135deg,
rgba(10, 23, 34, 0.9),
rgba(8, 18, 28, 0.72)
);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 6px;
color: #67d9c4;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #edfaff;
font-size: clamp(21px, 3vw, 34px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
color: #a4bac7;
font-size: 13px;
line-height: 1.45;
}
.relief-card {
position: absolute;
top: 22px;
right: 24px;
width: 250px;
padding: 16px 17px;
border: 1px solid rgba(160, 220, 235, 0.17);
border-radius: 16px;
background: rgba(9, 20, 30, 0.84);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
.relief-title {
color: #a4bac7;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.relief-value {
margin: 4px 0 12px;
color: #f4c768;
font-size: 28px;
font-weight: 800;
letter-spacing: -0.04em;
}
.relief-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
color: #d8e8ef;
font-size: 12px;
}
.swatch {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: 0 0 12px currentColor;
}
.gentle {
color: #52d6af;
background: #52d6af;
}
.steep {
color: #f3b956;
background: #f3b956;
}
.toolbar {
position: absolute;
right: 24px;
bottom: 24px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
padding: 10px 13px;
border: 1px solid rgba(155, 220, 235, 0.2);
border-radius: 10px;
color: #dff4fb;
background: rgba(9, 22, 32, 0.86);
font: 700 12px/1 ui-sans-serif, system-ui, sans-serif;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover,
button:focus-visible {
border-color: rgba(103, 217, 196, 0.65);
background: rgba(17, 39, 52, 0.95);
outline: none;
transform: translateY(-1px);
}
.help {
position: absolute;
bottom: 25px;
left: 24px;
padding: 10px 13px;
border: 1px solid rgba(155, 220, 235, 0.13);
border-radius: 10px;
color: #90a9b7;
background: rgba(7, 16, 25, 0.68);
font-size: 11px;
backdrop-filter: blur(8px);
}
.help strong {
color: #d8edf4;
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
right: 12px;
max-width: none;
padding: 13px 15px;
}
.relief-card {
top: auto;
right: 12px;
bottom: 68px;
width: 205px;
padding: 12px 13px;
}
.relief-value {
margin-bottom: 8px;
font-size: 22px;
}
.help {
display: none;
}
.toolbar {
right: 12px;
bottom: 14px;
}
}
</style>
</head>
<body>
<div id="ui">
<section class="title-card">
<div class="eyebrow">Topographic comparison</div>
<h1>Gentle slope beside a cliff</h1>
<p class="subtitle">
Both voxel slopes gain the same 3.2 m of elevation. Their matching
contour maps reveal the difference: widely separated contours on the
gentle side and tightly packed contours at the cliff.
</p>
</section>
<aside class="relief-card">
<div class="relief-title">Equal vertical relief</div>
<div class="relief-value">3.2 m</div>
<div class="relief-row">
<span class="swatch gentle"></span>
<span>Gentle slope · long horizontal run</span>
</div>
<div class="relief-row">
<span class="swatch steep"></span>
<span>Cliff · short horizontal run</span>
</div>
</aside>
<div class="help">
<strong>Drag</strong> to orbit ·
<strong>Scroll / pinch</strong> to zoom ·
<strong>Double-click</strong> to reset
</div>
<div class="toolbar">
<button id="reset-view" type="button">Reset view</button>
<button id="top-view" type="button">Top view</button>
</div>
</div>
<noscript>This scene requires JavaScript.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel terrain comparison with matching contour maps"
);
renderer.domElement.tabIndex = 0;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 48, 120);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemisphere = new THREE.HemisphereLight(
0xbceeff,
0x18251e,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(
0xfff2d2,
3.2
);
keyLight.position.set(-9, 18, 12);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -28;
keyLight.shadow.camera.right = 28;
keyLight.shadow.camera.top = 24;
keyLight.shadow.camera.bottom = -24;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(
0x70cfff,
1.15
);
fillLight.position.set(12, 8, -8);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(110, 110),
new THREE.MeshStandardMaterial({
color: 0x0b1720,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
90,
90,
0x284553,
0x152d39
);
grid.position.y = -1.185;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// -------------------------------------------------------------------------
// Shared dimensions
// -------------------------------------------------------------------------
const TERRAIN_WIDTH = 8;
const TERRAIN_DEPTH = 4.8;
const TERRAIN_HEIGHT = 3.2;
const COLUMN_COUNT = 15;
const ROW_COUNT = 10;
const MAP_WIDTH = 9.28;
const MAP_DEPTH = 5.75;
const GENTLE = {
id: "gentle",
title: "GENTLE SLOPE",
mapTitle: "GENTLE · CONTOUR MAP",
subtitle: "wide contour spacing",
x: -5.15,
z: -1.45,
accent: 0x59d8b4,
mapAccent: "#59d8b4",
heightAt(t) {
return TERRAIN_HEIGHT * t;
},
inverseHeight(level) {
return level;
}
};
const STEEP = {
id: "steep",
title: "STEEP CLIFF",
mapTitle: "CLIFF · CONTOUR MAP",
subtitle: "tight contour spacing",
x: 5.15,
z: -1.45,
accent: 0xf2b956,
mapAccent: "#f2b956",
heightAt(t) {
// A narrow transition creates the cliff, followed by a high plateau.
const start = 0.44;
const span = 0.24;
const u = THREE.MathUtils.clamp(
(t - start) / span,
0,
1
);
const smooth = u * u * (3 - 2 * u);
return TERRAIN_HEIGHT * smooth;
},
inverseHeight(level) {
// Inverse of smoothstep: 3u² - 2u³ = level.
const start = 0.44;
const span = 0.24;
const u =
0.5 -
Math.sin(Math.asin(1 - 2 * level) / 3);
return start + u * span;
}
};
// -------------------------------------------------------------------------
// Utility helpers
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, radius) {
const r = Math.min(radius, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabelSprite(
title,
subtitle = "",
accent = "#dff6ff",
width = 512,
height = 144
) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, width, height);
roundedRect(
ctx,
5,
5,
width - 10,
height - 10,
24
);
ctx.fillStyle = "rgba(7, 18, 28, 0.88)";
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = accent;
ctx.globalAlpha = 0.55;
ctx.stroke();
ctx.globalAlpha = 1;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = accent;
ctx.font = `800 ${subtitle ? 42 : 48}px system-ui, sans-serif`;
ctx.fillText(
title,
width / 2,
subtitle ? height * 0.39 : height * 0.5
);
if (subtitle) {
ctx.fillStyle = "#c1d4de";
ctx.font = "600 23px system-ui, sans-serif";
ctx.fillText(subtitle, width / 2, height * 0.72);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
function addLine(points, color, opacity = 1) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((p) => new THREE.Vector3(...p))
);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
const line = new THREE.Line(geometry, material);
scene.add(line);
return line;
}
function createContourTexture(config) {
const canvas = document.createElement("canvas");
canvas.width = 920;
canvas.height = 570;
const ctx = canvas.getContext("2d");
const left = 72;
const right = 800;
const top = 65;
const bottom = 450;
const plotWidth = right - left;
const plotHeight = bottom - top;
// Board background.
const bg = ctx.createLinearGradient(0, 0, 920, 570);
bg.addColorStop(0, "#102431");
bg.addColorStop(1, "#08151f");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Outer border.
roundedRect(ctx, 10, 10, 900, 550, 20);
ctx.strokeStyle = "rgba(151, 218, 234, 0.35)";
ctx.lineWidth = 3;
ctx.stroke();
// Title.
ctx.textAlign = "left";
ctx.textBaseline = "alphabetic";
ctx.fillStyle = config.mapAccent;
ctx.font = "800 26px system-ui, sans-serif";
ctx.fillText(config.mapTitle, 42, 41);
ctx.textAlign = "right";
ctx.fillStyle = "#91aeba";
ctx.font = "600 17px system-ui, sans-serif";
ctx.fillText("0 – 3.2 m RELIEF", 878, 40);
// Plot background.
ctx.fillStyle = "#0a1822";
ctx.fillRect(left, top, plotWidth, plotHeight);
// Fine grid.
ctx.strokeStyle = "rgba(121, 174, 192, 0.12)";
ctx.lineWidth = 1;
for (let i = 0; i <= 20; i++) {
const x = left + (i / 20) * plotWidth;
ctx.beginPath();
ctx.moveTo(x, top);
ctx.lineTo(x, bottom);
ctx.stroke();
}
for (let i = 0; i <= 10; i++) {
const y = top + (i / 10) * plotHeight;
ctx.beginPath();
ctx.moveTo(left, y);
ctx.lineTo(right, y);
ctx.stroke();
}
const levels = [0.2, 0.4, 0.6, 0.8];
const contourColors = [
"#58d7b4",
"#79dfc3",
"#f0bf63",
"#ef9d5a"
];
const levelPixels = levels.map((level) => {
const normalizedX = config.inverseHeight(level);
return (
left +
normalizedX * plotWidth
);
});
// Contour lines.
levels.forEach((level, index) => {
const x = levelPixels[index];
ctx.strokeStyle = contourColors[index];
ctx.lineWidth = 4;
ctx.setLineDash([]);
ctx.beginPath();
ctx.moveTo(x, top);
ctx.lineTo(x, bottom);
ctx.stroke();
// Elevation dot and label.
ctx.fillStyle = contourColors[index];
ctx.beginPath();
ctx.arc(x, bottom, 6, 0, Math.PI * 2);
ctx.fill();
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.font = "700 16px system-ui, sans-serif";
ctx.fillText(
`${(level * TERRAIN_HEIGHT).toFixed(1)} m`,
Math.min(x + 9, 842),
top + 20 + index * 34
);
});
// Dimension bracket between the 40% and 60% contours.
const bracketY = 480;
const x40 = levelPixels[1];
const x60 = levelPixels[2];
ctx.strokeStyle = config.mapAccent;
ctx.lineWidth = 2.5;
ctx.beginPath();
ctx.moveTo(x40, bracketY - 7);
ctx.lineTo(x40, bracketY + 7);
ctx.moveTo(x40, bracketY);
ctx.lineTo(x60, bracketY);
ctx.moveTo(x60, bracketY - 7);
ctx.lineTo(x60, bracketY + 7);
ctx.stroke();
const spacingMeters =
((x60 - x40) / plotWidth) * TERRAIN_WIDTH;
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
ctx.fillStyle = "#d9edf3";
ctx.font = "700 16px system-ui, sans-serif";
ctx.fillText(
`40–60% contour spacing ≈ ${spacingMeters.toFixed(2)} m`,
460,
bracketY - 7
);
ctx.textBaseline = "alphabetic";
ctx.fillStyle = config.mapAccent;
ctx.font = "800 18px system-ui, sans-serif";
ctx.fillText(
config.subtitle.toUpperCase(),
460,
531
);
ctx.fillStyle = "#8fa9b6";
ctx.font = "600 15px system-ui, sans-serif";
ctx.fillText(
"Horizontal distance between equal-elevation contours",
460,
553
);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function createContourBoard(config) {
const group = new THREE.Group();
group.position.set(config.x, 0, 4.65);
const backing = new THREE.Mesh(
new THREE.BoxGeometry(MAP_WIDTH + 0.22, 0.14, MAP_DEPTH + 0.22),
new THREE.MeshStandardMaterial({
color: 0x07131d,
roughness: 0.72,
metalness: 0.16
})
);
backing.position.y = -1.08;
backing.castShadow = true;
backing.receiveShadow = true;
group.add(backing);
const map = new THREE.Mesh(
new THREE.PlaneGeometry(MAP_WIDTH, MAP_DEPTH),
new THREE.MeshBasicMaterial({
map: createContourTexture(config),
side: THREE.DoubleSide,
toneMapped: false
})
);
map.rotation.x = -Math.PI / 2;
map.position.y = -0.995;
group.add(map);
scene.add(group);
}
// -------------------------------------------------------------------------
// Voxel terrain construction
// -------------------------------------------------------------------------
function createVoxelTerrain(config) {
const group = new THREE.Group();
group.position.set(config.x, 0, config.z);
const support = new THREE.Mesh(
new THREE.BoxGeometry(
TERRAIN_WIDTH + 0.5,
0.72,
TERRAIN_DEPTH + 0.5
),
new THREE.MeshStandardMaterial({
color: 0x102531,
roughness: 0.84,
metalness: 0.12
})
);
support.position.y = -0.62;
support.castShadow = true;
support.receiveShadow = true;
group.add(support);
const slab = new THREE.Mesh(
new THREE.BoxGeometry(
TERRAIN_WIDTH + 0.48,
0.28,
TERRAIN_DEPTH + 0.48
),
new THREE.MeshStandardMaterial({
color: 0x193540,
roughness: 0.7,
metalness: 0.16
})
);
slab.position.y = -0.14;
slab.castShadow = true;
slab.receiveShadow = true;
group.add(slab);
const count = COLUMN_COUNT * ROW_COUNT;
const cellWidth =
TERRAIN_WIDTH / (COLUMN_COUNT - 1);
const cellDepth = TERRAIN_DEPTH / ROW_COUNT;
const blocks = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.83,
metalness: 0.04
}),
count
);
blocks.castShadow = true;
blocks.receiveShadow = true;
const dummy = new THREE.Object3D();
const matrices = [];
const lowColor = new THREE.Color(0x164d61);
const middleColor = new THREE.Color(0x318d76);
const highColor = new THREE.Color(0xf0b758);
let instance = 0;
for (let column = 0; column < COLUMN_COUNT; column++) {
const t = column / (COLUMN_COUNT - 1);
const height = Math.max(
0.045,
config.heightAt(t)
);
const x =
-TERRAIN_WIDTH / 2 +
column * cellWidth;
for (let row = 0; row < ROW_COUNT; row++) {
const z =
-TERRAIN_DEPTH / 2 +
(row + 0.5) * cellDepth;
dummy.position.set(
x,
height / 2,
z
);
dummy.scale.set(
cellWidth * 0.92,
height,
cellDepth * 0.92
);
dummy.updateMatrix();
blocks.setMatrixAt(instance, dummy.matrix);
matrices.push(dummy.matrix.clone());
const normalized = THREE.MathUtils.clamp(
height / TERRAIN_HEIGHT,
0,
1
);
const color = new THREE.Color();
if (normalized < 0.5) {
color.lerpColors(
lowColor,
middleColor,
normalized * 2
);
} else {
color.lerpColors(
middleColor,
highColor,
(normalized - 0.5) * 2
);
}
// Small deterministic variation keeps the voxels from looking flat.
const variation =
0.94 +
0.08 *
(0.5 +
0.5 *
Math.sin(
column * 12.9898 +
row * 78.233 +
(config.id === "gentle" ? 0 : 2.4)
));
color.multiplyScalar(variation);
blocks.setColorAt(instance, color);
instance++;
}
}
blocks.instanceMatrix.needsUpdate = true;
if (blocks.instanceColor) {
blocks.instanceColor.needsUpdate = true;
}
group.add(blocks);
// Crisp voxel outlines assembled into one LineSegments object.
const edgeTemplate = new THREE.EdgesGeometry(
new THREE.BoxGeometry(1, 1, 1)
);
const edgeVertices =
edgeTemplate.attributes.position.array;
const outlinePositions = [];
const vertex = new THREE.Vector3();
for (const matrix of matrices) {
for (let i = 0; i < edgeVertices.length; i += 3) {
vertex
.set(
edgeVertices[i],
edgeVertices[i + 1],
edgeVertices[i + 2]
)
.applyMatrix4(matrix);
outlinePositions.push(
vertex.x,
vertex.y,
vertex.z
);
}
}
const outlineGeometry = new THREE.BufferGeometry();
outlineGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
outlinePositions,
3
)
);
outlineGeometry.computeBoundingSphere();
const outlines = new THREE.LineSegments(
outlineGeometry,
new THREE.LineBasicMaterial({
color: 0x07151c,
transparent: true,
opacity: 0.55
})
);
group.add(outlines);
scene.add(group);
// Equal-height ruler beside the terrain.
const rulerX = TERRAIN_WIDTH / 2 + 0.58;
const rulerZ = -TERRAIN_DEPTH / 2 - 0.08;
addLine(
[
[rulerX, 0, rulerZ],
[rulerX, TERRAIN_HEIGHT, rulerZ]
],
config.accent,
0.95
);
addLine(
[
[rulerX - 0.14, 0, rulerZ],
[rulerX + 0.14, 0, rulerZ]
],
config.accent
);
addLine(
[
[
rulerX - 0.14,
TERRAIN_HEIGHT,
rulerZ
],
[
rulerX + 0.14,
TERRAIN_HEIGHT,
rulerZ
]
],
config.accent
);
const zeroLabel = makeLabelSprite(
"0 m",
"",
"#9fb5c0",
192,
88
);
zeroLabel.scale.set(0.95, 0.44, 1);
zeroLabel.position.set(
rulerX + 0.42,
0,
rulerZ
);
scene.add(zeroLabel);
const topLabel = makeLabelSprite(
"3.2 m",
"",
config.mapAccent,
192,
88
);
topLabel.scale.set(0.95, 0.44, 1);
topLabel.position.set(
rulerX + 0.42,
TERRAIN_HEIGHT,
rulerZ
);
scene.add(topLabel);
// Main title above the voxel model.
const title = makeLabelSprite(
config.title,
"equal 3.2 m vertical rise",
config.mapAccent
);
title.scale.set(4.7, 1.32, 1);
title.position.set(
config.x,
TERRAIN_HEIGHT + 1.05,
config.z
);
scene.add(title);
// Direction arrow from low to high.
const arrowOrigin = new THREE.Vector3(
config.x - TERRAIN_WIDTH / 2,
0.02,
config.z + TERRAIN_DEPTH / 2 + 0.42
);
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
arrowOrigin,
TERRAIN_WIDTH,
config.accent,
0.35,
0.18
);
scene.add(arrow);
const low = makeLabelSprite(
"LOW",
"",
"#a8c2cd",
160,
80
);
low.scale.set(0.72, 0.36, 1);
low.position.set(
config.x - TERRAIN_WIDTH / 2,
0.22,
config.z + TERRAIN_DEPTH / 2 + 0.42
);
scene.add(low);
const high = makeLabelSprite(
"HIGH",
"",
config.mapAccent,
160,
80
);
high.scale.set(0.72, 0.36, 1);
high.position.set(
config.x + TERRAIN_WIDTH / 2,
0.22,
config.z + TERRAIN_DEPTH / 2 + 0.42
);
scene.add(high);
createContourBoard(config);
}
createVoxelTerrain(GENTLE);
createVoxelTerrain(STEEP);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, reset, and top view
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.8, 1.55);
const cameraState = {
radius: 25,
theta: 0.18,
phi: 0.91
};
let pointerActive = false;
let pointerId = null;
let lastPointerX = 0;
let lastPointerY = 0;
let userAdjustedCamera = false;
function responsiveRadius() {
const aspect =
window.innerWidth / window.innerHeight;
return 24 * Math.max(1, 1.55 / aspect);
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
target.x +
cameraState.radius *
sinPhi *
Math.sin(cameraState.theta),
target.y +
cameraState.radius *
Math.cos(cameraState.phi),
target.z +
cameraState.radius *
sinPhi *
Math.cos(cameraState.theta)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.radius = responsiveRadius();
cameraState.theta = 0.18;
cameraState.phi = 0.91;
target.set(0, 0.8, 1.55);
userAdjustedCamera = false;
updateCamera();
}
function topCamera() {
cameraState.radius = responsiveRadius() * 1.04;
cameraState.theta = 0;
cameraState.phi = 0.055;
target.set(0, 0, 1.55);
userAdjustedCamera = true;
updateCamera();
}
renderer.domElement.addEventListener(
"pointerdown",
(event) => {
pointerActive = true;
pointerId = event.pointerId;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
}
);
renderer.domElement.addEventListener(
"pointermove",
(event) => {
if (!pointerActive || event.pointerId !== pointerId) {
return;
}
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.theta -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.045,
1.36
);
userAdjustedCamera = true;
updateCamera();
}
);
function endPointer(event) {
if (event.pointerId !== pointerId) {
return;
}
pointerActive = false;
pointerId = null;
if (
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(
event.pointerId
);
}
}
renderer.domElement.addEventListener(
"pointerup",
endPointer
);
renderer.domElement.addEventListener(
"pointercancel",
endPointer
);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius *
Math.exp(event.deltaY * 0.001),
10,
90
);
userAdjustedCamera = true;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener(
"dblclick",
resetCamera
);
document
.getElementById("reset-view")
.addEventListener("click", resetCamera);
document
.getElementById("top-view")
.addEventListener("click", topCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
if (event.key.toLowerCase() === "t") {
topCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect =
window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
if (!userAdjustedCamera) {
cameraState.radius = responsiveRadius();
}
updateCamera();
});
resetCamera();
// Render loop.
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```