Prompt specification
Build or customize the Three.js 3D scene "Mountain footpath switchback and retaining edge" 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: Mountain footpath switchback and retaining edge
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mountain-footpath-switchback-and-retaining-edge-x3081
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Build a referenced Himalayan path fragment with one switchback and supporting stone edge. Show the gentler climbing route compared with a direct steep line up the slope.
Working Three.js Starter (mountain-footpath-switchback-and-retaining-edge-x3081):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Himalayan Path — Switchback Study</title>
<style>
:root {
color-scheme: dark;
--ink: #f3f1e8;
--muted: #b9c3c1;
--glass: rgba(18, 28, 31, 0.78);
--line: rgba(255, 255, 255, 0.14);
--gentle: #4de0c2;
--direct: #ff8b45;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c7d5d8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging { cursor: grabbing; }
.panel {
position: fixed;
z-index: 10;
color: var(--ink);
background: var(--glass);
border: 1px solid var(--line);
box-shadow: 0 18px 55px rgba(12, 22, 25, 0.22);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#title-card {
top: 24px;
left: 24px;
width: min(355px, calc(100vw - 48px));
padding: 19px 20px 17px;
border-radius: 16px;
}
.eyebrow {
margin-bottom: 8px;
color: #d4e2df;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 4vw, 37px);
font-weight: 500;
line-height: 1.04;
letter-spacing: -0.025em;
}
.subtitle {
margin: 12px 0 0;
max-width: 295px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.feature {
display: flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #dbe5e2;
font-size: 11px;
letter-spacing: 0.03em;
}
.feature::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--gentle);
box-shadow: 0 0 14px rgba(77, 224, 194, 0.8);
content: "";
}
#legend {
right: 24px;
bottom: 24px;
width: min(320px, calc(100vw - 48px));
padding: 15px;
border-radius: 15px;
}
.legend-heading {
margin: 0 0 11px 3px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 32px 1fr auto;
align-items: center;
gap: 10px;
width: 100%;
min-height: 46px;
padding: 7px 9px;
border: 0;
border-radius: 10px;
color: var(--ink);
background: transparent;
font: inherit;
text-align: left;
cursor: pointer;
transition: background 160ms ease, opacity 160ms ease;
}
.legend-row:hover { background: rgba(255, 255, 255, 0.07); }
.legend-row.off { opacity: 0.38; }
.swatch {
position: relative;
width: 28px;
height: 4px;
border-radius: 5px;
background: var(--gentle);
box-shadow: 0 0 10px rgba(77, 224, 194, 0.35);
}
.swatch.direct {
background: repeating-linear-gradient(
90deg,
var(--direct) 0 7px,
transparent 7px 11px
);
box-shadow: none;
}
.legend-name {
display: block;
font-size: 12px;
font-weight: 700;
}
.legend-note {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.grade {
min-width: 47px;
color: #eef2ef;
font-family: Georgia, serif;
font-size: 17px;
text-align: right;
}
#hint {
position: fixed;
z-index: 9;
left: 50%;
bottom: 26px;
padding: 9px 13px;
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 999px;
color: rgba(245, 248, 246, 0.82);
background: rgba(20, 29, 31, 0.55);
backdrop-filter: blur(10px);
font-size: 10px;
letter-spacing: 0.035em;
pointer-events: none;
transform: translateX(-50%);
transition: opacity 500ms ease;
}
#reset {
position: fixed;
z-index: 11;
top: 24px;
right: 24px;
width: 38px;
height: 38px;
border: 1px solid var(--line);
border-radius: 50%;
color: var(--ink);
background: var(--glass);
backdrop-filter: blur(12px);
font-size: 18px;
cursor: pointer;
transition: transform 160ms ease, background 160ms ease;
}
#reset:hover {
background: rgba(40, 55, 58, 0.9);
transform: rotate(-18deg);
}
.pin {
position: fixed;
z-index: 8;
display: none;
min-width: 145px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 10px;
color: #f8faf7;
background: rgba(21, 31, 34, 0.78);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
backdrop-filter: blur(9px);
pointer-events: none;
transform: translate(-50%, -100%);
}
.pin::after {
position: absolute;
left: 50%;
bottom: -7px;
width: 10px;
height: 10px;
border-right: 1px solid rgba(255, 255, 255, 0.22);
border-bottom: 1px solid rgba(255, 255, 255, 0.22);
background: rgba(21, 31, 34, 0.78);
content: "";
transform: translateX(-50%) rotate(45deg);
}
.pin strong {
display: block;
font-size: 9px;
letter-spacing: 0.12em;
}
.pin small {
display: block;
margin-top: 3px;
color: #c7d2cf;
font-size: 9px;
}
#gentle-pin strong { color: var(--gentle); }
#direct-pin strong { color: var(--direct); }
@media (max-width: 700px) {
#title-card {
top: 14px;
left: 14px;
width: min(310px, calc(100vw - 68px));
padding: 14px 15px;
}
.subtitle { display: none; }
.feature { margin-top: 9px; padding-top: 9px; }
#legend {
right: 14px;
bottom: 14px;
width: min(290px, calc(100vw - 28px));
}
#reset {
top: 14px;
right: 14px;
}
#hint { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="title-card" class="panel">
<div class="eyebrow">Himalayan path study · 01</div>
<h1>The switchback<br />and the steep line</h1>
<p class="subtitle">
A stylized mountain trail fragment showing how one broad hairpin
replaces a direct climb up the slope.
</p>
<div class="feature">One switchback with a dry-stone supporting edge</div>
</section>
<section id="legend" class="panel" aria-label="Route comparison">
<p class="legend-heading">Compare climbing routes</p>
<button class="legend-row" id="gentle-toggle" type="button">
<span class="swatch"></span>
<span>
<span class="legend-name">Gentle switchback</span>
<span class="legend-note">Longer traverse · lower average grade</span>
</span>
<span class="grade" id="gentle-grade">—</span>
</button>
<button class="legend-row" id="direct-toggle" type="button">
<span class="swatch direct"></span>
<span>
<span class="legend-name">Direct steep line</span>
<span class="legend-note">Shortest distance · steep ascent</span>
</span>
<span class="grade" id="direct-grade">—</span>
</button>
</section>
<button id="reset" type="button" title="Reset view" aria-label="Reset view">↺</button>
<div id="hint">Drag to orbit · Scroll to zoom · Click a route to toggle it</div>
<div class="pin" id="gentle-pin">
<strong>GENTLE SWITCHBACK</strong>
<small id="gentle-pin-note">Longer, lower-grade climb</small>
</div>
<div class="pin" id="direct-pin">
<strong>DIRECT STEEP LINE</strong>
<small id="direct-pin-note">Short route, severe grade</small>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const gentleToggle = document.getElementById("gentle-toggle");
const directToggle = document.getElementById("direct-toggle");
const resetButton = document.getElementById("reset");
const hint = document.getElementById("hint");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc9d7d9);
scene.fog = new THREE.Fog(0xc9d7d9, 48, 112);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xddebf0, 0x5b4939, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d2, 3.0);
sun.position.set(-22, 42, 22);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -38;
sun.shadow.camera.right = 38;
sun.shadow.camera.top = 38;
sun.shadow.camera.bottom = -38;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x91b7c4, 0.7);
fill.position.set(24, 16, -30);
scene.add(fill);
// A deterministic, gently rolling alpine slope.
function terrainHeight(x, z) {
const rise = Math.max(0, (18 - z) * 0.78);
const sideRise = 0.0065 * x * x;
const broadFold =
0.48 * Math.sin(x * 0.16 + z * 0.055) +
0.25 * Math.sin(z * 0.31 - x * 0.08);
const smallFold = 0.12 * Math.sin(x * 0.68 + z * 0.42);
return rise + sideRise + broadFold + smallFold;
}
function makeTerrain() {
const width = 54;
const minZ = -33;
const maxZ = 25;
const segmentsX = 72;
const segmentsZ = 84;
const row = segmentsX + 1;
const positions = [];
const colors = [];
const indices = [];
const low = new THREE.Color(0x68775b);
const alpine = new THREE.Color(0x847c66);
const rock = new THREE.Color(0x777b78);
const snow = new THREE.Color(0xe7ece7);
const color = new THREE.Color();
for (let iz = 0; iz <= segmentsZ; iz++) {
const v = iz / segmentsZ;
const z = maxZ - v * (maxZ - minZ);
for (let ix = 0; ix <= segmentsX; ix++) {
const u = ix / segmentsX;
const x = (u - 0.5) * width;
const y = terrainHeight(x, z);
positions.push(x, y, z);
if (y < 13) {
color.copy(low).lerp(alpine, THREE.MathUtils.clamp((y - 3) / 12, 0, 1));
} else if (y < 23) {
color.copy(alpine).lerp(rock, (y - 13) / 10);
} else {
color.copy(rock).lerp(snow, THREE.MathUtils.clamp((y - 23) / 7, 0, 1));
}
const variation =
0.92 +
0.08 * Math.sin(ix * 1.73 + iz * 2.11) +
0.035 * Math.sin(ix * 0.37 - iz * 0.51);
color.multiplyScalar(variation);
colors.push(color.r, color.g, color.b);
}
}
for (let iz = 0; iz < segmentsZ; iz++) {
for (let ix = 0; ix < segmentsX; ix++) {
const a = iz * row + ix;
const b = a + 1;
const c = a + row;
const d = c + 1;
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.96,
metalness: 0,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
}
makeTerrain();
// Distant faceted Himalayan-style peaks.
function addPeak(x, z, radius, height, rotation) {
const peakGeometry = new THREE.ConeGeometry(radius, height, 5, 1);
const peakMaterial = new THREE.MeshStandardMaterial({
color: 0x718087,
roughness: 1,
flatShading: true
});
const peak = new THREE.Mesh(peakGeometry, peakMaterial);
peak.position.set(x, height * 0.5 - 5, z);
peak.rotation.y = rotation;
peak.receiveShadow = true;
scene.add(peak);
const capHeight = height * 0.43;
const capGeometry = new THREE.ConeGeometry(radius * 0.43, capHeight, 5, 1);
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xe4e9e5,
roughness: 1,
flatShading: true
});
const cap = new THREE.Mesh(capGeometry, capMaterial);
cap.position.set(
x,
height - 5 - capHeight * 0.5 + 0.08,
z
);
cap.rotation.y = rotation;
scene.add(cap);
}
addPeak(-21, -49, 18, 39, 0.25);
addPeak(1, -56, 23, 47, 0.65);
addPeak(24, -48, 17, 35, 1.1);
// The single hairpin traverses right, turns, and returns left.
const pathXZ = [
[-11.0, 16.0],
[-10.5, 10.0],
[-8.0, 5.0],
[-1.0, 2.5],
[7.0, 0.5],
[10.0, -2.0],
[8.5, -5.0],
[1.0, -7.0],
[-6.0, -9.0],
[-5.5, -14.0],
[-3.0, -18.0]
];
class TerrainPathCurve extends THREE.Curve {
constructor(points) {
super();
this.controlCurve = new THREE.CatmullRomCurve3(
points.map(([x, z]) => new THREE.Vector3(x, 0, z)),
false,
"centripetal",
0.5
);
}
getPoint(t, target = new THREE.Vector3()) {
this.controlCurve.getPoint(t, target);
target.y = terrainHeight(target.x, target.z) + 0.17;
return target;
}
}
const pathCurve = new TerrainPathCurve(pathXZ);
// A broad ochre walking surface.
function buildTrailRibbon(curve, width = 1.55, segments = 230) {
const positions = [];
const indices = [];
const normals = [];
const center = new THREE.Vector3();
const tangent = new THREE.Vector3();
const side = new THREE.Vector3();
for (let i = 0; i <= segments; i++) {
const t = i / segments;
curve.getPoint(t, center);
curve.getTangent(t, tangent).normalize();
side.set(-tangent.z, 0, tangent.x).normalize();
for (const direction of [1, -1]) {
const x = center.x + side.x * width * 0.5 * direction;
const z = center.z + side.z * width * 0.5 * direction;
const y = terrainHeight(x, z) + 0.095;
positions.push(x, y, z);
normals.push(0, 1, 0);
}
}
for (let i = 0; i < segments; i++) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, c, b, b, c, d);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"normal",
new THREE.Float32BufferAttribute(normals, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: 0xc4a879,
roughness: 1,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
}
buildTrailRibbon(pathCurve);
// The cyan overlay communicates the gentler route.
const gentleGroup = new THREE.Group();
scene.add(gentleGroup);
const gentleTube = new THREE.Mesh(
new THREE.TubeGeometry(pathCurve, 260, 0.095, 7, false),
new THREE.MeshStandardMaterial({
color: 0x4de0c2,
emissive: 0x126c62,
emissiveIntensity: 0.75,
roughness: 0.45
})
);
gentleTube.castShadow = true;
gentleGroup.add(gentleTube);
// Dry-stone supporting edge, placed on the locally lower side.
function addStoneEdge(curve) {
const count = 94;
const geometry = new THREE.BoxGeometry(0.43, 0.53, 0.78);
const material = new THREE.MeshStandardMaterial({
color: 0x8c8d86,
roughness: 1,
flatShading: true
});
const stones = new THREE.InstancedMesh(geometry, material, count);
stones.castShadow = true;
stones.receiveShadow = true;
const dummy = new THREE.Object3D();
const center = new THREE.Vector3();
const tangent = new THREE.Vector3();
const side = new THREE.Vector3();
const color = new THREE.Color();
for (let i = 0; i < count; i++) {
const t = (i + 0.35) / count;
curve.getPoint(t, center);
curve.getTangent(t, tangent).normalize();
side.set(-tangent.z, 0, tangent.x).normalize();
const leftHeight = terrainHeight(
center.x + side.x * 0.98,
center.z + side.z * 0.98
);
const rightHeight = terrainHeight(
center.x - side.x * 0.98,
center.z - side.z * 0.98
);
const sign = leftHeight < rightHeight ? 1 : -1;
const edgeX = center.x + side.x * 0.98 * sign;
const edgeZ = center.z + side.z * 0.98 * sign;
dummy.position.set(edgeX, center.y - 0.13, edgeZ);
dummy.rotation.set(
0,
Math.atan2(tangent.x, tangent.z),
0
);
dummy.scale.set(
0.82 + 0.28 * Math.sin(i * 2.31),
0.78 + 0.31 * Math.abs(Math.cos(i * 1.73)),
0.84 + 0.23 * Math.sin(i * 0.91)
);
dummy.updateMatrix();
stones.setMatrixAt(i, dummy.matrix);
const shade = 0.79 + 0.18 * Math.sin(i * 1.47);
color.setRGB(shade * 0.98, shade, shade * 0.91);
stones.setColorAt(i, color);
}
stones.instanceMatrix.needsUpdate = true;
if (stones.instanceColor) stones.instanceColor.needsUpdate = true;
scene.add(stones);
}
addStoneEdge(pathCurve);
// The direct route is a straight horizontal plan, but follows the slope.
const startXZ = pathXZ[0];
const endXZ = pathXZ[pathXZ.length - 1];
function directPoint(t, target = new THREE.Vector3()) {
target.set(
THREE.MathUtils.lerp(startXZ[0], endXZ[0], t),
0,
THREE.MathUtils.lerp(startXZ[1], endXZ[1], t)
);
target.y = terrainHeight(target.x, target.z) + 0.24;
return target;
}
const directGroup = new THREE.Group();
scene.add(directGroup);
const dashGeometry = new THREE.CylinderGeometry(0.085, 0.085, 1, 7);
const dashMaterial = new THREE.MeshStandardMaterial({
color: 0xff8b45,
emissive: 0x8a2d0b,
emissiveIntensity: 0.55,
roughness: 0.5
});
const dashCount = 23;
const p0 = new THREE.Vector3();
const p1 = new THREE.Vector3();
const midpoint = new THREE.Vector3();
const dashDirection = new THREE.Vector3();
const up = new THREE.Vector3(0, 1, 0);
for (let i = 0; i < dashCount; i++) {
const t0 = i / dashCount;
const t1 = Math.min(1, t0 + 0.56 / dashCount);
directPoint(t0, p0);
directPoint(t1, p1);
midpoint.copy(p0).add(p1).multiplyScalar(0.5);
dashDirection.copy(p1).sub(p0);
const dash = new THREE.Mesh(dashGeometry, dashMaterial);
dash.position.copy(midpoint);
dash.scale.y = dashDirection.length();
dash.quaternion.setFromUnitVectors(
up,
dashDirection.clone().normalize()
);
dash.castShadow = true;
directGroup.add(dash);
}
// Directional arrow cones.
function addArrow(group, curve, t, color, size = 1) {
const position = curve.getPoint(t);
const tangent = curve.getTangent(t).normalize();
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.25 * size, 0.8 * size, 6),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.22,
roughness: 0.48
})
);
arrow.position.copy(position);
arrow.position.y += 0.12;
arrow.quaternion.setFromUnitVectors(up, tangent);
arrow.castShadow = true;
group.add(arrow);
}
[0.14, 0.36, 0.62, 0.86].forEach((t) => {
addArrow(gentleGroup, pathCurve, t, 0x4de0c2, 0.95);
});
const directCurve = new THREE.Curve();
directCurve.getPoint = (t, target = new THREE.Vector3()) =>
directPoint(t, target);
[0.26, 0.58, 0.84].forEach((t) => {
addArrow(directGroup, directCurve, t, 0xff8b45, 0.9);
});
// Start marker.
const startPoint = pathCurve.getPoint(0);
const startRing = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.075, 7, 28),
new THREE.MeshStandardMaterial({
color: 0xf2eee0,
emissive: 0x635d48,
emissiveIntensity: 0.25
})
);
startRing.rotation.x = Math.PI / 2;
startRing.position.copy(startPoint);
startRing.position.y = terrainHeight(startPoint.x, startPoint.z) + 0.16;
scene.add(startRing);
// Small alpine junipers along the outer edges of the fragment.
let randomSeed = 7481;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x5b4937,
roughness: 1,
flatShading: true
});
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0x405d4b,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 25; i++) {
const side = random() > 0.5 ? 1 : -1;
const x = side * (13 + random() * 10);
const z = 13 - random() * 32;
const y = terrainHeight(x, z);
if (y > 24) continue;
const scale = 0.7 + random() * 0.65;
const tree = new THREE.Group();
tree.position.set(x, y, z);
tree.rotation.y = random() * Math.PI * 2;
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.17, 1.4, 5),
trunkMaterial
);
trunk.position.y = 0.7;
trunk.castShadow = true;
tree.add(trunk);
const foliage = new THREE.Mesh(
new THREE.ConeGeometry(0.85, 2.8, 6),
foliageMaterial
);
foliage.position.y = 2.15;
foliage.castShadow = true;
tree.add(foliage);
tree.scale.setScalar(scale);
scene.add(tree);
}
// Loose rocks.
const rockGeometry = new THREE.DodecahedronGeometry(0.45, 0);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x777a74,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 22; i++) {
const x = (random() - 0.5) * 44;
const z = 15 - random() * 39;
const y = terrainHeight(x, z);
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(x, y + 0.12, z);
rock.scale.set(
0.55 + random() * 1.25,
0.35 + random() * 0.65,
0.55 + random() * 1.2
);
rock.rotation.set(random(), random() * 6, random());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// A small string of Himalayan-style prayer flags near the upper trail.
const flags = [];
const flagColors = [
0x2e73b8,
0xf3f1e7,
0xc83d3d,
0x3b9b61,
0xe6ad37
];
function addPrayerFlags() {
const ax = -9.6;
const az = -12.5;
const bx = 0.7;
const bz = -17.0;
const ay = terrainHeight(ax, az);
const by = terrainHeight(bx, bz);
const poleHeight = 3.5;
const poleGeometry = new THREE.CylinderGeometry(0.075, 0.1, poleHeight, 7);
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0x66503b,
roughness: 1
});
for (const [x, z, y] of [[ax, az, ay], [bx, bz, by]]) {
const pole = new THREE.Mesh(poleGeometry, poleMaterial);
pole.position.set(x, y + poleHeight * 0.5, z);
pole.castShadow = true;
scene.add(pole);
}
const stringPoints = [
new THREE.Vector3(ax, ay + poleHeight, az),
new THREE.Vector3((ax + bx) / 2, (ay + by) / 2 + poleHeight - 0.45, (az + bz) / 2),
new THREE.Vector3(bx, by + poleHeight, bz)
];
const stringGeometry = new THREE.BufferGeometry().setFromPoints(stringPoints);
scene.add(new THREE.Line(
stringGeometry,
new THREE.LineBasicMaterial({ color: 0x3d4038 })
));
const flagGeometry = new THREE.PlaneGeometry(0.62, 0.43);
const curve = new THREE.CatmullRomCurve3(stringPoints);
for (let i = 0; i < 10; i++) {
const t = (i + 0.5) / 10;
const point = curve.getPoint(t);
const material = new THREE.MeshStandardMaterial({
color: flagColors[i % flagColors.length],
roughness: 0.9,
side: THREE.DoubleSide
});
const flag = new THREE.Mesh(flagGeometry, material);
flag.position.copy(point);
flag.position.y -= 0.3;
flag.rotation.y = 0.08;
scene.add(flag);
flags.push({ mesh: flag, phase: i * 0.7 });
}
}
addPrayerFlags();
// Calculate the displayed average grades from the actual scene geometry.
let gentleHorizontalLength = 0;
let previous = pathCurve.getPoint(0);
for (let i = 1; i <= 300; i++) {
const current = pathCurve.getPoint(i / 300);
gentleHorizontalLength += Math.hypot(
current.x - previous.x,
current.z - previous.z
);
previous = current;
}
const routeRise = Math.abs(
terrainHeight(endXZ[0], endXZ[1]) -
terrainHeight(startXZ[0], startXZ[1])
);
const directHorizontalLength = Math.hypot(
endXZ[0] - startXZ[0],
endXZ[1] - startXZ[1]
);
const gentleGrade = Math.atan2(
routeRise,
gentleHorizontalLength
) * 180 / Math.PI;
const directGrade = Math.atan2(
routeRise,
directHorizontalLength
) * 180 / Math.PI;
document.getElementById("gentle-grade").textContent =
`${Math.round(gentleGrade)}°`;
document.getElementById("direct-grade").textContent =
`${Math.round(directGrade)}°`;
document.getElementById("gentle-pin-note").textContent =
`≈${Math.round(gentleGrade)}° average grade`;
document.getElementById("direct-pin-note").textContent =
`≈${Math.round(directGrade)}° average grade`;
// Direct camera controls: orbit, zoom and reset.
const target = new THREE.Vector3(0, 8.5, -2.5);
const view = {
radius: 51,
theta: 0.66,
phi: 1.12
};
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
target.x + view.radius * sinPhi * Math.sin(view.theta),
target.y + view.radius * Math.cos(view.phi),
target.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(target);
}
function resetView() {
view.radius = 51;
view.theta = 0.66;
view.phi = 1.12;
updateCamera();
}
updateCamera();
let pointer = null;
let hintTimer;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer = {
id: event.pointerId,
x: event.clientX,
y: event.clientY
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
hint.style.opacity = "0";
clearTimeout(hintTimer);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer || pointer.id !== event.pointerId) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
view.theta -= dx * 0.005;
view.phi = THREE.MathUtils.clamp(
view.phi - dy * 0.004,
0.48,
1.46
);
pointer.x = event.clientX;
pointer.y = event.clientY;
updateCamera();
});
function endPointer(event) {
if (!pointer || pointer.id !== event.pointerId) return;
pointer = null;
renderer.domElement.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
24,
80
);
updateCamera();
hint.style.opacity = "0";
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
resetButton.addEventListener("click", resetView);
gentleToggle.addEventListener("click", () => {
gentleGroup.visible = !gentleGroup.visible;
gentleToggle.classList.toggle("off", !gentleGroup.visible);
});
directToggle.addEventListener("click", () => {
directGroup.visible = !directGroup.visible;
directToggle.classList.toggle("off", !directGroup.visible);
});
window.addEventListener("keydown", (event) => {
if (event.key === "r" || event.key === "R") resetView();
});
// Screen-space labels.
const gentlePin = document.getElementById("gentle-pin");
const directPin = document.getElementById("direct-pin");
function placePin(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
let maxXPoint = pathCurve.getPoint(0);
for (let i = 1; i <= 100; i++) {
const point = pathCurve.getPoint(i / 100);
if (point.x > maxXPoint.x) maxXPoint = point;
}
const gentleAnchor = maxXPoint.clone();
gentleAnchor.y += 2.2;
const directAnchor = directPoint(0.5);
directAnchor.y += 2.2;
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
flags.forEach((flag, index) => {
flag.mesh.rotation.y =
0.08 + Math.sin(time * 2.1 + flag.phase) * 0.22;
flag.mesh.rotation.z =
Math.sin(time * 1.45 + index * 0.4) * 0.065;
});
placePin(gentlePin, gentleAnchor);
placePin(directPin, directAnchor);
renderer.render(scene, camera);
}
hintTimer = setTimeout(() => {
hint.style.opacity = "0";
}, 7000);
animate();
</script>
</body>
</html>
```