Prompt specification
Build or customize the Three.js 3D scene "Two paths and one bearing" 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: Two paths and one bearing
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-paths-and-one-bearing-x1290
Scene Description Prompt:
Build a terrain cutaway with forked paths, a lookout, and north marker; camera rotation must preserve bearings. Three.js from cdnjs, one HTML file.
Working Three.js Starter (two-paths-and-one-bearing-x1290):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>North Trail — Terrain Cutaway</title>
<style>
:root {
--ink: #17312d;
--muted: #637871;
--paper: rgba(250, 248, 238, 0.92);
--line: rgba(27, 57, 50, 0.17);
--green: #486b54;
--gold: #d49b45;
--red: #a94f3d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce7e2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.card {
position: absolute;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow:
0 16px 45px rgba(30, 57, 49, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.75);
backdrop-filter: blur(12px);
}
.title-card {
top: 22px;
left: 22px;
width: 318px;
padding: 19px 20px 17px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
content: "";
box-shadow: 0 0 0 4px rgba(72, 107, 84, 0.12);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 31px;
font-weight: 500;
letter-spacing: -0.035em;
line-height: 1;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px 12px;
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.045em;
text-transform: uppercase;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.legend i {
display: inline-block;
width: 18px;
height: 4px;
border-radius: 4px;
background: var(--gold);
box-shadow: 0 0 0 1px rgba(85, 55, 28, 0.13);
}
.legend .fork {
width: 8px;
height: 8px;
border: 2px solid var(--gold);
border-radius: 50%;
background: transparent;
box-shadow: none;
}
.legend .lookout {
width: 9px;
height: 9px;
border: 2px solid #65482d;
border-radius: 2px;
background: #c58b4e;
box-shadow: none;
}
.legend .north {
width: 0;
height: 0;
border-right: 5px solid transparent;
border-bottom: 10px solid var(--red);
border-left: 5px solid transparent;
background: transparent;
box-shadow: none;
}
.compass-card {
top: 22px;
right: 22px;
width: 190px;
padding: 15px 15px 13px;
text-align: center;
}
.compass-label {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.compass-dial {
position: relative;
width: 112px;
height: 112px;
margin: 0 auto 10px;
border: 1px solid rgba(23, 49, 45, 0.32);
border-radius: 50%;
background:
radial-gradient(circle, rgba(255,255,255,0.8) 0 58%, transparent 59%),
repeating-conic-gradient(
from -1deg,
rgba(23, 49, 45, 0.19) 0deg 1deg,
transparent 1deg 15deg
);
box-shadow: inset 0 0 0 6px rgba(255,255,255,0.38);
}
.compass-dial::after {
position: absolute;
inset: 9px;
border: 1px solid rgba(23, 49, 45, 0.12);
border-radius: 50%;
content: "";
}
.rose {
position: absolute;
inset: 0;
transform-origin: 50% 50%;
will-change: transform;
}
.rose span {
position: absolute;
color: var(--ink);
font-size: 9px;
font-weight: 900;
line-height: 1;
}
.rose .n {
top: 6px;
left: 50%;
color: var(--red);
transform: translateX(-50%);
}
.rose .e {
top: 50%;
right: 7px;
transform: translateY(-50%);
}
.rose .s {
bottom: 6px;
left: 50%;
transform: translateX(-50%);
}
.rose .w {
top: 50%;
left: 7px;
transform: translateY(-50%);
}
.needle {
position: absolute;
top: 19px;
left: 50%;
z-index: 2;
width: 0;
height: 0;
border-right: 6px solid transparent;
border-bottom: 17px solid var(--red);
border-left: 6px solid transparent;
filter: drop-shadow(0 2px 2px rgba(72, 30, 24, 0.2));
transform: translateX(-50%);
}
.needle::after {
position: absolute;
top: 15px;
left: -5px;
width: 10px;
height: 35px;
background: var(--red);
clip-path: polygon(50% 0, 100% 100%, 50% 78%, 0 100%);
content: "";
}
.bearing {
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-variant-numeric: tabular-nums;
}
.bearing small {
margin-left: 5px;
color: var(--muted);
font-family: inherit;
font-size: 13px;
}
.bearing-state {
display: flex;
justify-content: center;
align-items: center;
gap: 6px;
margin-top: 7px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.bearing-state::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #4c8c61;
content: "";
box-shadow: 0 0 0 3px rgba(76, 140, 97, 0.12);
}
.actions {
position: absolute;
top: 244px;
right: 22px;
display: flex;
gap: 7px;
pointer-events: auto;
}
button {
min-height: 34px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(250, 248, 238, 0.94);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.055em;
text-transform: uppercase;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease,
box-shadow 150ms ease;
}
button:hover {
background: #fffdf4;
box-shadow: 0 5px 14px rgba(30, 57, 49, 0.11);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button:focus-visible {
outline: 2px solid var(--gold);
outline-offset: 2px;
}
.help-card {
right: 22px;
bottom: 22px;
padding: 11px 14px;
color: var(--muted);
font-size: 10px;
line-height: 1.6;
}
.help-card strong {
color: var(--ink);
font-weight: 800;
}
kbd {
display: inline-block;
min-width: 19px;
margin: 0 2px;
padding: 1px 4px;
border: 1px solid rgba(23, 49, 45, 0.2);
border-bottom-width: 2px;
border-radius: 4px;
background: rgba(255,255,255,0.55);
color: var(--ink);
font: inherit;
font-size: 9px;
text-align: center;
}
.scale-card {
bottom: 22px;
left: 22px;
padding: 11px 14px;
}
.scale-line {
position: relative;
width: 90px;
height: 7px;
border-right: 1px solid var(--ink);
border-bottom: 2px solid var(--ink);
border-left: 1px solid var(--ink);
}
.scale-line::after {
position: absolute;
top: 0;
left: 50%;
width: 1px;
height: 7px;
background: var(--ink);
content: "";
}
.scale-text {
margin-top: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#labels {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
display: none;
padding: 6px 9px;
border: 1px solid rgba(23, 49, 45, 0.16);
border-radius: 7px;
background: rgba(255, 253, 243, 0.9);
box-shadow: 0 5px 16px rgba(31, 54, 47, 0.12);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -100%);
}
.world-label::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 11px;
background: rgba(23, 49, 45, 0.5);
content: "";
}
.world-label.north-label {
border-color: rgba(169, 79, 61, 0.25);
color: var(--red);
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #dce7e2;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
@media (max-width: 700px) {
.title-card {
top: 12px;
left: 12px;
width: 244px;
padding: 14px 15px;
}
h1 {
font-size: 24px;
}
.subtitle,
.legend {
display: none;
}
.compass-card {
top: 12px;
right: 12px;
width: 142px;
padding: 11px;
}
.compass-dial {
width: 88px;
height: 88px;
}
.needle {
top: 14px;
}
.needle::after {
top: 13px;
height: 28px;
}
.actions {
top: 199px;
right: 12px;
}
.help-card {
display: none;
}
.scale-card {
bottom: 12px;
left: 12px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels">
<div class="world-label" id="fork-label">Trail fork</div>
<div class="world-label" id="lookout-label">Lookout</div>
<div class="world-label north-label" id="north-label">North · 000°</div>
</div>
<main class="hud">
<section class="card title-card">
<div class="eyebrow">Field model 07</div>
<h1>North Trail</h1>
<p class="subtitle">
A layered terrain cutaway with two branching approaches to the lookout.
The arrow, compass, and camera bearings share one fixed world north.
</p>
<div class="legend">
<span><i></i> Trail</span>
<span><i class="fork"></i> Fork</span>
<span><i class="lookout"></i> Lookout</span>
<span><i class="north"></i> North</span>
</div>
</section>
<section class="card compass-card">
<div class="compass-label">View bearing</div>
<div class="compass-dial">
<div class="rose" id="rose">
<span class="n">N</span>
<span class="e">E</span>
<span class="s">S</span>
<span class="w">W</span>
</div>
<div class="needle"></div>
</div>
<div class="bearing">
<span id="bearing-value">324°</span>
<small id="bearing-name">NW</small>
</div>
<div class="bearing-state">World-up · no roll</div>
</section>
<div class="actions">
<button id="north-button" type="button">North-up</button>
<button id="reset-button" type="button">Reset</button>
</div>
<section class="card help-card">
<strong>Drag</strong> to orbit · <strong>Shift-drag</strong> or right-drag to pan<br>
<strong>Scroll</strong> to zoom · <kbd>←</kbd><kbd>→</kbd> rotate ·
<kbd>R</kbd> reset
</section>
<section class="card scale-card">
<div class="scale-line"></div>
<div class="scale-text">100 m · model scale</div>
</section>
</main>
<div id="loading">Preparing terrain model</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const loading = document.querySelector("#loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce7e2);
scene.fog = new THREE.Fog(0xdce7e2, 31, 62);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
120
);
camera.up.set(0, 1, 0);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive terrain cutaway. Drag to orbit while preserving north bearings."
);
scene.add(new THREE.HemisphereLight(0xf4fbff, 0x79654f, 2.15));
const sun = new THREE.DirectionalLight(0xffedcf, 3.1);
sun.position.set(8, 15, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbad8dd, 0.65);
fill.position.set(-9, 7, -8);
scene.add(fill);
const shadowFloor = new THREE.Mesh(
new THREE.PlaneGeometry(120, 120),
new THREE.ShadowMaterial({
color: 0x253f37,
opacity: 0.16
})
);
shadowFloor.rotation.x = -Math.PI / 2;
shadowFloor.position.y = -2.34;
shadowFloor.receiveShadow = true;
scene.add(shadowFloor);
// Geographic convention:
// +X = east, -Z = north, +Y = up.
const TERRAIN_WIDTH = 16;
const TERRAIN_DEPTH = 12;
const SEGMENTS_X = 96;
const SEGMENTS_Z = 72;
const BASE_Y = -2.25;
// -------------------------------------------------------------------------
// Terrain height field
// -------------------------------------------------------------------------
function terrainHeight(x, z) {
const westernHill =
1.62 *
Math.exp(
-(
Math.pow((x + 3.35) / 3.9, 2) +
Math.pow((z + 1.15) / 4.25, 2)
)
);
const easternHill =
1.38 *
Math.exp(
-(
Math.pow((x - 3.8) / 3.15, 2) +
Math.pow((z + 1.7) / 3.6, 2)
)
);
const northernRidge =
0.78 *
Math.exp(
-(
Math.pow((x - 0.7) / 4.55, 2) +
Math.pow((z + 4.25) / 2.25, 2)
)
);
const shallowValley =
0.34 *
Math.exp(
-(
Math.pow((x - 0.45 * Math.sin(z * 0.55)) / 1.12, 2) +
Math.pow((z - 2.5) / 4.2, 2)
)
);
return (
0.16 +
westernHill +
easternHill +
northernRidge -
shallowValley +
0.14 * Math.sin(x * 0.72) * Math.cos(z * 0.58)
);
}
const heightGrid = [];
const terrainPositions = [];
const terrainColors = [];
const terrainIndices = [];
const lowColor = new THREE.Color(0x6f8f68);
const middleColor = new THREE.Color(0x879b6b);
const highColor = new THREE.Color(0xb3ad7e);
const vertexColor = new THREE.Color();
for (let iz = 0; iz <= SEGMENTS_Z; iz++) {
heightGrid[iz] = [];
const z = -TERRAIN_DEPTH / 2 +
(iz / SEGMENTS_Z) * TERRAIN_DEPTH;
for (let ix = 0; ix <= SEGMENTS_X; ix++) {
const x = -TERRAIN_WIDTH / 2 +
(ix / SEGMENTS_X) * TERRAIN_WIDTH;
const height = terrainHeight(x, z);
heightGrid[iz][ix] = height;
terrainPositions.push(x, height, z);
const t = THREE.MathUtils.clamp((height - 0.05) / 1.8, 0, 1);
if (t < 0.55) {
vertexColor.copy(lowColor).lerp(middleColor, t / 0.55);
} else {
vertexColor.copy(middleColor).lerp(highColor, (t - 0.55) / 0.45);
}
const variation =
0.965 +
0.035 *
Math.sin(x * 2.7 + z * 1.9) *
Math.cos(x * 0.8 - z * 1.1);
vertexColor.multiplyScalar(variation);
terrainColors.push(vertexColor.r, vertexColor.g, vertexColor.b);
}
}
for (let iz = 0; iz < SEGMENTS_Z; iz++) {
for (let ix = 0; ix < SEGMENTS_X; ix++) {
const a = iz * (SEGMENTS_X + 1) + ix;
const b = a + 1;
const d = (iz + 1) * (SEGMENTS_X + 1) + ix;
const c = d + 1;
terrainIndices.push(a, d, b, b, d, c);
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.setIndex(terrainIndices);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.97,
metalness: 0
})
);
terrain.castShadow = true;
terrain.receiveShadow = true;
scene.add(terrain);
// -------------------------------------------------------------------------
// Exposed geological walls
// -------------------------------------------------------------------------
const edges = [
{
point: t => ({
x: -TERRAIN_WIDTH / 2 + t * TERRAIN_WIDTH,
z: TERRAIN_DEPTH / 2
})
},
{
point: t => ({
x: TERRAIN_WIDTH / 2 - t * TERRAIN_WIDTH,
z: -TERRAIN_DEPTH / 2
})
},
{
point: t => ({
x: -TERRAIN_WIDTH / 2,
z: -TERRAIN_DEPTH / 2 + t * TERRAIN_DEPTH
})
},
{
point: t => ({
x: TERRAIN_WIDTH / 2,
z: TERRAIN_DEPTH / 2 - t * TERRAIN_DEPTH
})
}
];
function createWallLayer(edge, upperHeight, lowerHeight, color) {
const segments = 100;
const positions = [];
const indices = [];
for (let i = 0; i < segments; i++) {
const p0 = edge.point(i / segments);
const p1 = edge.point((i + 1) / segments);
const u0 = upperHeight(p0.x, p0.z);
const u1 = upperHeight(p1.x, p1.z);
const l0 = lowerHeight(p0.x, p0.z);
const l1 = lowerHeight(p1.x, p1.z);
const index = positions.length / 3;
positions.push(
p0.x, u0, p0.z,
p0.x, l0, p0.z,
p1.x, u1, p1.z,
p1.x, l1, p1.z
);
indices.push(
index, index + 1, index + 2,
index + 2, index + 1, index + 3
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color,
roughness: 1,
side: THREE.DoubleSide
})
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
const strata = [
{
upper: (x, z) => terrainHeight(x, z),
lower: (x, z) => terrainHeight(x, z) - 0.24,
color: 0x6b5138
},
{
upper: (x, z) => terrainHeight(x, z) - 0.24,
lower: () => -0.35,
color: 0xb56e4c
},
{
upper: () => -0.35,
lower: () => -1.15,
color: 0xd0a05d
},
{
upper: () => -1.15,
lower: () => BASE_Y,
color: 0x6c8083
}
];
for (const edge of edges) {
for (const layer of strata) {
createWallLayer(edge, layer.upper, layer.lower, layer.color);
}
}
// Fine dark line around the cut edge.
const edgePositions = [];
for (const edge of edges) {
for (let i = 0; i < 100; i++) {
const p0 = edge.point(i / 100);
const p1 = edge.point((i + 1) / 100);
edgePositions.push(
p0.x, terrainHeight(p0.x, p0.z) + 0.025, p0.z,
p1.x, terrainHeight(p1.x, p1.z) + 0.025, p1.z
);
}
}
const edgeGeometry = new THREE.BufferGeometry();
edgeGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(edgePositions, 3)
);
scene.add(
new THREE.LineSegments(
edgeGeometry,
new THREE.LineBasicMaterial({
color: 0x344a40,
transparent: true,
opacity: 0.52
})
)
);
// -------------------------------------------------------------------------
// Topographic contour lines
// -------------------------------------------------------------------------
const contourPositions = [];
const dx = TERRAIN_WIDTH / SEGMENTS_X;
const dz = TERRAIN_DEPTH / SEGMENTS_Z;
for (let level = 0.35; level < 2.65; level += 0.35) {
for (let iz = 0; iz < SEGMENTS_Z; iz++) {
for (let ix = 0; ix < SEGMENTS_X; ix++) {
const x0 = -TERRAIN_WIDTH / 2 + ix * dx;
const x1 = x0 + dx;
const z0 = -TERRAIN_DEPTH / 2 + iz * dz;
const z1 = z0 + dz;
const corners = [
{ x: x0, z: z0, h: heightGrid[iz][ix] },
{ x: x1, z: z0, h: heightGrid[iz][ix + 1] },
{ x: x1, z: z1, h: heightGrid[iz + 1][ix + 1] },
{ x: x0, z: z1, h: heightGrid[iz + 1][ix] }
];
const intersections = [];
for (let e = 0; e < 4; e++) {
const a = corners[e];
const b = corners[(e + 1) % 4];
if ((a.h - level) * (b.h - level) < 0) {
const t = (level - a.h) / (b.h - a.h);
intersections.push({
x: THREE.MathUtils.lerp(a.x, b.x, t),
z: THREE.MathUtils.lerp(a.z, b.z, t)
});
}
}
for (let i = 0; i + 1 < intersections.length; i += 2) {
const a = intersections[i];
const b = intersections[i + 1];
contourPositions.push(
a.x, level + 0.027, a.z,
b.x, level + 0.027, b.z
);
}
}
}
}
const contourGeometry = new THREE.BufferGeometry();
contourGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(contourPositions, 3)
);
scene.add(
new THREE.LineSegments(
contourGeometry,
new THREE.LineBasicMaterial({
color: 0x304c3e,
transparent: true,
opacity: 0.22
})
)
);
// -------------------------------------------------------------------------
// Forked trail system
// -------------------------------------------------------------------------
const trailOutlineMaterial = new THREE.MeshStandardMaterial({
color: 0x725134,
roughness: 1,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -1
});
const trailMaterial = new THREE.MeshStandardMaterial({
color: 0xd0a15e,
roughness: 1,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -2
});
function sampleRoute(coords, count = 100) {
const controlPoints = coords.map(
([x, z]) => new THREE.Vector3(x, 0, z)
);
const curve = new THREE.CatmullRomCurve3(
controlPoints,
false,
"centripetal",
0.5
);
return curve.getPoints(count).map(point => {
point.y = terrainHeight(point.x, point.z);
return point;
});
}
function createRibbon(points, width, material, lift) {
const positions = [];
const indices = [];
for (let i = 0; i < points.length; i++) {
const previous = points[Math.max(0, i - 1)];
const next = points[Math.min(points.length - 1, i + 1)];
const tangentX = next.x - previous.x;
const tangentZ = next.z - previous.z;
const length = Math.hypot(tangentX, tangentZ) || 1;
const normalX = -tangentZ / length;
const normalZ = tangentX / length;
const leftX = points[i].x + normalX * width * 0.5;
const leftZ = points[i].z + normalZ * width * 0.5;
const rightX = points[i].x - normalX * width * 0.5;
const rightZ = points[i].z - normalZ * width * 0.5;
positions.push(
leftX, terrainHeight(leftX, leftZ) + lift, leftZ,
rightX, terrainHeight(rightX, rightZ) + lift, rightZ
);
if (i < points.length - 1) {
const a = i * 2;
indices.push(a, a + 2, a + 1, a + 1, a + 2, a + 3);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const fork = [-0.35, 1.45];
const lookoutPosition = [-0.35, -4.15];
const trunkRoute = sampleRoute([
[-1.1, 5.62],
[-0.82, 4.25],
[-0.66, 2.9],
fork
]);
const westRoute = sampleRoute([
fork,
[-1.85, 1.1],
[-3.38, 0.3],
[-3.25, -1.48],
[-2.12, -2.82],
lookoutPosition
]);
const eastRoute = sampleRoute([
fork,
[1.25, 1.18],
[2.78, 0.15],
[2.63, -1.62],
[1.58, -3.0],
lookoutPosition
]);
createRibbon(trunkRoute, 0.54, trailOutlineMaterial, 0.065);
createRibbon(trunkRoute, 0.35, trailMaterial, 0.09);
createRibbon(westRoute, 0.46, trailOutlineMaterial, 0.065);
createRibbon(westRoute, 0.28, trailMaterial, 0.09);
createRibbon(eastRoute, 0.46, trailOutlineMaterial, 0.065);
createRibbon(eastRoute, 0.28, trailMaterial, 0.09);
// Small fork marker stones.
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0x77776b,
roughness: 1
});
for (const [offsetX, offsetZ, scale] of [
[-0.42, -0.08, 0.13],
[0.37, 0.04, 0.1]
]) {
const stone = new THREE.Mesh(
new THREE.DodecahedronGeometry(scale, 0),
stoneMaterial
);
stone.position.set(
fork[0] + offsetX,
terrainHeight(fork[0] + offsetX, fork[1] + offsetZ) +
scale * 0.55,
fork[1] + offsetZ
);
stone.rotation.set(0.2, 0.5, 0.12);
stone.castShadow = true;
scene.add(stone);
}
// -------------------------------------------------------------------------
// Lookout platform and telescope
// -------------------------------------------------------------------------
const lookout = new THREE.Group();
scene.add(lookout);
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x855837,
roughness: 0.9
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
color: 0x4b3525,
roughness: 0.95
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x304d43,
roughness: 0.6,
metalness: 0.25
});
const lensMaterial = new THREE.MeshStandardMaterial({
color: 0x8bc5cb,
roughness: 0.2,
metalness: 0.2
});
const lookoutX = lookoutPosition[0];
const lookoutZ = lookoutPosition[1];
const lookoutGround = terrainHeight(lookoutX, lookoutZ);
const deckY = lookoutGround + 0.48;
const deck = new THREE.Mesh(
new THREE.CylinderGeometry(1.04, 1.04, 0.14, 40),
woodMaterial
);
deck.position.set(lookoutX, deckY, lookoutZ);
deck.castShadow = true;
deck.receiveShadow = true;
lookout.add(deck);
// Radial plank seams.
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const seam = new THREE.Mesh(
new THREE.BoxGeometry(0.018, 0.008, 1.95),
darkWoodMaterial
);
seam.position.set(
lookoutX,
deckY + 0.074,
lookoutZ
);
seam.rotation.y = angle;
lookout.add(seam);
}
// Supports under the platform.
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const x = lookoutX + Math.cos(angle) * 0.75;
const z = lookoutZ + Math.sin(angle) * 0.75;
const ground = terrainHeight(x, z);
const height = Math.max(0.12, deckY - 0.07 - ground);
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.065, height, 8),
darkWoodMaterial
);
support.position.set(x, ground + height * 0.5, z);
support.castShadow = true;
lookout.add(support);
}
// Circular safety railing.
for (const railingHeight of [0.4, 0.78]) {
const rail = new THREE.Mesh(
new THREE.TorusGeometry(0.94, 0.027, 8, 64),
darkWoodMaterial
);
rail.rotation.x = Math.PI / 2;
rail.position.set(lookoutX, deckY + railingHeight, lookoutZ);
rail.castShadow = true;
lookout.add(rail);
}
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.032, 0.032, 0.8, 8),
darkWoodMaterial
);
post.position.set(
lookoutX + Math.cos(angle) * 0.94,
deckY + 0.4,
lookoutZ + Math.sin(angle) * 0.94
);
post.castShadow = true;
lookout.add(post);
}
function cylinderBetween(start, end, radius, material, radialSegments = 8) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
return mesh;
}
// Tripod.
for (let i = 0; i < 3; i++) {
const angle = (i / 3) * Math.PI * 2;
const start = new THREE.Vector3(
lookoutX + Math.cos(angle) * 0.32,
deckY + 0.08,
lookoutZ + Math.sin(angle) * 0.32
);
const end = new THREE.Vector3(
lookoutX,
deckY + 0.94,
lookoutZ
);
lookout.add(cylinderBetween(start, end, 0.024, metalMaterial));
}
// Scope points along geographic north (-Z).
const scope = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.125, 0.68, 16),
metalMaterial
);
scope.position.set(lookoutX, deckY + 1.03, lookoutZ);
scope.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, 0, -1)
);
scope.castShadow = true;
lookout.add(scope);
const eyepiece = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.18, 12),
darkWoodMaterial
);
eyepiece.position.set(lookoutX, deckY + 1.03, lookoutZ + 0.41);
eyepiece.quaternion.copy(scope.quaternion);
lookout.add(eyepiece);
const lens = new THREE.Mesh(
new THREE.CylinderGeometry(0.083, 0.083, 0.015, 20),
lensMaterial
);
lens.position.set(lookoutX, deckY + 1.03, lookoutZ - 0.347);
lens.quaternion.copy(scope.quaternion);
lookout.add(lens);
// -------------------------------------------------------------------------
// North marker: a ground arrow pointing exactly toward -Z
// -------------------------------------------------------------------------
const northX = 6.0;
const northZ = -4.55;
const northGround = terrainHeight(northX, northZ);
const arrowShape = new THREE.Shape();
arrowShape.moveTo(0, 1.05);
arrowShape.lineTo(0.42, 0.15);
arrowShape.lineTo(0.15, 0.15);
arrowShape.lineTo(0.15, -0.82);
arrowShape.lineTo(-0.15, -0.82);
arrowShape.lineTo(-0.15, 0.15);
arrowShape.lineTo(-0.42, 0.15);
arrowShape.closePath();
const arrowGeometry = new THREE.ShapeGeometry(arrowShape);
arrowGeometry.rotateX(-Math.PI / 2);
const northArrow = new THREE.Mesh(
arrowGeometry,
new THREE.MeshStandardMaterial({
color: 0xeee5c7,
roughness: 0.85,
side: THREE.DoubleSide
})
);
northArrow.position.set(northX, northGround + 0.1, northZ);
northArrow.castShadow = true;
scene.add(northArrow);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.045, 1.5, 10),
darkWoodMaterial
);
post.position.set(northX + 0.48, northGround + 0.75, northZ + 0.05);
post.castShadow = true;
scene.add(post);
function makeNorthBadge() {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 256, 256);
context.beginPath();
context.arc(128, 128, 104, 0, Math.PI * 2);
context.fillStyle = "#f3ead0";
context.fill();
context.lineWidth = 12;
context.strokeStyle = "#9c4738";
context.stroke();
context.fillStyle = "#243b34";
context.font = "bold 126px Georgia";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText("N", 128, 137);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(0.8, 0.8, 1);
sprite.position.set(northX + 0.48, northGround + 1.7, northZ + 0.05);
return sprite;
}
scene.add(makeNorthBadge());
// -------------------------------------------------------------------------
// Custom world-up camera controls
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.45, -0.1);
const initialTarget = target.clone();
let azimuth = 0.62;
let polar = 0.86;
let radius = 23.5;
const initialCamera = {
azimuth,
polar,
radius
};
function updateCamera() {
const sinPolar = Math.sin(polar);
const cosPolar = Math.cos(polar);
camera.position.set(
target.x + radius * Math.sin(azimuth) * sinPolar,
target.y + radius * cosPolar,
target.z + radius * Math.cos(azimuth) * sinPolar
);
// The camera's up axis is permanently world-up. It never rolls.
camera.up.set(0, 1, 0);
camera.lookAt(target);
}
updateCamera();
const canvas = renderer.domElement;
let drag = null;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
drag = {
x: event.clientX,
y: event.clientY,
pan: event.button === 2 || event.shiftKey
};
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!drag) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
if (drag.pan) {
const right = new THREE.Vector3(
Math.cos(azimuth),
0,
-Math.sin(azimuth)
);
const forward = new THREE.Vector3(
-Math.sin(azimuth),
0,
-Math.cos(azimuth)
);
const panScale = radius * 0.00145;
target.addScaledVector(right, -dx * panScale);
target.addScaledVector(forward, dy * panScale);
target.x = THREE.MathUtils.clamp(target.x, -7, 7);
target.z = THREE.MathUtils.clamp(target.z, -5.5, 5.5);
} else {
// Yaw about world Y and pitch around the target. No roll is introduced.
azimuth -= dx * 0.006;
polar = THREE.MathUtils.clamp(
polar + dy * 0.005,
0.28,
1.34
);
}
});
function endDrag(event) {
drag = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
11,
34
);
},
{ passive: false }
);
document.querySelector("#reset-button").addEventListener("click", () => {
target.copy(initialTarget);
azimuth = initialCamera.azimuth;
polar = initialCamera.polar;
radius = initialCamera.radius;
});
document.querySelector("#north-button").addEventListener("click", () => {
azimuth = 0;
polar = 0.9;
});
addEventListener("keydown", event => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLTextAreaElement
) {
return;
}
switch (event.key.toLowerCase()) {
case "arrowleft":
azimuth += 0.09;
break;
case "arrowright":
azimuth -= 0.09;
break;
case "arrowup":
polar = Math.max(0.28, polar - 0.06);
break;
case "arrowdown":
polar = Math.min(1.34, polar + 0.06);
break;
case "+":
case "=":
radius = Math.max(11, radius - 1);
break;
case "-":
radius = Math.min(34, radius + 1);
break;
case "r":
target.copy(initialTarget);
azimuth = initialCamera.azimuth;
polar = initialCamera.polar;
radius = initialCamera.radius;
break;
}
});
// -------------------------------------------------------------------------
// Compass and projected world labels
// -------------------------------------------------------------------------
const rose = document.querySelector("#rose");
const bearingValue = document.querySelector("#bearing-value");
const bearingName = document.querySelector("#bearing-name");
const bearingNames = [
"N", "NNE", "NE", "ENE",
"E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW",
"W", "WNW", "NW", "NNW"
];
const worldLabels = [
{
element: document.querySelector("#fork-label"),
position: new THREE.Vector3(
fork[0],
terrainHeight(fork[0], fork[1]) + 1.05,
fork[1]
)
},
{
element: document.querySelector("#lookout-label"),
position: new THREE.Vector3(
lookoutX,
deckY + 1.75,
lookoutZ
)
},
{
element: document.querySelector("#north-label"),
position: new THREE.Vector3(
northX + 0.48,
northGround + 2.35,
northZ + 0.05
)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of worldLabels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
label.element.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
}
function updateCompass() {
const viewDirection = new THREE.Vector3().subVectors(
target,
camera.position
);
const heading = (
THREE.MathUtils.radToDeg(
Math.atan2(viewDirection.x, -viewDirection.z)
) +
360
) % 360;
const nameIndex = Math.round(heading / 22.5) % 16;
bearingValue.textContent = `${Math.round(heading)}°`;
bearingName.textContent = bearingNames[nameIndex];
// Rotate the map/cardinal rose opposite the viewing direction.
rose.style.transform = `rotate(${-heading}deg)`;
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
addEventListener("resize", onResize);
renderer.setAnimationLoop(() => {
updateCamera();
updateCompass();
updateLabels();
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loading.style.opacity = "0";
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```