Prompt specification
Build or customize the Three.js 3D scene "Mountain route linking valleys and market towns" 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 route linking valleys and market towns
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/mountain-route-linking-valleys-and-market-towns-x2631
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing mountain trade along the Tea Horse Road. Build a schematic highland route with passes, river crossings, and staged settlements. Show pack transport adapting to narrow paths and label the selected historical geography. Voxel style.
Working Three.js Starter (mountain-route-linking-valleys-and-market-towns-x2631):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tea Horse Road — Highland Trade</title>
<style>
:root {
--ink: #25302d;
--muted: #69746d;
--paper: #f4efdf;
--panel: rgba(249, 246, 236, 0.94);
--line: rgba(44, 57, 51, 0.16);
--gold: #c77a32;
--gold-dark: #99571f;
--teal: #3f7f7c;
--blue: #477f9c;
--shadow: 0 18px 50px rgba(35, 43, 38, 0.17);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e1cf;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.map-label {
position: absolute;
transform: translate(-50%, -100%);
pointer-events: auto;
border: 1px solid rgba(41, 53, 47, 0.22);
border-radius: 7px;
padding: 6px 9px 6px 8px;
background: rgba(248, 244, 231, 0.91);
color: #26312d;
box-shadow: 0 5px 15px rgba(35, 43, 38, 0.13);
cursor: pointer;
white-space: nowrap;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.015em;
transition: transform 150ms ease, background 150ms ease, border-color 150ms ease;
}
.map-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -5px;
width: 8px;
height: 8px;
transform: translateX(-50%) rotate(45deg);
background: inherit;
border-right: 1px solid rgba(41, 53, 47, 0.22);
border-bottom: 1px solid rgba(41, 53, 47, 0.22);
}
.map-label:hover,
.map-label.selected {
transform: translate(-50%, -100%) scale(1.04);
background: #fff8e9;
border-color: rgba(176, 99, 35, 0.7);
}
.map-label .type-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 6px;
border-radius: 1px;
background: var(--gold);
vertical-align: 1px;
}
.map-label[data-type="pass"] .type-dot { background: #ad5b3f; }
.map-label[data-type="gorge"] .type-dot { background: var(--teal); }
.map-label[data-type="crossing"] .type-dot { background: var(--blue); }
.map-label .stage {
display: inline-block;
margin-right: 5px;
color: var(--gold-dark);
font-variant-numeric: tabular-nums;
}
.header {
position: fixed;
z-index: 10;
top: 24px;
left: 26px;
max-width: min(610px, calc(100vw - 410px));
pointer-events: none;
}
.eyebrow {
color: var(--gold-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 4px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(31px, 4vw, 57px);
line-height: 0.98;
font-weight: 500;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: #56615b;
font-size: 12px;
line-height: 1.5;
}
.header-rule {
width: 84px;
height: 3px;
margin-top: 14px;
background: var(--gold);
}
.legend {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: 218px;
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(248, 245, 234, 0.88);
backdrop-filter: blur(12px);
box-shadow: 0 10px 30px rgba(35, 43, 38, 0.09);
}
.legend-title {
margin-bottom: 10px;
color: #6c756e;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 7px 0;
color: #4b5750;
font-size: 11px;
}
.legend-swatch {
width: 16px;
height: 5px;
border-radius: 1px;
background: #d5a15b;
box-shadow: 0 2px 0 #a46d32;
}
.legend-swatch.pass {
width: 8px;
height: 8px;
background: #ad5b3f;
box-shadow: none;
transform: rotate(45deg);
}
.legend-swatch.gorge {
width: 8px;
height: 8px;
background: var(--teal);
box-shadow: none;
}
.legend-swatch.crossing {
width: 8px;
height: 8px;
background: var(--blue);
box-shadow: none;
}
.view-controls {
display: flex;
gap: 6px;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.view-controls button {
flex: 1;
padding: 7px 8px;
border: 1px solid rgba(45, 60, 52, 0.18);
border-radius: 6px;
background: rgba(255, 255, 255, 0.55);
color: #48554e;
cursor: pointer;
font-size: 10px;
font-weight: 750;
}
.view-controls button:hover {
border-color: rgba(176, 99, 35, 0.5);
color: var(--gold-dark);
}
.panel {
position: fixed;
z-index: 11;
top: 188px;
right: 24px;
width: 350px;
max-height: calc(100vh - 284px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(49, 61, 54, 0.16);
border-radius: 15px;
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: var(--shadow);
scrollbar-width: thin;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.panel-heading .eyebrow { font-size: 8px; }
#placeCategory {
padding: 4px 7px;
border-radius: 999px;
background: #e9dfc7;
color: #805426;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
#placeName {
margin: 8px 0 2px;
font-family: Georgia, "Times New Roman", serif;
font-size: 29px;
font-weight: 500;
line-height: 1.04;
letter-spacing: -0.025em;
}
#placeChinese {
color: #8a765a;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
}
.meta-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 15px 0 17px;
}
.meta {
padding: 9px 10px;
border: 1px solid rgba(48, 61, 53, 0.1);
border-radius: 7px;
background: rgba(255, 255, 255, 0.4);
}
.meta-label {
margin-bottom: 3px;
color: #879087;
font-size: 7px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.meta-value {
color: #3d4942;
font-size: 10px;
font-weight: 720;
line-height: 1.35;
}
.panel-section {
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.panel-section h3 {
display: flex;
align-items: center;
gap: 7px;
margin: 0 0 7px;
color: #4d5a53;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-section h3::before {
content: "";
width: 7px;
height: 7px;
background: var(--gold);
transform: rotate(45deg);
}
.panel-section p {
margin: 0;
color: #56615a;
font-size: 11px;
line-height: 1.58;
}
.adaptation {
padding: 11px 12px;
border-left: 3px solid var(--teal);
border-radius: 0 7px 7px 0;
background: rgba(63, 127, 124, 0.08);
}
.adaptation h3::before { background: var(--teal); }
.eras {
position: fixed;
z-index: 11;
left: 26px;
bottom: 91px;
display: flex;
align-items: center;
gap: 4px;
padding: 5px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(248, 245, 234, 0.85);
backdrop-filter: blur(12px);
}
.era-label {
padding: 0 8px 0 5px;
color: #788179;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.era-button {
padding: 7px 10px;
border: 0;
border-radius: 5px;
background: transparent;
color: #657068;
cursor: pointer;
font-size: 9px;
font-weight: 800;
}
.era-button.active {
background: var(--ink);
color: #f7f0df;
}
.stage-strip {
position: fixed;
z-index: 10;
left: 26px;
bottom: 24px;
display: flex;
align-items: center;
gap: 5px;
max-width: calc(100vw - 430px);
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(248, 245, 234, 0.86);
backdrop-filter: blur(12px);
overflow-x: auto;
scrollbar-width: none;
}
.stage-strip::-webkit-scrollbar { display: none; }
.stage-button {
position: relative;
flex: 0 0 auto;
padding: 7px 9px;
border: 0;
border-radius: 5px;
background: transparent;
color: #69746d;
cursor: pointer;
font-size: 9px;
font-weight: 750;
white-space: nowrap;
}
.stage-button:hover,
.stage-button.active {
background: #e7dcc4;
color: #7e4d1d;
}
.stage-button .number {
margin-right: 4px;
color: #aa7738;
font-variant-numeric: tabular-nums;
}
.hint {
margin-left: 8px;
padding-left: 10px;
border-left: 1px solid var(--line);
color: #858c85;
font-size: 8px;
white-space: nowrap;
}
#adaptBadge {
position: fixed;
z-index: 8;
transform: translate(-50%, -100%);
padding: 6px 9px;
border: 1px solid rgba(54, 73, 65, 0.2);
border-radius: 6px;
background: rgba(38, 54, 47, 0.88);
color: #fff7e8;
box-shadow: 0 8px 20px rgba(25, 35, 30, 0.2);
pointer-events: none;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
#adaptBadge::after {
content: "";
position: absolute;
left: 50%;
bottom: -5px;
width: 8px;
height: 8px;
transform: translateX(-50%) rotate(45deg);
background: #263630;
}
.caveat {
position: fixed;
z-index: 9;
right: 25px;
bottom: 23px;
max-width: 348px;
color: rgba(59, 70, 63, 0.72);
font-size: 8px;
line-height: 1.4;
text-align: right;
}
@media (max-width: 900px) {
.header {
top: 16px;
left: 17px;
max-width: calc(100vw - 34px);
}
h1 { font-size: 37px; }
.subtitle { max-width: 430px; }
.legend {
top: 15px;
right: 15px;
width: auto;
padding: 9px;
}
.legend-title,
.legend-row,
.view-controls { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 69px;
left: 12px;
width: auto;
max-height: 39vh;
padding: 15px;
}
#placeName { font-size: 23px; }
.meta-grid { margin: 10px 0; }
.stage-strip,
.eras { display: none; }
.caveat { display: none; }
.map-label { padding: 5px 7px; font-size: 9px; }
}
@media (max-height: 650px) and (min-width: 901px) {
.panel {
top: 165px;
max-height: calc(100vh - 235px);
}
.header h1 { font-size: 42px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel map of the Tea Horse Road"></canvas>
<div id="labels"></div>
<header class="header">
<div class="eyebrow">An interactive highland transect</div>
<h1>Tea Horse Road</h1>
<p class="subtitle">
Dali · Lijiang · Zhongdian · Batang · Chamdo · Lhasa<br />
Mountain exchange staged across basin roads, high passes, and river gorges.
</p>
<div class="header-rule"></div>
</header>
<section class="legend" aria-label="Map legend">
<div class="legend-title">Map key</div>
<div class="legend-row"><span class="legend-swatch"></span> Caravan route</div>
<div class="legend-row"><span class="legend-swatch pass"></span> Mountain pass</div>
<div class="legend-row"><span class="legend-swatch gorge"></span> River gorge</div>
<div class="legend-row"><span class="legend-swatch crossing"></span> River crossing</div>
<div class="view-controls">
<button id="zoomIn" aria-label="Zoom in">+</button>
<button id="zoomOut" aria-label="Zoom out">−</button>
<button id="resetView">Reset</button>
</div>
</section>
<aside class="panel" id="selectedPanel" aria-live="polite">
<div class="panel-heading">
<div class="eyebrow">Selected historical geography</div>
<div id="placeCategory">River gorge</div>
</div>
<h2 id="placeName">Tiger Leaping Gorge</h2>
<div id="placeChinese">虎跳峡</div>
<div class="meta-grid">
<div class="meta">
<div class="meta-label">Elevation / setting</div>
<div class="meta-value" id="placeAltitude">River ≈ 1,800 m · rim ≈ 2,700 m</div>
</div>
<div class="meta">
<div class="meta-label">Route role</div>
<div class="meta-value" id="placePeriod">Long-term gorge passage</div>
</div>
</div>
<div class="panel-section">
<h3>Historical geography</h3>
<p id="placeHistory"></p>
</div>
<div class="panel-section adaptation">
<h3>Pack transport adaptation</h3>
<p id="placeAdaptation"></p>
</div>
<div class="panel-section">
<h3>Historical setting</h3>
<p id="eraContext"></p>
</div>
</aside>
<nav class="eras" aria-label="Historical period">
<span class="era-label">Period</span>
<button class="era-button" data-era="tang">Tang</button>
<button class="era-button active" data-era="ming">Ming</button>
<button class="era-button" data-era="qing">Qing</button>
</nav>
<nav class="stage-strip" id="stageStrip" aria-label="Settlement stages"></nav>
<div id="adaptBadge">Paired load · wider relay track</div>
<div class="caveat">
Schematic cultural geography, not a surveyed itinerary. Elevations and stages are generalized.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* ------------------------------------------------------------------
DATA
------------------------------------------------------------------ */
const placeDefs = [
{
id: "dali",
name: "Dali",
chinese: "大理",
type: "settlement",
stage: 1,
t: 0.00,
altitude: "≈ 1,970 m · Erhai basin",
period: "Lowland market and consolidation point",
history:
"Dali stood in a broad basin where tea, lacquer, textiles, salt, and grain could be exchanged and repacked. The settlement functioned as a lowland gateway into the higher caravan corridors rather than as a single isolated stop.",
adaptation:
"Wide approach tracks allowed paired loads near the depots. Merchants reorganized freight here before mules, horses, and porters entered narrower mountain paths."
},
{
id: "jianchuan",
name: "Jianchuan",
chinese: "剑川",
type: "settlement",
stage: 2,
t: 0.14,
altitude: "≈ 2,200 m · valley relay town",
period: "Salt-market and relay economy",
history:
"Jianchuan's position among valley markets and routes toward the Cangshan uplands made it a useful place for smaller transactions, seasonal labor, repair, and the movement of salt and manufactured goods.",
adaptation:
"Teams could reorganize here. Smaller consignments replaced heavy loads before local guides selected the safest ledge or tributary crossing."
},
{
id: "lijiang",
name: "Lijiang",
chinese: "丽江",
type: "settlement",
stage: 3,
t: 0.28,
altitude: "≈ 2,400 m · mountain basin",
period: "Caravan market under Naxi and Tibetan authorities",
history:
"Lijiang became an important meeting place for caravan traffic, local cultivation, pastoral products, and textiles from the highlands. Its authority and markets connected southeastern Tibetan routes with Sichuan and Yunnan.",
adaptation:
"Relay depots and animal exchanges took place in the basin. Teams then climbed toward Yunling, where loads were divided and animals led in single file."
},
{
id: "yunling",
name: "Yunling Ledge",
chinese: "云岭山道",
type: "pass",
t: 0.35,
altitude: "≈ 2,800 m · narrow shelf track",
period: "Constrained mountain passage",
history:
"The route between Lijiang and the upper Mekong region crossed steep wooded slopes, where practical tracks shifted between ledges, side valleys, and high saddles.",
adaptation:
"Loads were split between animals and porters. Riders dismounted, animals moved single file, and inward-leaning packs reduced their chance of catching on rock or brush."
},
{
id: "tiger",
name: "Tiger Leaping Gorge",
chinese: "虎跳峡",
type: "gorge",
t: 0.42,
altitude: "River ≈ 1,800 m · rim ≈ 2,700 m",
period: "Jinsha River gorge passage",
history:
"The Jinsha River cuts between the Haba and Jade Dragon ranges. Rather than following one continuous valley road, caravans negotiated tributary tracks, steep rim shelves, and difficult crossings along the gorge system.",
adaptation:
"Teams went single file and cargo was divided. Animals were led rather than ridden, while inward-tilted loads and frequent halts reduced snagging and balance losses."
},
{
id: "zhongdian",
name: "Zhongdian",
chinese: "中甸 · Shangri-La",
type: "settlement",
stage: 4,
t: 0.57,
altitude: "≈ 3,280 m · plateau basin",
period: "Tibetan plateau exchange centre",
history:
"Zhongdian linked southeastern Tibetan pastoral regions with Yunnan and Sichuan. Markets served local communities as well as long-distance caravans moving cloth, tea, salt, grain, and animals.",
adaptation:
"High-altitude crews favored compact loads and steady animals. Seasonal weather and pasture made flexible scheduling as important as the road itself."
},
{
id: "batang",
name: "Batang",
chinese: "巴塘",
type: "settlement",
stage: 5,
t: 0.70,
altitude: "≈ 3,200 m · upper river valley",
period: "Tea–horse exchange station",
history:
"Batang occupied an important position on routes between Sichuan, the eastern Tibetan plateau, and farther west. Pack traffic connected local valley production with regional markets and monastery networks.",
adaptation:
"Animal trains and porters used different sections of the corridor. Freight was reassigned around difficult terrain, while fodder and repair facilities sustained relay journeys."
},
{
id: "chola",
name: "Chola High Pass",
chinese: "折多山口",
type: "pass",
t: 0.745,
altitude: "≈ 4,200 m · open saddle",
period: "Seasonal high crossing",
history:
"High passes around the Chola region connected the upper Dadu and eastern Tibetan drainage systems. Weather, snow, and pasture could determine whether a route remained practical.",
adaptation:
"Loads were lightened for steep gradients and animals moved in a close line. Drivers and porters used cold-weather equipment, spare rope, and carefully rationed fodder."
},
{
id: "chamdo",
name: "Chamdo",
chinese: "昌都",
type: "settlement",
stage: 6,
t: 0.84,
altitude: "≈ 3,200 m · eastern Tibet",
period: "Regional market and administrative centre",
history:
"Chamdo connected routes from Sichuan and the southeastern plateau with the upper Salween and Lancang systems. Its regional importance grew through trade, monasteries, and administration.",
adaptation:
"Large convoys could split into smaller mountain parties. River approaches were relatively wider, but nearby passes still required single-file movement and careful load balance."
},
{
id: "lhasa",
name: "Lhasa",
chinese: "拉萨",
type: "settlement",
stage: 7,
t: 1.00,
altitude: "≈ 3,650 m · plateau terminus",
period: "Long-distance market and trans-Himalayan hub",
history:
"Lhasa was both a destination and a redistribution point. Tea, cloth, metalware, grain, salt, and horses moved through a wider network of monasteries, markets, and regional roads.",
adaptation:
"Final approach roads were more consolidated, but highland conditions remained decisive. Teams often divided into smaller trains and purchased fresh animals before continuing west."
}
];
const eraFacts = {
tang: {
label: "Tang period · 618–907",
text:
"The mountain corridor belonged to a wider web of regional exchange before the later, highly organized tea-for-horse system. Basin markets, local pastoral routes, and imperial roads overlapped without forming one fixed itinerary."
},
ming: {
label: "Ming period · 1368–1644",
text:
"Official and household tea-for-horse exchange networks linked Sichuan tea with Tibetan demand for horses. Military administration and market regulation shaped some corridors, while many stages remained under local arrangement."
},
qing: {
label: "Qing period · 1644–1911",
text:
"Caravan traffic expanded through regulated markets and established relay points. Local communities still negotiated most daily movement, and narrow trails continued to determine how teams, loads, and animals were organized."
}
};
const state = {
selected: "tiger",
era: "ming",
yaw: -0.58,
elevation: 0.73,
zoom: 1,
dragging: false,
moved: 0,
lastX: 0,
lastY: 0
};
const target = new THREE.Vector3(0, 3.4, 0);
/* ------------------------------------------------------------------
THREE.JS SETUP
------------------------------------------------------------------ */
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e1cf);
scene.fog = new THREE.Fog(0xe8e1cf, 57, 110);
const camera = new THREE.OrthographicCamera(-30, 30, 20, -20, 0.1, 180);
const cameraDistance = 68;
scene.add(new THREE.HemisphereLight(0xfff6dd, 0x65776c, 2.0));
const sun = new THREE.DirectionalLight(0xffe5b8, 3.1);
sun.position.set(-24, 37, 25);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -31;
sun.shadow.camera.right = 31;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 2;
sun.shadow.camera.far = 95;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9c7c8, 0.75);
fill.position.set(28, 16, -24);
scene.add(fill);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const materials = {
base: new THREE.MeshStandardMaterial({ color: 0x66594a, roughness: 1 }),
edge: new THREE.MeshStandardMaterial({ color: 0x3f443d, roughness: 1 }),
route: new THREE.MeshStandardMaterial({ color: 0xd39b51, roughness: 0.86 }),
routeLight: new THREE.MeshStandardMaterial({ color: 0xf0c779, roughness: 0.82 }),
river: new THREE.MeshStandardMaterial({
color: 0x4f93a4,
emissive: 0x173f48,
emissiveIntensity: 0.25,
roughness: 0.34
}),
riverLight: new THREE.MeshStandardMaterial({
color: 0x9bcbd0,
emissive: 0x37666a,
emissiveIntensity: 0.2,
roughness: 0.45
}),
wood: new THREE.MeshStandardMaterial({ color: 0x75503a, roughness: 1 }),
woodLight: new THREE.MeshStandardMaterial({ color: 0xa47a51, roughness: 1 }),
wall: new THREE.MeshStandardMaterial({ color: 0xe5d9bd, roughness: 1 }),
wallWarm: new THREE.MeshStandardMaterial({ color: 0xc7b38f, roughness: 1 }),
roof: new THREE.MeshStandardMaterial({ color: 0x4d4b45, roughness: 1 }),
roofRed: new THREE.MeshStandardMaterial({ color: 0x874b36, roughness: 1 }),
gold: new THREE.MeshStandardMaterial({ color: 0xc88b35, roughness: 0.72 }),
stone: new THREE.MeshStandardMaterial({ color: 0x777c72, roughness: 1 }),
stoneLight: new THREE.MeshStandardMaterial({ color: 0xa5a292, roughness: 1 }),
settlement: new THREE.MeshStandardMaterial({ color: 0xc67a32, roughness: 0.8 }),
pass: new THREE.MeshStandardMaterial({ color: 0xa9583d, roughness: 0.85 }),
gorge: new THREE.MeshStandardMaterial({ color: 0x3f7f7c, roughness: 0.8 }),
crossing: new THREE.MeshStandardMaterial({ color: 0x477f9c, roughness: 0.8 }),
horse: new THREE.MeshStandardMaterial({ color: 0x77503b, roughness: 0.9 }),
horseLight: new THREE.MeshStandardMaterial({ color: 0xaa7651, roughness: 0.9 }),
yak: new THREE.MeshStandardMaterial({ color: 0x383c37, roughness: 1 }),
blanket: new THREE.MeshStandardMaterial({ color: 0xb34f3b, roughness: 0.9 }),
pack: new THREE.MeshStandardMaterial({ color: 0xc9954d, roughness: 1 }),
trunk: new THREE.MeshStandardMaterial({ color: 0x5b4a36, roughness: 1 }),
foliage: new THREE.MeshStandardMaterial({ color: 0x4f7455, roughness: 1 }),
foliageLight: new THREE.MeshStandardMaterial({ color: 0x65825b, roughness: 1 }),
hoof: new THREE.MeshStandardMaterial({ color: 0x34322d, roughness: 1 })
};
function makeBox(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
/* ------------------------------------------------------------------
ROUTE AND RIVER GEOMETRY
------------------------------------------------------------------ */
const routePoints = [
new THREE.Vector3(-18.0, 1.45, 9.5),
new THREE.Vector3(-11.0, 2.25, 6.2),
new THREE.Vector3(-5.5, 3.25, 2.8),
new THREE.Vector3(-0.7, 5.75, -1.4),
new THREE.Vector3(5.3, 6.35, -5.4),
new THREE.Vector3(11.2, 7.10, -8.1),
new THREE.Vector3(15.1, 7.65, -2.8),
new THREE.Vector3(18.2, 8.05, 6.2)
];
const routeCurve = new THREE.CatmullRomCurve3(routePoints, false, "catmullrom", 0.32);
const riverCurves = [
new THREE.CatmullRomCurve3([
new THREE.Vector3(-6.0, 1.05, -15.0),
new THREE.Vector3(-8.2, 1.40, -8.0),
new THREE.Vector3(-11.0, 1.86, 6.2),
new THREE.Vector3(-8.0, 2.12, 15.0)
], false, "catmullrom", 0.3),
new THREE.CatmullRomCurve3([
new THREE.Vector3(2.0, 1.20, -15.0),
new THREE.Vector3(1.2, 1.70, -8.0),
new THREE.Vector3(1.0, 2.18, -1.4),
new THREE.Vector3(4.0, 2.55, 5.0),
new THREE.Vector3(10.0, 2.85, 15.0)
], false, "catmullrom", 0.3)
];
const routeSamples = routeCurve.getSpacedPoints(420);
const riverSamples = riverCurves.map(curve => curve.getSpacedPoints(210));
function nearestOnSamples(samples, x, z) {
let bestDistance = Infinity;
let bestPoint = samples[0];
for (const sample of samples) {
const dx = sample.x - x;
const dz = sample.z - z;
const distance = dx * dx + dz * dz;
if (distance < bestDistance) {
bestDistance = distance;
bestPoint = sample;
}
}
return { distance: Math.sqrt(bestDistance), point: bestPoint };
}
function naturalHeight(x, z) {
const eastRise = Math.max(0, x + 21) * 0.088;
const northRise = Math.max(0, 14 - z) * 0.067;
const plateau = Math.max(0, x - 5) * 0.09;
const ridges =
Math.abs(Math.sin(x * 0.31 + z * 0.08)) * 0.72 +
Math.abs(Math.cos(z * 0.27 - x * 0.05)) * 0.46;
const tigerRidge =
1.7 * Math.exp(-((x + 2.2) ** 2 + (z + 3.2) ** 2) / 34);
return 0.78 + eastRise + northRise + plateau + ridges + tigerRidge;
}
function terrainHeightAt(x, z) {
const route = nearestOnSamples(routeSamples, x, z);
const blend = Math.exp(-(route.distance ** 2) / 12.5);
let height = THREE.MathUtils.lerp(
naturalHeight(x, z),
route.point.y,
blend * 0.94
);
for (const samples of riverSamples) {
const river = nearestOnSamples(samples, x, z);
if (river.distance < 1.25) {
height = Math.min(height, river.point.y - 0.28);
}
}
return Math.max(0.35, Math.round(height * 4) / 4);
}
function narrownessAt(point) {
const zones = [
{ x: -0.7, z: -1.4, radius: 5.4, strength: 1.0 },
{ x: 3.2, z: -3.4, radius: 4.3, strength: 0.88 },
{ x: 8.1, z: -6.8, radius: 4.8, strength: 0.90 }
];
let narrow = 0;
for (const zone of zones) {
const dx = point.x - zone.x;
const dz = point.z - zone.z;
const value =
zone.strength * Math.exp(-(dx * dx + dz * dz) / (zone.radius * zone.radius));
narrow = Math.max(narrow, value);
}
for (const place of placeDefs) {
if (place.type !== "settlement") continue;
const p = routeCurve.getPointAt(place.t);
const dx = point.x - p.x;
const dz = point.z - p.z;
narrow *= 1 - 0.82 * Math.exp(-(dx * dx + dz * dz) / 6.0);
}
return THREE.MathUtils.clamp(narrow, 0, 1);
}
/* ------------------------------------------------------------------
FLOATING VOXEL TERRAIN
------------------------------------------------------------------ */
const mapWidth = 44;
const mapDepth = 32;
const cellSize = 1;
const columnsX = 44;
const columnsZ = 32;
makeBox(45.2, 1.45, 33.2, materials.edge, 0, -0.88, 0);
makeBox(44.3, 0.68, 32.3, materials.base, 0, -0.06, 0);
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0,
flatShading: true
});
const terrain = new THREE.InstancedMesh(
unitBox,
terrainMaterial,
columnsX * columnsZ
);
terrain.castShadow = true;
terrain.receiveShadow = true;
scene.add(terrain);
const lowColor = new THREE.Color(0x718668);
const middleColor = new THREE.Color(0x95966f);
const highColor = new THREE.Color(0xa68a69);
const summitColor = new THREE.Color(0xcfc5ae);
const bankColor = new THREE.Color(0x69745e);
const tempColor = new THREE.Color();
let instanceIndex = 0;
for (let ix = 0; ix < columnsX; ix++) {
for (let iz = 0; iz < columnsZ; iz++) {
const x = -mapWidth / 2 + (ix + 0.5) * cellSize;
const z = -mapDepth / 2 + (iz + 0.5) * cellSize;
const height = terrainHeightAt(x, z);
dummy.position.set(x, height / 2, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.965, height, 0.965);
dummy.updateMatrix();
terrain.setMatrixAt(instanceIndex, dummy.matrix);
const variation =
(Math.sin(x * 12.9898 + z * 78.233) * 43758.5453) % 1;
const noise = Math.abs(variation);
if (height < 2.7) {
tempColor.copy(lowColor).lerp(middleColor, THREE.MathUtils.clamp((height - 1) / 2.1, 0, 1));
} else if (height < 5.3) {
tempColor.copy(middleColor).lerp(highColor, (height - 2.7) / 2.6);
} else {
tempColor.copy(highColor).lerp(summitColor, THREE.MathUtils.clamp((height - 5.3) / 3.1, 0, 1));
}
let nearestRiverDistance = Infinity;
for (const samples of riverSamples) {
nearestRiverDistance = Math.min(
nearestRiverDistance,
nearestOnSamples(samples, x, z).distance
);
}
if (nearestRiverDistance < 1.6) {
tempColor.lerp(bankColor, 0.55 * (1 - nearestRiverDistance / 1.6));
}
tempColor.offsetHSL(0, 0, (noise - 0.5) * 0.075);
terrain.setColorAt(instanceIndex, tempColor);
instanceIndex++;
}
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
/* ------------------------------------------------------------------
VOXEL RIVERS AND ROUTE TILES
------------------------------------------------------------------ */
function addVoxelRiver(curve, segmentCount) {
for (let i = 0; i < segmentCount; i++) {
const t = i / (segmentCount - 1);
const p = curve.getPointAt(t);
const tangent = curve.getTangentAt(t);
const yaw = Math.atan2(tangent.x, tangent.z);
const water = makeBox(
1.22,
0.12,
0.52,
i % 5 === 0 ? materials.riverLight : materials.river,
p.x,
p.y + 0.02,
p.z
);
water.rotation.y = yaw;
if (i % 9 === 0) {
const foam = makeBox(
0.30,
0.055,
0.18,
materials.riverLight,
p.x + Math.cos(yaw) * 0.28,
p.y + 0.10,
p.z - Math.sin(yaw) * 0.28
);
foam.rotation.y = yaw + 0.3;
}
}
}
addVoxelRiver(riverCurves[0], 155);
addVoxelRiver(riverCurves[1], 165);
const routeTileCount = 245;
for (let i = 0; i < routeTileCount; i++) {
const t = i / (routeTileCount - 1);
const p = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t);
const yaw = Math.atan2(tangent.x, tangent.z);
const narrow = narrownessAt(p);
const width = THREE.MathUtils.lerp(1.18, 0.46, narrow);
const tile = makeBox(
width,
0.12,
0.44,
i % 4 === 0 ? materials.routeLight : materials.route,
p.x,
p.y + 0.13,
p.z
);
tile.rotation.y = yaw;
}
// Small directional chevrons along the route.
for (let i = 0; i < 18; i++) {
const t = 0.035 + i * 0.052;
const p = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t);
const yaw = Math.atan2(tangent.x, tangent.z);
const group = new THREE.Group();
group.position.set(p.x, p.y + 0.23, p.z);
group.rotation.y = yaw;
scene.add(group);
const left = makeBox(0.12, 0.08, 0.46, materials.gold, -0.15, 0, 0, group);
const right = makeBox(0.12, 0.08, 0.46, materials.gold, 0.15, 0, 0, group);
left.rotation.y = -0.72;
right.rotation.y = 0.72;
}
/* ------------------------------------------------------------------
CROSSING BRIDGE AND VOXEL VEGETATION
------------------------------------------------------------------ */
function addBridge() {
const bridgePoint = routeCurve.getPointAt(0.14);
const riverPoint = riverCurves[0].getPointAt(0.62);
const tangent = riverCurves[0].getTangentAt(0.62);
const yaw = Math.atan2(-tangent.z, tangent.x);
const group = new THREE.Group();
group.position.set(bridgePoint.x, bridgePoint.y + 0.09, bridgePoint.z);
group.rotation.y = yaw;
scene.add(group);
for (let i = -4; i <= 4; i++) {
makeBox(0.34, 0.13, 1.14, materials.woodLight, i * 0.34, 0, 0, group);
}
makeBox(3.3, 0.16, 0.18, materials.wood, 0, 0.25, -0.53, group);
makeBox(3.3, 0.16, 0.18, materials.wood, 0, 0.25, 0.53, group);
for (const x of [-1.25, 0, 1.25]) {
makeBox(0.16, 1.0, 0.16, materials.wood, x, -0.46, -0.48, group);
makeBox(0.16, 1.0, 0.16, materials.wood, x, -0.46, 0.48, group);
}
}
addBridge();
let randomSeed = 9187;
function seededRandom() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const treePositions = [];
for (let attempt = 0; attempt < 350 && treePositions.length < 120; attempt++) {
const x = (seededRandom() - 0.5) * 42.5;
const z = (seededRandom() - 0.5) * 30.5;
const height = terrainHeightAt(x, z);
const routeDistance = nearestOnSamples(routeSamples, x, z).distance;
let riverDistance = Infinity;
for (const samples of riverSamples) {
riverDistance = Math.min(
riverDistance,
nearestOnSamples(samples, x, z).distance
);
}
if (height < 1.2 || height > 5.2) continue;
if (routeDistance < 2.0 || riverDistance < 1.8) continue;
treePositions.push({ x, z, height });
}
const trunks = new THREE.InstancedMesh(unitBox, materials.trunk, treePositions.length);
const crowns = new THREE.InstancedMesh(unitBox, materials.foliage, treePositions.length);
const crownTips = new THREE.InstancedMesh(unitBox, materials.foliageLight, treePositions.length);
trunks.castShadow = true;
crowns.castShadow = true;
crownTips.castShadow = true;
trunks.receiveShadow = true;
treePositions.forEach((tree, index) => {
dummy.position.set(tree.x, tree.height + 0.27, tree.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.18, 0.55, 0.18);
dummy.updateMatrix();
trunks.setMatrixAt(index, dummy.matrix);
dummy.position.set(tree.x, tree.height + 0.72, tree.z);
dummy.scale.set(0.62, 0.63, 0.62);
dummy.updateMatrix();
crowns.setMatrixAt(index, dummy.matrix);
dummy.position.set(tree.x, tree.height + 1.18, tree.z);
dummy.scale.set(0.38, 0.58, 0.38);
dummy.updateMatrix();
crownTips.setMatrixAt(index, dummy.matrix);
});
scene.add(trunks, crowns, crownTips);
/* ------------------------------------------------------------------
PLACES, SETTLEMENTS, PASSES, MARKERS, LABELS
------------------------------------------------------------------ */
const markerRoots = [];
const markerHeads = [];
const places = new Map();
const labelsLayer = document.querySelector("#labels");
const markerMaterials = {
settlement: materials.settlement,
pass: materials.pass,
gorge: materials.gorge,
crossing: materials.crossing
};
function addMarker(place, point, tangent) {
const group = new THREE.Group();
group.position.copy(point);
group.userData.placeId = place.id;
scene.add(group);
makeBox(0.10, 0.78, 0.10, materials.wood, 0, 0.38, 0, group);
const head = makeBox(
0.43,
0.43,
0.43,
markerMaterials[place.type],
0,
0.95,
0,
group
);
head.rotation.y = Math.PI * 0.25;
head.userData.placeId = place.id;
markerRoots.push(group);
markerHeads.push(head);
return group;
}
function addSettlement(place, point, tangent) {
const side = new THREE.Vector3(tangent.z, 0, -tangent.x).normalize();
for (let i = 0; i < 7; i++) {
const along = (i - 3) * 0.73;
const lateral = 2.15 + (i % 2) * 0.72;
const position = point.clone()
.addScaledVector(tangent, along)
.addScaledVector(side, lateral);
const ground = terrainHeightAt(position.x, position.z);
const width = 0.62 + (i % 3) * 0.12;
const depth = 0.66 + ((i + 1) % 3) * 0.11;
const height = 0.65 + (i % 2) * 0.28;
const building = new THREE.Group();
building.position.set(position.x, ground, position.z);
building.rotation.y = Math.atan2(tangent.x, tangent.z) + (i % 2 ? 0.12 : -0.08);
scene.add(building);
makeBox(
width,
height,
depth,
i % 3 === 0 ? materials.wallWarm : materials.wall,
0,
height / 2,
0,
building
);
makeBox(
width + 0.20,
0.13,
depth + 0.20,
i % 4 === 0 ? materials.roofRed : materials.roof,
0,
height + 0.07,
0,
building
);
makeBox(
width * 0.55,
0.10,
depth * 0.55,
place.id === "lhasa" ? materials.gold : materials.woodLight,
0,
height + 0.18,
0,
building
);
if (place.id === "lhasa" && i === 3) {
makeBox(0.34, 1.2, 0.34, materials.gold, 0, height + 0.68, 0, building);
makeBox(0.72, 0.18, 0.72, materials.gold, 0, height + 1.3, 0, building);
}
}
// Courtyard walls suggest a staged market settlement.
const wallCenter = point.clone().addScaledVector(side, 2.7);
const courtyard = makeBox(
3.5,
0.24,
0.18,
materials.wallWarm,
wallCenter.x,
terrainHeightAt(wallCenter.x, wallCenter.z) + 0.12,
wallCenter.z
);
courtyard.rotation.y = Math.atan2(tangent.x, tangent.z);
}
function addPass(place, point, tangent) {
const side = new THREE.Vector3(tangent.z, 0, -tangent.x).normalize();
for (let i = 0; i < 4; i++) {
const p = point.clone().addScaledVector(side, (i - 1.5) * 0.58);
const base = terrainHeightAt(p.x, p.z);
const stackHeight = 0.32 + (i % 2) * 0.25;
makeBox(
0.48,
stackHeight,
0.48,
i % 2 ? materials.stoneLight : materials.stone,
p.x,
base + stackHeight / 2,
p.z
);
}
}
function addCrossingMarker(place, point, tangent) {
const side = new THREE.Vector3(tangent.z, 0, -tangent.x).normalize();
const p = point.clone().addScaledVector(side, 1.05);
const ground = terrainHeightAt(p.x, p.z);
makeBox(0.13, 1.1, 0.13, materials.wood, p.x, ground + 0.55, p.z);
makeBox(0.68, 0.42, 0.12, materials.crossing, p.x, ground + 1.05, p.z);
}
function createLabel(place, anchor) {
const label = document.createElement("button");
label.className = "map-label";
label.dataset.type = place.type;
label.dataset.placeId = place.id;
label.setAttribute("aria-label", `Select ${place.name}`);
const stage = place.stage
? `<span class="stage">${String(place.stage).padStart(2, "0")}</span>`
: "";
label.innerHTML = `${stage}<span class="type-dot"></span>${place.name}`;
label.addEventListener("click", event => {
event.stopPropagation();
selectPlace(place.id);
});
labelsLayer.appendChild(label);
place.label = label;
place.anchor = anchor;
}
for (const definition of placeDefs) {
const place = { ...definition };
const point = routeCurve.getPointAt(place.t);
const tangent = routeCurve.getTangentAt(place.t).normalize();
places.set(place.id, place);
addMarker(place, point, tangent);
if (place.type === "settlement") {
addSettlement(place, point, tangent);
} else if (place.type === "pass") {
addPass(place, point, tangent);
} else if (place.type === "crossing") {
addCrossingMarker(place, point, tangent);
}
let labelAnchor = point.clone().add(new THREE.Vector3(0, 2.05, 0));
if (place.type === "settlement") {
const side = new THREE.Vector3(tangent.z, 0, -tangent.x).normalize();
labelAnchor = point.clone().addScaledVector(side, 1.7).add(new THREE.Vector3(0, 2.65, 0));
}
createLabel(place, labelAnchor);
}
/* ------------------------------------------------------------------
SELECTION FRAME AND INFORMATION PANEL
------------------------------------------------------------------ */
const selectionFrame = new THREE.Group();
const selectionMaterial = new THREE.MeshBasicMaterial({ color: 0xf0a43f });
makeBox(1.6, 0.07, 0.08, selectionMaterial, 0, 0, -0.76, selectionFrame);
makeBox(1.6, 0.07, 0.08, selectionMaterial, 0, 0, 0.76, selectionFrame);
makeBox(0.08, 0.07, 1.44, selectionMaterial, -0.76, 0, 0, selectionFrame);
makeBox(0.08, 0.07, 1.44, selectionMaterial, 0.76, 0, 0, selectionFrame);
scene.add(selectionFrame);
const panel = {
category: document.querySelector("#placeCategory"),
name: document.querySelector("#placeName"),
chinese: document.querySelector("#placeChinese"),
altitude: document.querySelector("#placeAltitude"),
period: document.querySelector("#placePeriod"),
history: document.querySelector("#placeHistory"),
adaptation: document.querySelector("#placeAdaptation"),
era: document.querySelector("#eraContext")
};
function selectPlace(id) {
if (!places.has(id)) return;
state.selected = id;
const place = places.get(id);
panel.category.textContent = {
settlement: "Settlement",
pass: "Mountain pass",
gorge: "River gorge",
crossing: "River crossing"
}[place.type];
panel.name.textContent = place.name;
panel.chinese.textContent = place.chinese;
panel.altitude.textContent = place.altitude;
panel.period.textContent = place.period;
panel.history.textContent = place.history;
panel.adaptation.textContent = place.adaptation;
panel.era.textContent = `${eraFacts[state.era].label}. ${eraFacts[state.era].text}`;
for (const item of places.values()) {
item.label?.classList.toggle("selected", item.id === id);
}
for (const button of document.querySelectorAll(".stage-button")) {
button.classList.toggle("active", button.dataset.placeId === id);
}
const point = routeCurve.getPointAt(place.t);
selectionFrame.position.set(point.x, point.y + 0.23, point.z);
}
const stageStrip = document.querySelector("#stageStrip");
for (const place of placeDefs.filter(item => item.type === "settlement")) {
const button = document.createElement("button");
button.className = "stage-button";
button.dataset.placeId = place.id;
button.innerHTML = `<span class="number">${String(place.stage).padStart(2, "0")}</span>${place.name}`;
button.addEventListener("click", () => selectPlace(place.id));
stageStrip.appendChild(button);
}
const hint = document.createElement("span");
hint.className = "hint";
hint.textContent = "Drag: orbit · Shift/right-drag: pan · Wheel: zoom · Click: inspect";
stageStrip.appendChild(hint);
document.querySelectorAll(".era-button").forEach(button => {
button.addEventListener("click", () => {
state.era = button.dataset.era;
document.querySelectorAll(".era-button").forEach(item => {
item.classList.toggle("active", item === button);
});
selectPlace(state.selected);
});
});
/* ------------------------------------------------------------------
ADAPTING PACK TRAIN
------------------------------------------------------------------ */
function createPackAnimal(index) {
const group = new THREE.Group();
const isYak = index === 1;
const bodyMaterial = isYak
? materials.yak
: (index === 0 ? materials.horse : materials.horseLight);
const bodyWidth = isYak ? 0.82 : 0.62;
const bodyHeight = isYak ? 0.62 : 0.50;
const bodyLength = isYak ? 1.08 : 1.18;
makeBox(bodyWidth, bodyHeight, bodyLength, bodyMaterial, 0, 1.02, 0, group);
makeBox(0.43, 0.46, 0.43, bodyMaterial, 0, 1.30, 0.73, group);
makeBox(0.36, 0.24, 0.45, materials.hoof, 0, 1.18, 1.00, group);
if (isYak) {
makeBox(0.12, 0.10, 0.40, materials.stoneLight, -0.31, 1.55, 0.70, group);
makeBox(0.12, 0.10, 0.40, materials.stoneLight, 0.31, 1.55, 0.70, group);
} else {
makeBox(0.10, 0.25, 0.10, bodyMaterial, -0.14, 1.58, 0.66, group);
makeBox(0.10, 0.25, 0.10, bodyMaterial, 0.14, 1.58, 0.66, group);
}
const legs = [];
for (const x of [-bodyWidth * 0.31, bodyWidth * 0.31]) {
for (const z of [-bodyLength * 0.34, bodyLength * 0.34]) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.82, z);
group.add(pivot);
makeBox(0.12, 0.70, 0.13, bodyMaterial, 0, -0.35, 0, pivot);
makeBox(0.15, 0.12, 0.23, materials.hoof, 0, -0.70, 0.05, pivot);
legs.push(pivot);
}
}
const cargoPivot = new THREE.Group();
cargoPivot.position.set(0, 1.08, -0.14);
group.add(cargoPivot);
makeBox(bodyWidth + 0.16, 0.09, 0.72, materials.blanket, 0, 0.15, 0, cargoPivot);
makeBox(bodyWidth * 0.86, 0.58, 0.57, materials.pack, 0, 0.48, 0, cargoPivot);
makeBox(bodyWidth * 0.95, 0.08, 0.62, materials.woodLight, 0, 0.49, 0, cargoPivot);
const tail = makeBox(0.09, 0.44, 0.09, materials.hoof, 0, 1.00, -bodyLength * 0.58, group);
tail.rotation.x = -0.28;
group.userData.legs = legs;
group.userData.cargoPivot = cargoPivot;
scene.add(group);
return group;
}
const packAnimals = [createPackAnimal(0), createPackAnimal(1), createPackAnimal(2)];
const ropePositions = new Float32Array(3 * 3);
const ropeGeometry = new THREE.BufferGeometry();
ropeGeometry.setAttribute("position", new THREE.BufferAttribute(ropePositions, 3));
const rope = new THREE.Line(
ropeGeometry,
new THREE.LineBasicMaterial({ color: 0x5a4938 })
);
rope.frustumCulled = false;
scene.add(rope);
const adaptBadge = document.querySelector("#adaptBadge");
/* ------------------------------------------------------------------
CAMERA CONTROLS — implemented directly, without addons
------------------------------------------------------------------ */
function updateCamera() {
const horizontal = Math.cos(state.elevation) * cameraDistance;
camera.position.set(
target.x + Math.sin(state.yaw) * horizontal,
target.y + Math.sin(state.elevation) * cameraDistance,
target.z + Math.cos(state.yaw) * horizontal
);
camera.lookAt(target);
const aspect = window.innerWidth / window.innerHeight;
const height = 44 / state.zoom;
camera.left = -height * aspect / 2;
camera.right = height * aspect / 2;
camera.top = height / 2;
camera.bottom = -height / 2;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resetView() {
state.yaw = -0.58;
state.elevation = 0.73;
state.zoom = 1;
target.set(0, 3.4, 0);
updateCamera();
}
document.querySelector("#resetView").addEventListener("click", resetView);
document.querySelector("#zoomIn").addEventListener("click", () => {
state.zoom = Math.min(2.1, state.zoom * 1.16);
updateCamera();
});
document.querySelector("#zoomOut").addEventListener("click", () => {
state.zoom = Math.max(0.62, state.zoom / 1.16);
updateCamera();
});
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
state.dragging = true;
state.moved = 0;
state.lastX = event.clientX;
state.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!state.dragging) {
updateCursor(event);
return;
}
const dx = event.clientX - state.lastX;
const dy = event.clientY - state.lastY;
state.lastX = event.clientX;
state.lastY = event.clientY;
state.moved += Math.abs(dx) + Math.abs(dy);
if (event.shiftKey || event.button === 2) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const scale = 44 / state.zoom / 700;
target.addScaledVector(right, -dx * scale);
target.addScaledVector(forward, -dy * scale);
} else {
state.yaw -= dx * 0.006;
state.elevation = THREE.MathUtils.clamp(
state.elevation + dy * 0.005,
0.38,
1.24
);
}
updateCamera();
});
canvas.addEventListener("pointerup", event => {
if (!state.dragging) return;
state.dragging = false;
if (state.moved < 6 && event.button === 0) {
selectFromPointer(event);
}
});
canvas.addEventListener("pointercancel", () => {
state.dragging = false;
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
state.zoom = THREE.MathUtils.clamp(
state.zoom * (event.deltaY > 0 ? 0.91 : 1.10),
0.62,
2.1
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") resetView();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickPlace(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(markerRoots, true);
for (const intersection of intersections) {
let object = intersection.object;
while (object && !object.userData.placeId) object = object.parent;
if (object?.userData.placeId) return object.userData.placeId;
}
return null;
}
function selectFromPointer(event) {
const id = pickPlace(event);
if (id) selectPlace(id);
}
function updateCursor(event) {
const id = pickPlace(event);
canvas.style.cursor = id ? "pointer" : "grab";
}
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
/* ------------------------------------------------------------------
PROJECT LABELS
------------------------------------------------------------------ */
const projected = new THREE.Vector3();
function updateLabels() {
for (const place of places.values()) {
if (!place.label) continue;
projected.copy(place.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.1 &&
projected.y < 1.1;
place.label.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
place.label.style.left = `${x}px`;
place.label.style.top = `${y}px`;
}
}
}
/* ------------------------------------------------------------------
ANIMATION
------------------------------------------------------------------ */
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
// Pack train advances from Dali toward Lhasa.
const leadT = 0.055 + (elapsed * 0.016) % 0.89;
const leadPoint = routeCurve.getPointAt(leadT);
const leadNarrow = narrownessAt(leadPoint);
adaptBadge.textContent = leadNarrow > 0.53
? "Single file · packs tilted inboard"
: "Paired load · wider relay track";
const animalPositions = [];
packAnimals.forEach((animal, index) => {
const sampleT = routeCurve.getPointAt(leadT);
const narrow = narrownessAt(sampleT);
const spacing = THREE.MathUtils.lerp(0.073, 0.037, narrow);
const t = (leadT - index * spacing + 1) % 1;
const point = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t);
const horizontalTangent = new THREE.Vector3(tangent.x, 0, tangent.z).normalize();
const side = new THREE.Vector3(horizontalTangent.z, 0, -horizontalTangent.x);
const pairOffset = index === 1 ? 0.33 : index === 2 ? -0.16 : 0;
const lateral = pairOffset * (1 - narrow);
animal.position.copy(point)
.addScaledVector(side, lateral);
animal.position.y += 0.15;
animal.rotation.y = Math.atan2(horizontalTangent.x, horizontalTangent.z);
const gait = elapsed * 8.2 + index * 1.7;
animal.userData.legs.forEach((leg, legIndex) => {
const alternating = legIndex % 2 === 0 ? 1 : -1;
leg.rotation.x = Math.sin(gait + (legIndex > 1 ? Math.PI : 0)) *
0.34 * alternating * (1 - narrow * 0.25);
});
const cargo = animal.userData.cargoPivot;
cargo.rotation.z = -Math.sign(lateral || 1) * narrow * 0.15;
cargo.rotation.x = Math.sin(gait * 0.5) * 0.015;
animalPositions.push(animal.position.clone().add(new THREE.Vector3(0, 1.0, 0)));
});
for (let i = 0; i < animalPositions.length - 1; i++) {
ropePositions[i * 3] = animalPositions[i].x;
ropePositions[i * 3 + 1] = animalPositions[i].y;
ropePositions[i * 3 + 2] = animalPositions[i].z;
}
ropeGeometry.attributes.position.needsUpdate = true;
projected.copy(animalPositions[0]).add(new THREE.Vector3(0, 1.75, 0)).project(camera);
const badgeX = (projected.x * 0.5 + 0.5) * window.innerWidth;
const badgeY = (-projected.y * 0.5 + 0.5) * window.innerHeight;
adaptBadge.style.left = `${badgeX}px`;
adaptBadge.style.top = `${badgeY}px`;
const selected = places.get(state.selected);
const selectedPoint = routeCurve.getPointAt(selected.t);
selectionFrame.position.set(selectedPoint.x, selectedPoint.y + 0.24, selectedPoint.z);
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.055;
selectionFrame.scale.set(pulse, 1, pulse);
markerHeads.forEach((head, index) => {
head.position.y = 0.95 + Math.sin(elapsed * 2.1 + index * 0.7) * 0.045;
});
updateCamera();
renderer.render(scene, camera);
updateLabels();
}
selectPlace("tiger");
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```