Prompt specification
Build or customize the Three.js 3D scene "Northern river route with an interrupted connection" 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: Northern river route with an interrupted connection
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/northern-river-route-with-an-interrupted-connection-x2639
Scene Description Prompt:
Move cargo and vessels across a low-poly Viking-era river network's overland portage, with visibly compressed landscape scale. Three.js from cdnjs, one HTML file.
Working Three.js Starter (northern-river-route-with-an-interrupted-connection-x2639):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Short Haul — Viking Portage</title>
<style>
:root {
--ink: #172823;
--panel: rgba(22, 37, 31, 0.88);
--panel-light: rgba(235, 222, 188, 0.92);
--gold: #d6b56d;
--cream: #eee4c7;
--green: #8ba881;
--muted: #aeb9aa;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8cfca;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--cream);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#overlay {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid rgba(230, 218, 180, 0.2);
box-shadow: 0 14px 40px rgba(20, 31, 27, 0.22);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title-panel {
position: absolute;
top: 22px;
left: 22px;
width: 335px;
padding: 20px 21px 18px;
border-radius: 4px 18px 4px 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 1px;
background: var(--gold);
}
h1 {
margin: 10px 0 6px;
font-family: Georgia, "Times New Roman", serif;
font-size: 35px;
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 17px;
color: #c7d0c1;
font-size: 12px;
line-height: 1.55;
}
.badges {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.badge {
padding: 6px 8px;
border: 1px solid rgba(214, 181, 109, 0.36);
border-radius: 2px;
color: #e4d8b8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#manifest {
position: absolute;
top: 22px;
right: 22px;
width: 280px;
padding: 18px;
border-radius: 16px 4px 16px 4px;
}
.manifest-title {
margin-bottom: 16px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.job {
margin-top: 15px;
}
.job-heading {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
font-size: 11px;
}
.job-heading strong {
color: #f0e6c9;
font-size: 12px;
}
.job-heading span {
color: var(--muted);
font-variant-numeric: tabular-nums;
font-size: 10px;
}
.track {
height: 5px;
overflow: hidden;
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
}
.fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #789b72, #d6b56d);
transition: width 0.15s linear;
}
#vessel-fill {
background: linear-gradient(90deg, #658d98, #9fc8c5);
}
.route-note {
display: flex;
justify-content: space-between;
margin-top: 16px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.route-note b {
color: var(--cream);
font-weight: 650;
}
#toolbar {
position: absolute;
left: 50%;
bottom: 24px;
display: flex;
gap: 8px;
padding: 8px;
transform: translateX(-50%);
border-radius: 15px;
}
button {
border: 0;
border-radius: 9px;
padding: 12px 16px;
background: rgba(255, 255, 255, 0.07);
color: var(--cream);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
transition:
transform 0.16s ease,
background 0.16s ease,
color 0.16s ease;
white-space: nowrap;
}
button:hover {
transform: translateY(-2px);
background: rgba(255, 255, 255, 0.13);
}
button.active {
background: var(--gold);
color: var(--ink);
}
button.primary {
background: var(--cream);
color: var(--ink);
}
button.primary:hover {
background: #fff4d5;
}
.button-dot {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
border-radius: 50%;
background: currentColor;
opacity: 0.75;
}
#help {
position: absolute;
right: 22px;
bottom: 24px;
padding: 10px 12px;
border-radius: 8px;
color: rgba(237, 229, 202, 0.75);
background: rgba(22, 37, 31, 0.68);
font-size: 9px;
line-height: 1.6;
letter-spacing: 0.04em;
}
#toast {
position: absolute;
left: 50%;
top: 28px;
padding: 10px 17px;
transform: translate(-50%, -12px);
border: 1px solid rgba(214, 181, 109, 0.38);
border-radius: 30px;
background: rgba(22, 37, 31, 0.9);
color: var(--cream);
font-size: 11px;
opacity: 0;
transition:
opacity 0.25s ease,
transform 0.25s ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.world-label {
position: absolute;
padding: 5px 8px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 3px;
background: rgba(25, 44, 39, 0.76);
color: #f1e8ce;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15);
}
.world-label.portage {
border-color: rgba(214, 181, 109, 0.6);
color: #f2d58e;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #b8cfca;
color: #23372f;
font-family: Georgia, serif;
font-size: 22px;
transition: opacity 0.5s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 800px) {
#title-panel {
top: 12px;
left: 12px;
width: 280px;
padding: 15px;
}
h1 {
font-size: 28px;
}
.subtitle {
display: none;
}
#manifest {
top: 12px;
right: 12px;
width: 215px;
padding: 13px;
}
.route-note,
#help {
display: none;
}
#toolbar {
bottom: 12px;
width: calc(100% - 24px);
justify-content: center;
}
button {
padding: 11px 12px;
font-size: 10px;
}
}
@media (max-width: 540px) {
#manifest {
display: none;
}
#title-panel {
width: calc(100% - 24px);
}
#toolbar {
flex-wrap: wrap;
}
}
</style>
</head>
<body>
<div id="loading">Surveying the river…</div>
<div id="overlay">
<section id="title-panel" class="panel">
<div class="eyebrow">Northwater Expedition · 872</div>
<h1>The Short Haul</h1>
<p class="subtitle">
A branching fjord, a narrow carry, and a vessel too heavy for the road.
Move the cargo and portage the ship between reaches.
</p>
<div class="badges">
<span class="badge">Low-poly fjord</span>
<span class="badge">Compressed scale</span>
<span class="badge">Active transport</span>
</div>
</section>
<aside id="manifest" class="panel">
<div class="manifest-title">Route Manifest</div>
<div class="job">
<div class="job-heading">
<strong>Cargo crew</strong>
<span id="cargo-status">At lower landing</span>
</div>
<div class="track"><div id="cargo-fill" class="fill"></div></div>
</div>
<div class="job">
<div class="job-heading">
<strong>Viking vessel</strong>
<span id="vessel-status">Lower reach</span>
</div>
<div class="track"><div id="vessel-fill" class="fill"></div></div>
</div>
<div class="route-note">
<span>Portage distance<br><b id="distance">48 m</b></span>
<span>Carry capacity<br><b>6 loads</b></span>
</div>
</aside>
<div id="toolbar" class="panel">
<button id="cargo-button" class="active">
<span class="button-dot"></span><span id="cargo-button-label">Cargo running</span>
</button>
<button id="vessel-button" class="active">
<span class="button-dot"></span><span id="vessel-button-label">Vessel running</span>
</button>
<button id="play-button" class="primary">Pause route</button>
<button id="reset-button">Reset</button>
</div>
<div id="help">
DRAG to orbit · SHIFT/RIGHT-DRAG to pan · SCROLL to zoom<br>
CLICK the crew or vessel to follow it
</div>
<div id="toast"></div>
<div id="label-lower" class="world-label">Lower Reach</div>
<div id="label-portage" class="world-label portage">Portage · Overland</div>
<div id="label-upper" class="world-label">Upper Reach</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Application state
// ------------------------------------------------------------
const state = {
playing: true,
cargoActive: true,
vesselActive: true,
cargoProgress: 0.035,
vesselProgress: 0.025,
cargoDone: false,
vesselDone: false
};
let seed = 872;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function range(a, b) {
return a + (b - a) * random();
}
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
// ------------------------------------------------------------
// Renderer, scene, lighting
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb8cfca);
scene.fog = new THREE.Fog(0xb8cfca, 48, 88);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const hemi = new THREE.HemisphereLight(0xd9ebe3, 0x586348, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe9bc, 3.2);
sun.position.set(-19, 31, 15);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -30;
sun.shadow.camera.right = 30;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9cc8d0, 0.75);
fill.position.set(22, 12, -20);
scene.add(fill);
// ------------------------------------------------------------
// Custom camera controls: no examples/addons are imported
// ------------------------------------------------------------
class CameraRig {
constructor(camera) {
this.camera = camera;
this.target = new THREE.Vector3(0, 0.4, -1);
this.focusTarget = this.target.clone();
this.radius = 42;
this.theta = 0.67;
this.phi = 0.82;
this.pointer = null;
this.dragged = 0;
this.canvas = renderer.domElement;
this.canvas.addEventListener("pointerdown", (event) => {
this.pointer = {
id: event.pointerId,
x: event.clientX,
y: event.clientY,
button: event.button,
mode: event.button === 2 || event.shiftKey ? "pan" : "orbit"
};
this.dragged = 0;
this.canvas.setPointerCapture(event.pointerId);
});
this.canvas.addEventListener("pointermove", (event) => {
if (!this.pointer || event.pointerId !== this.pointer.id) return;
const dx = event.clientX - this.pointer.x;
const dy = event.clientY - this.pointer.y;
this.pointer.x = event.clientX;
this.pointer.y = event.clientY;
this.dragged += Math.abs(dx) + Math.abs(dy);
if (this.pointer.mode === "orbit") {
this.theta -= dx * 0.005;
this.phi = clamp(this.phi - dy * 0.004, 0.34, 1.28);
} else {
const scale = this.radius * 0.00125;
const right = new THREE.Vector3();
const forward = new THREE.Vector3();
this.camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
right.crossVectors(forward, this.camera.up).normalize();
this.focusTarget.addScaledVector(right, -dx * scale);
this.focusTarget.addScaledVector(forward, -dy * scale);
this.focusTarget.x = clamp(this.focusTarget.x, -22, 22);
this.focusTarget.z = clamp(this.focusTarget.z, -17, 17);
}
});
this.canvas.addEventListener("pointerup", (event) => {
if (this.pointer?.id === event.pointerId) {
this.canvas.releasePointerCapture(event.pointerId);
}
this.pointer = null;
});
this.canvas.addEventListener("pointercancel", () => {
this.pointer = null;
});
this.canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
this.radius = clamp(
this.radius * Math.exp(event.deltaY * 0.001),
13,
68
);
},
{ passive: false }
);
this.canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
this.update(0);
}
focus(position) {
this.focusTarget.copy(position);
this.focusTarget.y += 0.4;
}
update(dt) {
this.target.lerp(this.focusTarget, 1 - Math.exp(-dt * 4.5));
const sinPhi = Math.sin(this.phi);
const x =
this.target.x +
this.radius * sinPhi * Math.sin(this.theta);
const y = this.target.y + this.radius * Math.cos(this.phi);
const z =
this.target.z +
this.radius * sinPhi * Math.cos(this.theta);
this.camera.position.set(x, y, z);
this.camera.lookAt(this.target);
}
}
const cameraRig = new CameraRig(camera);
// ------------------------------------------------------------
// River and portage geometry
// ------------------------------------------------------------
const lowerCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-23, 0, 8),
new THREE.Vector3(-18.5, 0, 7.2),
new THREE.Vector3(-14, 0, 6.3),
new THREE.Vector3(-10, 0, 4.8),
new THREE.Vector3(-6.1, 0, 3.6),
new THREE.Vector3(-2.6, 0, 2),
new THREE.Vector3(-0.65, 0, 0.45)
],
false,
"catmullrom",
0.35
);
const upperCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(6.3, 0, -5.3),
new THREE.Vector3(10.2, 0, -6.8),
new THREE.Vector3(14, 0, -8.4),
new THREE.Vector3(18.2, 0, -10.4),
new THREE.Vector3(22, 0, -13.5)
],
false,
"catmullrom",
0.35
);
const tributaryCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-14, 0, 6.3),
new THREE.Vector3(-13.2, 0, 2.5),
new THREE.Vector3(-11.4, 0, -1.5),
new THREE.Vector3(-8.7, 0, -4.5),
new THREE.Vector3(-6.6, 0, -8.2)
],
false,
"catmullrom",
0.35
);
const branchCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(14, 0, -8.4),
new THREE.Vector3(15.6, 0, -5.1),
new THREE.Vector3(18.1, 0, -2.4),
new THREE.Vector3(21.2, 0, 0.2)
],
false,
"catmullrom",
0.35
);
const portageCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.65, 0, 0.45),
new THREE.Vector3(0.8, 0, 1.05),
new THREE.Vector3(2.25, 0, 0.5),
new THREE.Vector3(3.25, 0, -1.15),
new THREE.Vector3(4.65, 0, -3.15),
new THREE.Vector3(6.3, 0, -5.3)
],
false,
"catmullrom",
0.3
);
const lowerPoints = lowerCurve.getPoints(90);
const upperPoints = upperCurve.getPoints(70);
const tributaryPoints = tributaryCurve.getPoints(65);
const branchPoints = branchCurve.getPoints(45);
const portagePoints = portageCurve.getPoints(65);
const allWaterPoints = [
lowerPoints,
upperPoints,
tributaryPoints,
branchPoints
];
function segmentDistance(px, pz, ax, az, bx, bz) {
const dx = bx - ax;
const dz = bz - az;
const denominator = dx * dx + dz * dz;
if (denominator === 0) {
return Math.hypot(px - ax, pz - az);
}
const t = clamp(
((px - ax) * dx + (pz - az) * dz) / denominator,
0,
1
);
return Math.hypot(px - (ax + dx * t), pz - (az + dz * t));
}
function distanceToPoints(px, pz, points) {
let distance = Infinity;
for (let i = 0; i < points.length - 1; i++) {
distance = Math.min(
distance,
segmentDistance(
px,
pz,
points[i].x,
points[i].z,
points[i + 1].x,
points[i + 1].z
)
);
}
return distance;
}
function distanceToWater(px, pz) {
let distance = Infinity;
for (const points of allWaterPoints) {
distance = Math.min(distance, distanceToPoints(px, pz, points));
}
return distance;
}
function distanceToPortage(px, pz) {
return distanceToPoints(px, pz, portagePoints);
}
function terrainHeight(x, z) {
let h =
0.38 +
0.28 * Math.sin(x * 0.43) +
0.23 * Math.cos(z * 0.51) +
0.11 * Math.sin((x + z) * 0.71);
h +=
2.45 *
Math.exp(
-(
((x + 16) * (x + 16)) / 34 +
((z + 9) * (z + 9)) / 18
)
);
h +=
1.85 *
Math.exp(
-(
((x - 15) * (x - 15)) / 38 +
((z - 8) * (z - 8)) / 27
)
);
h +=
1.15 *
Math.exp(
-(
((x - 6) * (x - 6)) / 22 +
((z + 12) * (z + 12)) / 14
)
);
const riverDistance = distanceToWater(x, z);
const riverBlend = THREE.MathUtils.smoothstep(
riverDistance,
1.05,
3.35
);
h = lerp(-0.3, h, riverBlend);
const pathDistance = distanceToPortage(x, z);
const pathBlend = 1 - THREE.MathUtils.smoothstep(pathDistance, 0.4, 1.25);
h = lerp(h, Math.max(h, 0.28), pathBlend * 0.65);
return h;
}
function ribbonGeometry(points, width, lift = 0, terrainFollow = false) {
const vertices = [];
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 tangent = new THREE.Vector3().subVectors(next, previous);
tangent.y = 0;
tangent.normalize();
const perpendicular = new THREE.Vector3(
-tangent.z,
0,
tangent.x
);
const point = points[i];
const y = terrainFollow
? terrainHeight(point.x, point.z) + lift
: point.y + lift;
const left = new THREE.Vector3()
.copy(point)
.addScaledVector(perpendicular, width * 0.5);
const right = new THREE.Vector3()
.copy(point)
.addScaledVector(perpendicular, -width * 0.5);
left.y = y;
right.y = y;
vertices.push(left.x, left.y, left.z);
vertices.push(right.x, right.y, right.z);
if (i < points.length - 1) {
const a = i * 2;
indices.push(a, a + 1, a + 2);
indices.push(a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
// Faceted terrain
const terrainWidth = 46;
const terrainDepth = 32;
const terrainGeometry = new THREE.PlaneGeometry(
terrainWidth,
terrainDepth,
30,
22
);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
const terrainColors = [];
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
const h = terrainHeight(x, z);
terrainPositions.setY(i, h);
const variation = (random() - 0.5) * 0.035;
const color = new THREE.Color().setHSL(
0.255 + variation,
0.27,
clamp(0.36 + h * 0.025 + variation, 0.28, 0.5)
);
terrainColors.push(color.r, color.g, color.b);
}
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
})
);
terrain.receiveShadow = true;
scene.add(terrain);
const earthBase = new THREE.Mesh(
new THREE.BoxGeometry(terrainWidth, 2.8, terrainDepth),
new THREE.MeshStandardMaterial({
color: 0x4c5140,
roughness: 1,
flatShading: true
})
);
earthBase.position.y = -1.7;
earthBase.receiveShadow = true;
scene.add(earthBase);
const bankMaterial = new THREE.MeshStandardMaterial({
color: 0xa79c73,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
});
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x4d8992,
roughness: 0.28,
metalness: 0.04,
transparent: true,
opacity: 0.92,
flatShading: true,
side: THREE.DoubleSide
});
function addRiver(points, width) {
const bank = new THREE.Mesh(
ribbonGeometry(points, width + 1.55, 0.01),
bankMaterial
);
bank.receiveShadow = true;
scene.add(bank);
const water = new THREE.Mesh(
ribbonGeometry(points, width, 0.075),
waterMaterial
);
water.receiveShadow = true;
scene.add(water);
}
addRiver(lowerPoints, 1.55);
addRiver(upperPoints, 1.45);
addRiver(tributaryPoints, 0.9);
addRiver(branchPoints, 0.82);
const pathMaterial = new THREE.MeshStandardMaterial({
color: 0x967247,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
});
const portagePath = new THREE.Mesh(
ribbonGeometry(portagePoints, 1.16, 0.075, true),
pathMaterial
);
portagePath.receiveShadow = true;
scene.add(portagePath);
// ------------------------------------------------------------
// Portage details: rollers, stones, direction markers
// ------------------------------------------------------------
const logMaterial = new THREE.MeshStandardMaterial({
color: 0x705037,
roughness: 1,
flatShading: true
});
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0x858878,
roughness: 1,
flatShading: true
});
function cylinderBetween(a, b, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
for (let i = 0; i < 13; i++) {
const t = i / 12;
const p = portageCurve.getPointAt(t);
const tangent = portageCurve.getTangentAt(t).setY(0).normalize();
const side = new THREE.Vector3(-tangent.z, 0, tangent.x);
const y = terrainHeight(p.x, p.z) + 0.14;
const left = new THREE.Vector3(p.x, y, p.z)
.addScaledVector(side, 0.88);
const right = new THREE.Vector3(p.x, y, p.z)
.addScaledVector(side, -0.88);
const roller = cylinderBetween(
left,
right,
0.075,
logMaterial,
6
);
scene.add(roller);
for (const sign of [-1, 1]) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(range(0.09, 0.18), 0),
stoneMaterial
);
rock.position
.copy(p)
.addScaledVector(side, sign * range(0.68, 0.82));
rock.position.y = terrainHeight(rock.position.x, rock.position.z) + 0.1;
rock.scale.y = range(0.55, 0.9);
rock.rotation.set(range(0, 3), range(0, 3), range(0, 3));
rock.castShadow = true;
scene.add(rock);
}
}
const arrowGeometry = new THREE.BufferGeometry();
arrowGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[0, 0, 0.25, -0.17, 0, -0.18, 0.17, 0, -0.18],
3
)
);
arrowGeometry.computeVertexNormals();
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0xe0c88b,
side: THREE.DoubleSide
});
for (let i = 1; i < 8; i++) {
const t = i / 8;
const p = portageCurve.getPointAt(t);
const tangent = portageCurve.getTangentAt(t);
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
arrow.position.set(
p.x,
terrainHeight(p.x, p.z) + 0.14,
p.z
);
arrow.rotation.y = Math.atan2(tangent.x, tangent.z);
scene.add(arrow);
}
// ------------------------------------------------------------
// Trees, rocks, buildings and docks
// ------------------------------------------------------------
const hutLocations = [
new THREE.Vector3(-11.5, 0, -8),
new THREE.Vector3(13.3, 0, 4.6),
new THREE.Vector3(17.8, 0, 9.8)
];
function makeTree(x, z, scale) {
const group = new THREE.Group();
group.position.set(x, terrainHeight(x, z), z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.17, 1.15, 5),
new THREE.MeshStandardMaterial({
color: 0x66513a,
roughness: 1,
flatShading: true
})
);
trunk.position.y = 0.57;
trunk.castShadow = true;
group.add(trunk);
const lower = new THREE.Mesh(
new THREE.ConeGeometry(0.78, 1.75, 6),
new THREE.MeshStandardMaterial({
color: 0x456c50,
roughness: 1,
flatShading: true
})
);
lower.position.y = 1.53;
lower.castShadow = true;
group.add(lower);
const upper = new THREE.Mesh(
new THREE.ConeGeometry(0.54, 1.3, 6),
new THREE.MeshStandardMaterial({
color: 0x638060,
roughness: 1,
flatShading: true
})
);
upper.position.y = 2.18;
upper.castShadow = true;
group.add(upper);
scene.add(group);
}
let treeCount = 0;
for (let attempt = 0; attempt < 700 && treeCount < 62; attempt++) {
const x = range(-21.5, 21.5);
const z = range(-14.5, 14.5);
if (distanceToWater(x, z) < 3.35) continue;
if (distanceToPortage(x, z) < 1.7) continue;
if (hutLocations.some((p) => Math.hypot(x - p.x, z - p.z) < 3.2)) {
continue;
}
makeTree(x, z, range(0.72, 1.35));
treeCount++;
}
for (let i = 0; i < 30; i++) {
const x = range(-21, 21);
const z = range(-14, 14);
if (distanceToWater(x, z) < 2.2) continue;
if (distanceToPortage(x, z) < 1) continue;
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(range(0.16, 0.42), 0),
stoneMaterial
);
rock.position.set(x, terrainHeight(x, z) + 0.1, z);
rock.scale.set(range(0.8, 1.5), range(0.55, 1.1), range(0.8, 1.4));
rock.rotation.set(range(0, 3), range(0, 3), range(0, 3));
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
function makeLonghouse(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, terrainHeight(x, z), z);
group.rotation.y = rotation;
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0x806349,
roughness: 1,
flatShading: true
});
const turfMaterial = new THREE.MeshStandardMaterial({
color: 0x617453,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
});
const walls = new THREE.Mesh(
new THREE.BoxGeometry(3.4, 1.25, 2.05),
wallMaterial
);
walls.position.y = 0.625;
walls.castShadow = true;
walls.receiveShadow = true;
group.add(walls);
const roofGeometry = new THREE.BufferGeometry();
roofGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
-1.95, 1.15, -1.2,
1.95, 1.15, -1.2,
0, 2.35, -1.2,
-1.95, 1.15, 1.2,
1.95, 1.15, 1.2,
0, 2.35, 1.2
],
3
)
);
roofGeometry.setIndex([
0, 2, 1,
3, 4, 5,
0, 3, 5,
0, 5, 2,
1, 2, 5,
1, 5, 4
]);
roofGeometry.computeVertexNormals();
const roof = new THREE.Mesh(roofGeometry, turfMaterial);
roof.castShadow = true;
group.add(roof);
const door = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.95, 0.08),
new THREE.MeshStandardMaterial({ color: 0x302b25 })
);
door.position.set(0, 0.48, 1.06);
group.add(door);
for (const side of [-1, 1]) {
const beam = new THREE.Mesh(
new THREE.BoxGeometry(0.1, 0.1, 2.2),
logMaterial
);
beam.position.set(side * 1.72, 0.88, 0);
group.add(beam);
}
scene.add(group);
}
makeLonghouse(-11.5, -8, -0.25);
makeLonghouse(13.3, 4.6, 0.42);
makeLonghouse(17.8, 9.8, -0.65);
function makeDock(x, z, angle) {
const group = new THREE.Group();
group.position.set(x, terrainHeight(x, z) + 0.18, z);
group.rotation.y = angle;
const plankMaterial = new THREE.MeshStandardMaterial({
color: 0x806149,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 7; i++) {
const plank = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 0.12, 0.34),
plankMaterial
);
plank.position.set(0, 0, (i - 3) * 0.39);
plank.castShadow = true;
plank.receiveShadow = true;
group.add(plank);
}
for (const side of [-1, 1]) {
for (const zOffset of [-0.9, 0.9]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.12, 1.45, 6),
logMaterial
);
post.position.set(side * 0.63, -0.62, zOffset);
post.castShadow = true;
group.add(post);
}
}
scene.add(group);
}
makeDock(-3.7, 2.75, -0.48);
makeDock(7.5, -5.75, 0.52);
// Portage boundary posts
const bannerMaterial = new THREE.MeshStandardMaterial({
color: 0xb34f3f,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
});
for (const t of [0, 1]) {
const p = portageCurve.getPointAt(t);
const tangent = portageCurve.getTangentAt(t);
const side = new THREE.Vector3(-tangent.z, 0, tangent.x);
const postPosition = p.clone().addScaledVector(side, t === 0 ? -0.8 : 0.8);
postPosition.y = terrainHeight(postPosition.x, postPosition.z);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.075, 1.55, 6),
logMaterial
);
post.position.copy(postPosition);
post.position.y += 0.75;
post.castShadow = true;
scene.add(post);
const banner = new THREE.Mesh(
new THREE.PlaneGeometry(0.55, 0.34),
bannerMaterial
);
banner.position.copy(postPosition);
banner.position.y += 1.25;
banner.position.addScaledVector(side, 0.28);
banner.rotation.y = Math.atan2(tangent.x, tangent.z);
scene.add(banner);
}
// ------------------------------------------------------------
// Viking longship
// ------------------------------------------------------------
const darkWood = new THREE.MeshStandardMaterial({
color: 0x4a3021,
roughness: 0.9,
flatShading: true,
side: THREE.DoubleSide
});
const lightWood = new THREE.MeshStandardMaterial({
color: 0x9a7045,
roughness: 0.88,
flatShading: true
});
const ironMaterial = new THREE.MeshStandardMaterial({
color: 0x39403b,
roughness: 0.55,
metalness: 0.45,
flatShading: true
});
const sailMaterial = new THREE.MeshStandardMaterial({
color: 0xe7d9b6,
roughness: 1,
side: THREE.DoubleSide
});
const redMaterial = new THREE.MeshStandardMaterial({
color: 0xa94739,
roughness: 0.9,
side: THREE.DoubleSide
});
function halfShipWidth(t) {
return 0.92 * Math.pow(Math.max(0, 1 - Math.pow(Math.abs(t), 3.4)), 0.56);
}
function makeHuman(tunicColor, scale = 1) {
const group = new THREE.Group();
group.scale.setScalar(scale);
const skin = new THREE.MeshStandardMaterial({
color: 0xc89570,
roughness: 1,
flatShading: true
});
const tunic = new THREE.MeshStandardMaterial({
color: tunicColor,
roughness: 1,
flatShading: true
});
const legs = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.14, 0.56, 0);
const leg = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.085, 0.53, 6),
tunic
);
leg.position.y = -0.25;
leg.castShadow = true;
pivot.add(leg);
const boot = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.1, 0.23),
darkWood
);
boot.position.set(0, -0.49, 0.04);
pivot.add(boot);
group.add(pivot);
legs.push(pivot);
}
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.32, 0.67, 6),
tunic
);
body.position.y = 0.87;
body.castShadow = true;
group.add(body);
const belt = new THREE.Mesh(
new THREE.CylinderGeometry(0.325, 0.325, 0.08, 6),
darkWood
);
belt.position.y = 0.68;
group.add(belt);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.2, 1),
skin
);
head.position.y = 1.36;
head.castShadow = true;
group.add(head);
const helmet = new THREE.Mesh(
new THREE.ConeGeometry(0.225, 0.3, 6),
ironMaterial
);
helmet.position.y = 1.53;
helmet.castShadow = true;
group.add(helmet);
const arms = [];
for (const side of [-1, 1]) {
const arm = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.075, 0.55, 6),
tunic
);
arm.position.set(side * 0.31, 0.9, 0);
arm.rotation.z = side * 0.12;
arm.castShadow = true;
group.add(arm);
arms.push(arm);
}
group.userData.legs = legs;
group.userData.arms = arms;
return group;
}
function makeVessel() {
const root = new THREE.Group();
root.userData.focusType = "vessel";
const length = 5.6;
const stationCount = 13;
const crossCount = 7;
const vertices = [];
const indices = [];
for (let i = 0; i < stationCount; i++) {
const t = -1 + (2 * i) / (stationCount - 1);
const width = halfShipWidth(t);
const z = (t * length) / 2;
const rise = 0.43 * Math.pow(Math.abs(t), 4);
const section = [
[-width, 0.5 + rise],
[-width * 0.87, 0.05],
[-width * 0.52, -0.13],
[0, -0.2],
[width * 0.52, -0.13],
[width * 0.87, 0.05],
[width, 0.5 + rise]
];
for (const [x, y] of section) {
vertices.push(x, y, z);
}
}
for (let i = 0; i < stationCount - 1; i++) {
for (let j = 0; j < crossCount - 1; j++) {
const a = i * crossCount + j;
const b = a + crossCount;
indices.push(a, b, a + 1, a + 1, b, b + 1);
}
}
const hullGeometry = new THREE.BufferGeometry();
hullGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
hullGeometry.setIndex(indices);
hullGeometry.computeVertexNormals();
const hull = new THREE.Mesh(hullGeometry, darkWood);
hull.castShadow = true;
hull.receiveShadow = true;
root.add(hull);
const deckShape = new THREE.Shape();
deckShape.moveTo(0, -length / 2);
for (let i = 0; i < stationCount; i++) {
const t = -1 + (2 * i) / (stationCount - 1);
deckShape.lineTo(-halfShipWidth(t) * 0.88, (t * length) / 2);
}
for (let i = stationCount - 1; i >= 0; i--) {
const t = -1 + (2 * i) / (stationCount - 1);
deckShape.lineTo(halfShipWidth(t) * 0.88, (t * length) / 2);
}
deckShape.closePath();
const deckGeometry = new THREE.ShapeGeometry(deckShape);
deckGeometry.rotateX(Math.PI / 2);
const deck = new THREE.Mesh(deckGeometry, lightWood);
deck.position.y = 0.13;
deck.receiveShadow = true;
root.add(deck);
for (let i = 0; i < 9; i++) {
const z = -length * 0.42 + (i / 8) * length * 0.84;
const t = z / (length / 2);
const rib = new THREE.Mesh(
new THREE.BoxGeometry(halfShipWidth(t) * 1.8, 0.055, 0.075),
lightWood
);
rib.position.set(0, 0.43, z);
root.add(rib);
}
for (const side of [-1, 1]) {
const gunwalePoints = [];
for (let i = 0; i < stationCount; i++) {
const t = -1 + (2 * i) / (stationCount - 1);
const width = halfShipWidth(t);
const rise = 0.43 * Math.pow(Math.abs(t), 4);
gunwalePoints.push(
new THREE.Vector3(
side * width,
0.52 + rise,
(t * length) / 2
)
);
}
const rail = new THREE.Mesh(
new THREE.TubeGeometry(
new THREE.CatmullRomCurve3(gunwalePoints),
40,
0.065,
6,
false
),
lightWood
);
rail.castShadow = true;
root.add(rail);
}
const shieldColors = [0x9a493b, 0xb48b46, 0x3f6570];
const shieldMaterials = shieldColors.map(
(color) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.85,
flatShading: true
})
);
for (const side of [-1, 1]) {
for (let i = 0; i < 5; i++) {
const z = -1.85 + i * 0.88;
const t = z / (length / 2);
const width = halfShipWidth(t);
const shield = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.24, 0.075, 10),
shieldMaterials[i % shieldMaterials.length]
);
shield.rotation.z = Math.PI / 2;
shield.position.set(side * (width + 0.035), 0.28, z);
shield.castShadow = true;
root.add(shield);
const boss = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 7, 5),
ironMaterial
);
boss.position.set(side * (width + 0.085), 0.28, z);
root.add(boss);
}
}
const mast = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.075, 3.9, 7),
lightWood
);
mast.position.set(0, 2.45, -0.42);
mast.castShadow = true;
root.add(mast);
const sailGroup = new THREE.Group();
root.add(sailGroup);
const sailGeometry = new THREE.PlaneGeometry(2.35, 2.25, 6, 6);
const sailPosition = sailGeometry.attributes.position;
for (let i = 0; i < sailPosition.count; i++) {
const x = sailPosition.getX(i);
const y = sailPosition.getY(i);
sailPosition.setZ(
i,
0.15 * (1 - Math.pow(x / 1.175, 2)) +
0.025 * Math.sin(y * 2)
);
}
sailGeometry.computeVertexNormals();
const sail = new THREE.Mesh(sailGeometry, sailMaterial);
sail.position.set(0, 2.45, -0.34);
sail.castShadow = true;
sailGroup.add(sail);
const diamond = new THREE.Shape();
diamond.moveTo(0, 0.28);
diamond.lineTo(0.2, 0);
diamond.lineTo(0, -0.28);
diamond.lineTo(-0.2, 0);
diamond.closePath();
const emblem = new THREE.Mesh(
new THREE.ShapeGeometry(diamond),
redMaterial
);
emblem.position.set(0, 0, 0.18);
sailGroup.add(emblem);
const riggingGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 4.25, -0.42),
new THREE.Vector3(0, 0.65, 2.6)
]);
root.add(
new THREE.Line(
riggingGeometry,
new THREE.LineBasicMaterial({ color: 0x4b4033 })
)
);
const rearRiggingGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 4.25, -0.42),
new THREE.Vector3(0, 0.78, -2.65)
]);
root.add(
new THREE.Line(
rearRiggingGeometry,
new THREE.LineBasicMaterial({ color: 0x4b4033 })
)
);
// Stylized dragon head
const neck = cylinderBetween(
new THREE.Vector3(0, 0.52, 2.35),
new THREE.Vector3(0, 0.9, 2.95),
0.15,
lightWood,
7
);
root.add(neck);
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.34, 0.58),
lightWood
);
head.position.set(0, 0.94, 3.08);
head.rotation.x = -0.12;
head.castShadow = true;
root.add(head);
const snout = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.2, 0.32),
darkWood
);
snout.position.set(0, 0.85, 3.43);
root.add(snout);
for (const side of [-1, 1]) {
const horn = new THREE.Mesh(
new THREE.ConeGeometry(0.09, 0.48, 5),
lightWood
);
horn.position.set(side * 0.17, 1.23, 3.05);
horn.rotation.z = side * -0.4;
horn.castShadow = true;
root.add(horn);
}
// Oars
for (const side of [-1, 1]) {
for (let i = 0; i < 4; i++) {
const z = -1.25 + i * 0.8;
const start = new THREE.Vector3(side * 0.72, 0.35, z);
const end = new THREE.Vector3(side * 2.2, -0.02, z + 0.24);
const oar = cylinderBetween(start, end, 0.025, lightWood, 5);
root.add(oar);
const blade = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.025, 0.48),
darkWood
);
blade.position.copy(end);
blade.rotation.y = side * -0.22;
root.add(blade);
}
}
const crew = new THREE.Group();
const helmsman = makeHuman(0x6e4937, 0.78);
helmsman.position.set(0, 0.22, -1.35);
crew.add(helmsman);
const passenger = makeHuman(0x455f68, 0.72);
passenger.position.set(0.22, 0.22, 1.0);
crew.add(passenger);
root.add(crew);
const carryCrew = new THREE.Group();
for (const side of [-1, 1]) {
const carrier = makeHuman(side > 0 ? 0x8b5a39 : 0x4e6551, 0.88);
carrier.position.set(side * 1.15, -0.14, -0.35);
carryCrew.add(carrier);
}
carryCrew.visible = false;
root.add(carryCrew);
// Deck cargo
const deckCrate = new THREE.Mesh(
new THREE.BoxGeometry(0.7, 0.46, 0.62),
lightWood
);
deckCrate.position.set(0, 0.4, -0.1);
deckCrate.rotation.y = 0.12;
deckCrate.castShadow = true;
root.add(deckCrate);
const deckBarrel = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.28, 0.55, 8),
darkWood
);
deckBarrel.position.set(0.4, 0.42, 0.35);
deckBarrel.castShadow = true;
root.add(deckBarrel);
root.rotation.order = "YXZ";
return { root, sailGroup, carryCrew, crew, human: [helmsman, passenger] };
}
const vessel = makeVessel();
scene.add(vessel.root);
// ------------------------------------------------------------
// Cargo crew and sled
// ------------------------------------------------------------
function makeCargoTeam() {
const root = new THREE.Group();
root.userData.focusType = "cargo";
const sled = new THREE.Group();
sled.position.z = -0.35;
root.add(sled);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(1.25, 0.12, 1.35),
lightWood
);
platform.position.y = 0.29;
platform.castShadow = true;
sled.add(platform);
for (const side of [-1, 1]) {
const runner = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.11, 1.65),
darkWood
);
runner.position.set(side * 0.55, 0.16, 0);
runner.castShadow = true;
sled.add(runner);
}
const crate = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 0.58, 0.62),
new THREE.MeshStandardMaterial({
color: 0x9a7548,
roughness: 1,
flatShading: true
})
);
crate.position.set(-0.25, 0.64, 0.12);
crate.rotation.y = -0.08;
crate.castShadow = true;
sled.add(crate);
const barrel = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.3, 0.66, 8),
darkWood
);
barrel.position.set(0.3, 0.68, -0.18);
barrel.castShadow = true;
sled.add(barrel);
for (const y of [0.45, 0.88]) {
const hoop = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.025, 5, 12),
ironMaterial
);
hoop.rotation.x = Math.PI / 2;
hoop.position.set(0.3, y, -0.18);
sled.add(hoop);
}
const sack = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.29, 1),
new THREE.MeshStandardMaterial({
color: 0xb6a274,
roughness: 1,
flatShading: true
})
);
sack.position.set(-0.28, 1.08, -0.25);
sack.scale.set(0.9, 1.2, 0.85);
sack.castShadow = true;
sled.add(sack);
const porters = [];
for (const side of [-1, 1]) {
const porter = makeHuman(
side > 0 ? 0x8d6541 : 0x536958,
0.9
);
porter.position.set(side * 0.48, 0, 1.1);
root.add(porter);
porters.push(porter);
}
const ropeGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.36, 0.75, 1.02),
new THREE.Vector3(0.36, 0.75, 1.02),
new THREE.Vector3(0.4, 0.4, 0.3),
new THREE.Vector3(-0.4, 0.4, 0.3)
]);
root.add(
new THREE.Line(
ropeGeometry,
new THREE.LineBasicMaterial({ color: 0x594936 })
)
);
root.rotation.order = "YXZ";
return { root, porters };
}
const cargo = makeCargoTeam();
scene.add(cargo.root);
// ------------------------------------------------------------
// Combined vessel route with weighted segment lengths
// ------------------------------------------------------------
const route = [];
let routeLength = 0;
function appendRoute(points, kind) {
for (let i = 0; i < points.length; i++) {
if (route.length && i === 0) continue;
const previous = points[Math.max(0, i - 1)];
const next = points[Math.min(points.length - 1, i + 1)];
const tangent = new THREE.Vector3()
.subVectors(next, previous)
.setY(0)
.normalize();
const point = points[i].clone();
if (kind === "portage") {
point.y = terrainHeight(point.x, point.z) + 0.4;
} else {
point.y = 0.2;
}
if (route.length) {
routeLength += point.distanceTo(route[route.length - 1].point);
}
route.push({
point,
tangent,
kind,
distance: routeLength
});
}
}
appendRoute(lowerPoints, "lower");
appendRoute(portagePoints, "portage");
appendRoute(upperPoints, "upper");
function routeAt(progress) {
const distance = clamp(progress, 0, 1) * routeLength;
let index = 0;
while (
index < route.length - 2 &&
route[index + 1].distance < distance
) {
index++;
}
const a = route[index];
const b = route[Math.min(index + 1, route.length - 1)];
const segmentLength = Math.max(0.001, b.distance - a.distance);
const local = clamp((distance - a.distance) / segmentLength, 0, 1);
const position = new THREE.Vector3().lerpVectors(
a.point,
b.point,
local
);
const tangent = new THREE.Vector3()
.lerpVectors(a.tangent, b.tangent, local)
.setY(0)
.normalize();
return {
position,
tangent,
kind: local < 0.5 ? a.kind : b.kind
};
}
// ------------------------------------------------------------
// Clouds, glints and small world details
// ------------------------------------------------------------
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xe8eee5,
roughness: 1,
flatShading: true
});
function makeCloud(x, y, z, scale) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
for (let i = 0; i < 5; i++) {
const puff = new THREE.Mesh(
new THREE.IcosahedronGeometry(range(0.65, 1.1), 1),
cloudMaterial
);
puff.position.set(
(i - 2) * 0.68,
Math.sin(i * 1.7) * 0.15,
range(-0.3, 0.3)
);
puff.scale.y = 0.58;
group.add(puff);
}
scene.add(group);
}
makeCloud(-18, 13, -15, 1.4);
makeCloud(14, 15, -20, 1.1);
makeCloud(24, 12, 4, 0.9);
const glintGeometry = new THREE.BoxGeometry(0.42, 0.012, 0.045);
const glintMaterial = new THREE.MeshBasicMaterial({
color: 0xc6e0d6,
transparent: true,
opacity: 0.7
});
const glints = [];
for (const curve of [lowerCurve, upperCurve, tributaryCurve, branchCurve]) {
for (let i = 0; i < 9; i++) {
const glint = new THREE.Mesh(glintGeometry, glintMaterial);
const offset = random();
const speed = range(0.003, 0.008);
glints.push({ mesh: glint, curve, offset, speed });
scene.add(glint);
}
}
// ------------------------------------------------------------
// UI and interactions
// ------------------------------------------------------------
const cargoButton = document.getElementById("cargo-button");
const vesselButton = document.getElementById("vessel-button");
const playButton = document.getElementById("play-button");
const resetButton = document.getElementById("reset-button");
const cargoButtonLabel = document.getElementById("cargo-button-label");
const vesselButtonLabel = document.getElementById("vessel-button-label");
const cargoFill = document.getElementById("cargo-fill");
const vesselFill = document.getElementById("vessel-fill");
const cargoStatus = document.getElementById("cargo-status");
const vesselStatus = document.getElementById("vessel-status");
const toast = document.getElementById("toast");
let toastTimer;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2400);
}
cargoButton.addEventListener("click", () => {
if (state.cargoProgress >= 0.999) {
state.cargoProgress = 0;
state.cargoDone = false;
state.cargoActive = true;
showToast("Cargo returned to the lower landing.");
} else {
state.cargoActive = !state.cargoActive;
showToast(
state.cargoActive
? "Porters moving over the carry."
: "Cargo crew holding at the trail."
);
}
});
vesselButton.addEventListener("click", () => {
if (state.vesselProgress >= 0.999) {
state.vesselProgress = 0;
state.vesselDone = false;
state.vesselActive = true;
showToast("Vessel returned to the lower reach.");
} else {
state.vesselActive = !state.vesselActive;
showToast(
state.vesselActive
? "Vessel advancing along the route."
: "Vessel crew holding position."
);
}
});
playButton.addEventListener("click", () => {
state.playing = !state.playing;
showToast(state.playing ? "Route resumed." : "Route paused.");
});
resetButton.addEventListener("click", () => {
state.cargoProgress = 0.035;
state.vesselProgress = 0.025;
state.cargoActive = true;
state.vesselActive = true;
state.cargoDone = false;
state.vesselDone = false;
state.playing = true;
cameraRig.focus(new THREE.Vector3(0, 0, -1));
showToast("Route reset to the lower reach.");
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
state.playing = !state.playing;
}
});
// Click-to-follow picking
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pickables = [vessel.root, cargo.root];
renderer.domElement.addEventListener("pointerup", (event) => {
if (cameraRig.dragged > 5) return;
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, true);
if (!intersections.length) return;
let object = intersections[0].object;
while (object && !object.userData.focusType) {
object = object.parent;
}
if (object?.userData.focusType === "vessel") {
cameraRig.focus(vessel.root.position);
showToast("Following the Viking vessel.");
} else if (object?.userData.focusType === "cargo") {
cameraRig.focus(cargo.root.position);
showToast("Following the cargo crew.");
}
});
const labels = [
{
element: document.getElementById("label-lower"),
position: new THREE.Vector3(-12, 3.7, 4.6)
},
{
element: document.getElementById("label-portage"),
position: new THREE.Vector3(2.3, 2.4, -0.1)
},
{
element: document.getElementById("label-upper"),
position: new THREE.Vector3(14, 3.1, -8.2)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
label.element.style.display = visible ? "block" : "none";
if (visible) {
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
const portageDistance = Math.round(portageCurve.getLength() * 5);
document.getElementById("distance").textContent = `${portageDistance} m`;
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (state.playing) {
if (state.cargoActive && state.cargoProgress < 1) {
state.cargoProgress = Math.min(
1,
state.cargoProgress + dt * 0.036
);
}
if (state.vesselActive && state.vesselProgress < 1) {
state.vesselProgress = Math.min(
1,
state.vesselProgress + dt * 0.016
);
}
}
if (
state.cargoProgress >= 1 &&
!state.cargoDone
) {
state.cargoDone = true;
state.cargoActive = false;
showToast("Cargo delivered to the upper landing.");
}
if (
state.vesselProgress >= 1 &&
!state.vesselDone
) {
state.vesselDone = true;
state.vesselActive = false;
showToast("The vessel has reached the upper reach.");
}
// Vessel movement
const vesselState = routeAt(state.vesselProgress);
vessel.root.position.copy(vesselState.position);
const isOnPortage = vesselState.kind === "portage";
vessel.sailGroup.visible = !isOnPortage;
vessel.carryCrew.visible = isOnPortage;
if (!isOnPortage) {
vessel.root.position.y += Math.sin(elapsed * 2.1) * 0.022;
vessel.root.rotation.z = Math.sin(elapsed * 1.4) * 0.012;
vessel.root.rotation.x = Math.sin(elapsed * 1.8) * 0.006;
} else {
vessel.root.rotation.z = Math.sin(elapsed * 4.2) * 0.035;
vessel.root.rotation.x = Math.sin(elapsed * 3.1) * 0.018;
}
vessel.root.rotation.y = Math.atan2(
vesselState.tangent.x,
vesselState.tangent.z
);
for (const human of vessel.human) {
human.userData.legs[0].rotation.x =
Math.sin(elapsed * 3.2) * 0.08;
human.userData.legs[1].rotation.x =
-Math.sin(elapsed * 3.2) * 0.08;
}
// Cargo movement
const cargoPoint = portageCurve.getPointAt(state.cargoProgress);
const cargoTangent = portageCurve.getTangentAt(state.cargoProgress);
cargo.root.position.set(
cargoPoint.x,
terrainHeight(cargoPoint.x, cargoPoint.z) + 0.08,
cargoPoint.z
);
cargo.root.rotation.y = Math.atan2(
cargoTangent.x,
cargoTangent.z
);
const walking = state.playing && state.cargoActive;
const walkCycle = walking ? Math.sin(elapsed * 9.5) : 0;
for (let i = 0; i < cargo.porters.length; i++) {
const porter = cargo.porters[i];
const phase = i === 0 ? 0 : Math.PI;
porter.userData.legs[0].rotation.x =
walkCycle * 0.48 * Math.cos(phase);
porter.userData.legs[1].rotation.x =
walkCycle * 0.48 * Math.cos(phase + Math.PI);
porter.userData.arms[0].rotation.x =
-walkCycle * 0.28 * Math.cos(phase);
porter.userData.arms[1].rotation.x =
-walkCycle * 0.28 * Math.cos(phase + Math.PI);
}
// Water glints drift downstream
for (const glint of glints) {
const t = (glint.offset + elapsed * glint.speed) % 1;
const p = glint.curve.getPointAt(t);
const tangent = glint.curve.getTangentAt(t);
glint.mesh.position.set(p.x, 0.095, p.z);
glint.mesh.rotation.y = -Math.atan2(tangent.z, tangent.x);
glint.mesh.material.opacity =
0.42 + Math.sin(elapsed * 1.7 + glint.offset * 20) * 0.2;
}
// Progress readouts
const cargoPercent = Math.round(state.cargoProgress * 100);
const vesselPercent = Math.round(state.vesselProgress * 100);
cargoFill.style.width = `${cargoPercent}%`;
vesselFill.style.width = `${vesselPercent}%`;
if (state.cargoProgress <= 0.025) {
cargoStatus.textContent = "At lower landing";
} else if (state.cargoProgress >= 0.975) {
cargoStatus.textContent = "Delivered";
} else {
cargoStatus.textContent = "On the carry";
}
if (vesselState.kind === "lower") {
vesselStatus.textContent = "Lower reach";
} else if (vesselState.kind === "portage") {
vesselStatus.textContent = "Overland";
} else {
vesselStatus.textContent = "Upper reach";
}
cargoButton.classList.toggle("active", state.cargoActive);
vesselButton.classList.toggle("active", state.vesselActive);
cargoButtonLabel.textContent =
state.cargoProgress >= 0.999
? "Cargo delivered"
: state.cargoActive
? "Cargo running"
: "Cargo paused";
vesselButtonLabel.textContent =
state.vesselProgress >= 0.999
? "Vessel arrived"
: state.vesselActive
? "Vessel running"
: "Vessel paused";
playButton.textContent = state.playing ? "Pause route" : "Resume route";
cameraRig.update(dt);
updateLabels();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
animate();
document.getElementById("loading").classList.add("hidden");
</script>
</body>
</html>
```