Prompt specification
Build or customize the Three.js 3D scene "Ferry arriving at a narrow fjord settlement" 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: Ferry arriving at a narrow fjord settlement
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ferry-arriving-at-a-narrow-fjord-settlement-x3102
Scene Description Prompt:
Animate a ferry arriving at a voxel Norwegian fjord settlement, with homes and paths beneath steep slopes. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (ferry-arriving-at-a-narrow-fjord-settlement-x3102):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fjordhavn — A Voxel Norwegian Ferry Arrival</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 20%, rgba(255, 244, 207, 0.45), transparent 28%),
linear-gradient(180deg, #91bdca 0%, #c5dadd 48%, #e7ddc6 100%);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
body::after {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(10, 28, 34, 0.04), transparent 30%),
radial-gradient(ellipse at center, transparent 55%, rgba(16, 37, 42, 0.19) 100%);
}
.hud {
position: fixed;
z-index: 10;
color: #f8f5e9;
text-shadow: 0 2px 14px rgba(0, 0, 0, 0.28);
pointer-events: none;
}
.title-block {
top: 30px;
left: 34px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
color: rgba(255, 255, 255, 0.78);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.25em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 27px;
height: 2px;
background: #e15345;
}
h1 {
margin: 0;
font-size: clamp(30px, 5vw, 58px);
font-weight: 800;
line-height: 0.9;
letter-spacing: -0.055em;
}
.subtitle {
margin-top: 12px;
max-width: 310px;
color: rgba(255, 255, 255, 0.74);
font-size: 12px;
line-height: 1.55;
letter-spacing: 0.035em;
}
.status {
left: 34px;
bottom: 30px;
display: flex;
align-items: center;
gap: 11px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 999px;
background: rgba(17, 44, 51, 0.42);
backdrop-filter: blur(12px);
box-shadow: 0 10px 32px rgba(9, 28, 34, 0.14);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #f3c45e;
box-shadow: 0 0 0 4px rgba(243, 196, 94, 0.16);
}
.controls {
right: 30px;
bottom: 30px;
color: rgba(255, 255, 255, 0.7);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.coordinates {
right: 32px;
top: 30px;
color: rgba(255, 255, 255, 0.68);
text-align: right;
font-size: 10px;
font-weight: 650;
line-height: 1.8;
letter-spacing: 0.13em;
text-transform: uppercase;
}
@media (max-width: 640px) {
.title-block {
top: 22px;
left: 22px;
}
.subtitle {
display: none;
}
.status {
left: 20px;
bottom: 20px;
}
.controls {
right: 20px;
bottom: 24px;
max-width: 110px;
text-align: right;
line-height: 1.6;
}
.coordinates {
top: 22px;
right: 20px;
}
}
</style>
</head>
<body>
<div class="hud title-block">
<div class="eyebrow">Norway · 63° North</div>
<h1>FJORDHAVN</h1>
<div class="subtitle">
A small northern settlement beneath the mountains.
The morning ferry is making its way into the village.
</div>
</div>
<div class="hud coordinates">
Aurlandsfjord<br>
Local time · 08:42
</div>
<div class="hud status">
<span class="status-dot" id="status-dot"></span>
<span id="status-text">Approaching the dock</span>
</div>
<div class="hud controls">
Drag to orbit<br>
Scroll to zoom
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0xb9d7dc, 0.0057);
const camera = new THREE.PerspectiveCamera(
46,
innerWidth / innerHeight,
0.1,
300
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Animated voxel Norwegian fjord with an arriving ferry"
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xdff4f7, 0x455342, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe5b8, 3.25);
sun.position.set(-32, 58, 34);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -70;
sun.shadow.camera.right = 70;
sun.shadow.camera.top = 70;
sun.shadow.camera.bottom = -70;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 150;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8fc7db, 0.7);
fill.position.set(40, 24, -35);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared materials and static geometry batching
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
metalness: 0,
flatShading: true,
...options
});
const M = {
grass: [
standard(0x647f50),
standard(0x58764a),
standard(0x718a55)
],
rock: standard(0x626b68),
lightRock: standard(0x7b817b),
darkRock: standard(0x4c5856),
snow: standard(0xe8eee7),
path: standard(0xb8a177),
soil: standard(0x80745c),
wood: standard(0x79543d),
woodLight: standard(0xa17950),
red: standard(0xa93e35),
brick: standard(0x884037),
cream: standard(0xe9dfc4),
white: standard(0xf2eee0),
ochre: standard(0xc99649),
blue: standard(0x355d70),
roof: standard(0x36464a),
roofRed: standard(0x743c36),
window: standard(0xa9dce0, {
emissive: 0x5b9da7,
emissiveIntensity: 0.72,
roughness: 0.35
}),
lamp: standard(0xffdc8a, {
emissive: 0xffbd5b,
emissiveIntensity: 1.6
}),
tree: [
standard(0x315a43),
standard(0x3c6749),
standard(0x4b7550)
],
trunk: standard(0x604735),
flagRed: standard(0xba3038),
flagWhite: standard(0xf4f0df),
flagBlue: standard(0x193e68),
cloud: standard(0xf2f0df, {
emissive: 0xe8e6d4,
emissiveIntensity: 0.16
}),
waterfall: standard(0x8ecbd0, {
emissive: 0x467e85,
emissiveIntensity: 0.25
})
};
const batches = new Map();
const dummy = new THREE.Object3D();
function queueBox(x, y, z, width, height, depth, material, rotationY = 0) {
dummy.position.set(x, y, z);
dummy.rotation.set(0, rotationY, 0);
dummy.scale.set(width, height, depth);
dummy.updateMatrix();
if (!batches.has(material)) {
batches.set(material, []);
}
batches.get(material).push(dummy.matrix.clone());
}
function flushBatches() {
for (const [material, matrices] of batches) {
const mesh = new THREE.InstancedMesh(unitBox, material, matrices.length);
matrices.forEach((matrix, index) => {
mesh.setMatrixAt(index, matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = material !== M.cloud;
mesh.receiveShadow = true;
scene.add(mesh);
}
}
// -------------------------------------------------------------------------
// Water
// -------------------------------------------------------------------------
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x438f9e,
roughness: 0.43,
metalness: 0.12
});
const water = new THREE.Mesh(
new THREE.PlaneGeometry(240, 240),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.set(0, 0, -10);
water.receiveShadow = true;
scene.add(water);
// Small animated voxel-like water highlights.
const waveCount = 95;
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0xa4d9d5,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const waveMesh = new THREE.InstancedMesh(unitBox, waveMaterial, waveCount);
waveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waveMesh.frustumCulled = false;
scene.add(waveMesh);
const waveSeeds = Array.from({ length: waveCount }, (_, i) => ({
x: -14 + Math.random() * 28,
z: -82 + Math.random() * 150,
width: 0.12 + Math.random() * 0.42,
depth: 0.7 + Math.random() * 2.6,
phase: Math.random() * Math.PI * 2,
speed: 0.25 + Math.random() * 0.5,
rotation: (Math.random() - 0.5) * 0.22
}));
// -------------------------------------------------------------------------
// Terraced fjord terrain
// -------------------------------------------------------------------------
function heightAt(x, z) {
if (Math.abs(x) < 14) {
return 1.55;
}
const side = x < 0 ? -1 : 1;
const distanceFromWater = Math.max(0, side * x - 14);
const terrace = Math.floor(distanceFromWater / 3);
const variation =
Math.sin(z * 0.115 + side) * 0.58 +
Math.sin(x * 0.34 + z * 0.055) * 0.27;
return Math.max(1.35, 2.15 + terrace * 3.12 + variation);
}
for (const side of [-1, 1]) {
for (let column = 0; column < 9; column++) {
const x = side * (16.5 + column * 3);
for (let z = -72; z < 30; z += 5) {
const zCenter = z + 2.5;
const height = heightAt(x, zCenter);
const rockTop = height - 0.46;
queueBox(
x,
(rockTop - 1) / 2,
zCenter,
3.02,
rockTop + 1,
5.02,
column % 3 === 0 ? M.darkRock : M.rock
);
const grassMaterial =
M.grass[Math.abs(Math.round(x + zCenter)) % M.grass.length];
queueBox(
x,
height - 0.23,
zCenter,
3.04,
0.46,
5.04,
grassMaterial
);
}
}
}
// Rocky shoreline ledges.
for (const side of [-1, 1]) {
for (let z = -68; z < 28; z += 8) {
queueBox(
side * 14.85,
0.2,
z,
1.25,
1.55,
4.1,
M.lightRock
);
}
}
// Distant stepped mountains.
const mountainLeft = [
[-29, 4, -70, 22, 10, 20, M.rock],
[-29, 13, -70, 18, 8, 16, M.lightRock],
[-29, 21, -70, 13, 8, 12, M.rock],
[-29, 28, -70, 8, 6, 8, M.snow],
[-29, 32.5, -70, 4, 3, 4, M.snow]
];
const mountainRight = [
[31, 3, -73, 23, 9, 22, M.rock],
[31, 11, -73, 18, 7, 17, M.lightRock],
[31, 18, -73, 13, 7, 13, M.rock],
[31, 24, -73, 8, 5, 8, M.snow],
[31, 28, -73, 4, 3, 4, M.snow]
];
[...mountainLeft, ...mountainRight].forEach((b) => {
queueBox(...b);
});
// A thin voxel waterfall descending from the left mountain.
for (let i = 0; i < 13; i++) {
queueBox(
-24.6,
24 - i * 1.25,
-59.9,
0.9 + Math.sin(i * 0.5) * 0.16,
1.28,
0.22,
M.waterfall
);
}
// Blocky clouds.
const cloudClusters = [
[-27, 34, -79, 8],
[14, 39, -91, 7],
[39, 31, -60, 6],
[-52, 29, -46, 5]
];
for (const [x, y, z, scale] of cloudClusters) {
queueBox(x, y, z, scale * 1.5, 2.1, 3.4, M.cloud);
queueBox(x - scale * 0.35, y + 1.1, z, scale, 2.2, 3.1, M.cloud);
queueBox(x + scale * 0.65, y - 0.2, z + 0.4, scale * 0.8, 1.7, 2.7, M.cloud);
}
// -------------------------------------------------------------------------
// Paths, houses and settlement details
// -------------------------------------------------------------------------
function addPath(points) {
for (let segment = 0; segment < points.length - 1; segment++) {
const [x1, z1] = points[segment];
const [x2, z2] = points[segment + 1];
const distance = Math.hypot(x2 - x1, z2 - z1);
const steps = Math.max(1, Math.ceil(distance / 1.5));
for (let step = 0; step < steps; step++) {
const u = step / steps;
const x = THREE.MathUtils.lerp(x1, x2, u);
const z = THREE.MathUtils.lerp(z1, z2, u);
const y = heightAt(x, z) + 0.18;
queueBox(x, y, z, 1.65, 0.28, 1.65, M.path);
}
}
}
addPath([
[-11.1, 12],
[-14.2, 10.5],
[-17.1, 10.1],
[-20.8, 8],
[-22.2, 2.8],
[-24.4, -3],
[-27.2, -13],
[-28.5, -22],
[-31, -33]
]);
addPath([
[14.2, 13],
[16.6, 12],
[18, 16],
[20.3, 10],
[21, 4],
[22.6, -4],
[24.5, -12],
[27.6, -21],
[29, -28]
]);
function addNorwegianFlag(x, y, z, scale = 1) {
const poleHeight = 2.8 * scale;
queueBox(
x,
y + poleHeight / 2,
z,
0.12 * scale,
poleHeight,
0.12 * scale,
M.wood
);
const flagX = x + 0.48 * scale;
queueBox(
flagX,
y + poleHeight - 0.38 * scale,
z,
0.96 * scale,
0.6 * scale,
0.07 * scale,
M.flagRed
);
queueBox(
flagX,
y + poleHeight - 0.38 * scale,
z + 0.05 * scale,
0.16 * scale,
0.61 * scale,
0.09 * scale,
M.flagWhite
);
queueBox(
flagX,
y + poleHeight - 0.38 * scale,
z + 0.05 * scale,
0.07 * scale,
0.62 * scale,
0.11 * scale,
M.flagBlue
);
queueBox(
flagX,
y + poleHeight - 0.38 * scale,
z + 0.065 * scale,
0.97 * scale,
0.17 * scale,
0.1 * scale,
M.flagWhite
);
queueBox(
flagX,
y + poleHeight - 0.38 * scale,
z + 0.075 * scale,
0.98 * scale,
0.08 * scale,
0.11 * scale,
M.flagBlue
);
}
function addHouse(x, z, width, depth, wallMaterial, roofMaterial, flag = false) {
const base = Math.max(
heightAt(x - width * 0.6, z - depth * 0.6),
heightAt(x + width * 0.6, z - depth * 0.6),
heightAt(x - width * 0.6, z + depth * 0.6),
heightAt(x + width * 0.6, z + depth * 0.6)
) + 0.12;
const foundationHeight = 0.72;
const wallHeight = 3.15;
const wallBottom = base + foundationHeight;
const wallCenter = wallBottom + wallHeight / 2;
const roofBottom = wallBottom + wallHeight;
queueBox(
x,
base + foundationHeight / 2,
z,
width + 0.9,
foundationHeight,
depth + 0.9,
M.lightRock
);
queueBox(
x,
wallCenter,
z,
width,
wallHeight,
depth,
wallMaterial
);
// Stepped voxel roof.
for (let step = 0; step < 4; step++) {
queueBox(
x,
roofBottom + 0.25 + step * 0.43,
z,
width + 0.85 - step * (width * 0.24),
0.45,
depth + 0.8,
roofMaterial
);
}
// Front windows.
for (const side of [-1, 1]) {
queueBox(
x + side * width * 0.25,
wallBottom + wallHeight * 0.59,
z + depth / 2 + 0.045,
0.9,
1.05,
0.1,
M.window
);
}
// Side windows.
for (const side of [-1, 1]) {
queueBox(
x + width / 2 + 0.045,
wallBottom + wallHeight * 0.59,
z + side * depth * 0.23,
0.1,
1.05,
0.9,
M.window
);
}
queueBox(
x,
wallBottom + 1.05,
z + depth / 2 + 0.08,
1.05,
2.1,
0.16,
M.blue
);
queueBox(
x + width * 0.28,
roofBottom + 1.15,
z - depth * 0.22,
0.65,
2.25,
0.65,
M.rock
);
if (flag) {
addNorwegianFlag(
x - width / 2 - 0.35,
base + 0.15,
z - depth * 0.3,
0.9
);
}
}
const houses = [
[-17.8, 17.5, 4.2, 5.1, M.red, M.roof, true],
[-20.8, 8, 4.5, 5.2, M.cream, M.roof, false],
[-24.4, -3, 4.6, 5.4, M.red, M.roofRed, true],
[-19.8, -18, 4.2, 5.1, M.ochre, M.roof, false],
[-28, -35, 4.5, 5.4, M.cream, M.roof, true],
[-31, -48, 4.3, 5.1, M.red, M.roofRed, false],
[18, 16, 4.4, 5.1, M.white, M.roof, false],
[21, 4, 4.6, 5.3, M.red, M.roof, true],
[24.5, -12, 4.5, 5.2, M.cream, M.roof, false],
[29, -28, 4.5, 5.3, M.ochre, M.roofRed, true],
[18, -39, 4.2, 5.0, M.white, M.roof, false]
];
houses.forEach((house) => addHouse(...house));
// -------------------------------------------------------------------------
// Wooden pier and boathouse
// -------------------------------------------------------------------------
for (let z = -2; z < 18; z += 0.9) {
queueBox(-11.9, 1.35, z + 0.44, 3.4, 0.35, 0.82, M.woodLight);
}
for (const x of [-13.35, -10.45]) {
for (let z = -1; z < 18; z += 4) {
queueBox(x, 0.2, z, 0.42, 3.3, 0.42, M.wood);
queueBox(x, 1.93, z, 0.68, 0.16, 0.68, M.wood);
}
}
// Dock lights.
for (const z of [-0.5, 5.5, 11.5, 17]) {
queueBox(-13.05, 3.05, z, 0.15, 3.15, 0.15, M.wood);
queueBox(-13.05, 4.72, z, 0.48, 0.48, 0.48, M.lamp);
queueBox(-13.05, 4.35, z, 0.66, 0.16, 0.66, M.wood);
}
// Small red boathouse on the pier.
queueBox(-12.1, 2.55, 15.4, 2.7, 2.05, 3.3, M.red);
queueBox(-12.1, 3.76, 15.4, 3.2, 0.36, 3.8, M.roofRed);
queueBox(-12.1, 2.62, 17.08, 1.25, 1.8, 0.14, M.wood);
queueBox(-12.1, 2.75, 17.17, 0.8, 0.85, 0.1, M.window);
addNorwegianFlag(-10.45, 1.55, 14.1, 0.75);
// -------------------------------------------------------------------------
// Voxel pine trees
// -------------------------------------------------------------------------
function addPine(x, z, scale = 1) {
const y = heightAt(x, z);
const treeMaterial =
M.tree[Math.abs(Math.round(x * 2 + z)) % M.tree.length];
queueBox(x, y + 1.1 * scale, z, 0.42 * scale, 2.2 * scale, 0.42 * scale, M.trunk);
queueBox(x, y + 2.3 * scale, z, 2.7 * scale, 1.35 * scale, 2.7 * scale, treeMaterial);
queueBox(x, y + 3.25 * scale, z, 2.05 * scale, 1.3 * scale, 2.05 * scale, treeMaterial);
queueBox(x, y + 4.1 * scale, z, 1.25 * scale, 1.25 * scale, 1.25 * scale, treeMaterial);
}
let treeCount = 0;
let attempts = 0;
while (treeCount < 52 && attempts < 500) {
attempts++;
const side = Math.random() > 0.5 ? -1 : 1;
const x = side * (17.5 + Math.random() * 18);
const z = -60 + Math.random() * 88;
const nearHouse = houses.some(
(house) => Math.hypot(x - house[0], z - house[1]) < 6.2
);
const nearDock = x < -8 && Math.abs(z - 8) < 17;
const nearPath = x < -16 && x > -31 && z > -36 && z < 19;
if (nearHouse || nearDock || nearPath) {
continue;
}
addPine(x, z, 0.72 + Math.random() * 0.48);
treeCount++;
}
flushBatches();
// -------------------------------------------------------------------------
// Voxel ferry
// -------------------------------------------------------------------------
const boat = new THREE.Group();
scene.add(boat);
const boatMaterials = {
hull: standard(0x183b4a),
hullLight: standard(0x285769),
red: standard(0xc4483c),
cream: standard(0xf0e6cd),
roof: standard(0xd8d5c7),
glass: standard(0x8dc6d0, {
emissive: 0x4e8f9a,
emissiveIntensity: 0.72,
roughness: 0.25
}),
dark: standard(0x25343a),
orange: standard(0xe97835),
metal: standard(0xb6b9ac, {
metalness: 0.4,
roughness: 0.45
})
};
function boatPart(group, material, x, y, z, width, height, depth) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.position.set(x, y, z);
mesh.scale.set(width, height, depth);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
// Stepped hull and bow.
boatPart(boat, boatMaterials.hull, 0, 0.67, -0.5, 5.2, 1.35, 10);
boatPart(boat, boatMaterials.hullLight, 0, 0.67, 5.2, 4.25, 1.35, 1.8);
boatPart(boat, boatMaterials.hull, 0, 0.62, 6.7, 2.75, 1.2, 0.85);
boatPart(boat, boatMaterials.hullLight, 0, 0.64, -6.05, 4.65, 1.3, 1.45);
// Red stripe along both sides.
for (const side of [-1, 1]) {
boatPart(boat, boatMaterials.red, side * 2.63, 1.05, -0.5, 0.08, 0.34, 9.95);
boatPart(boat, boatMaterials.red, side * 2.15, 1.05, 5.25, 0.08, 0.34, 1.75);
}
// Deck.
boatPart(boat, boatMaterials.cream, 0, 1.48, -0.5, 5.35, 0.26, 10.1);
boatPart(boat, boatMaterials.cream, 0, 1.48, 5.15, 4.35, 0.26, 1.8);
boatPart(boat, boatMaterials.cream, 0, 1.48, -6.05, 4.75, 0.26, 1.45);
// Main cabin.
boatPart(boat, boatMaterials.cream, 0, 2.43, 1.1, 4.2, 1.65, 6.45);
boatPart(boat, boatMaterials.roof, 0, 3.33, 1.1, 4.45, 0.22, 6.7);
// Upper wheelhouse.
boatPart(boat, boatMaterials.cream, 0, 3.98, 1.75, 3.25, 1.08, 4.25);
boatPart(boat, boatMaterials.roof, 0, 4.61, 1.75, 3.55, 0.22, 4.55);
// Cabin windows.
for (const side of [-1, 1]) {
for (let i = 0; i < 4; i++) {
boatPart(
boat,
boatMaterials.glass,
side * 2.12,
2.55,
-1.35 + i * 1.45,
0.08,
0.78,
0.95
);
}
for (let i = 0; i < 3; i++) {
boatPart(
boat,
boatMaterials.glass,
side * 1.65,
4.02,
0.45 + i * 1.18,
0.08,
0.58,
0.77
);
}
}
boatPart(boat, boatMaterials.glass, 0, 4.02, 3.85, 2.65, 0.58, 0.08);
boatPart(boat, boatMaterials.glass, 0, 2.55, 4.35, 3.35, 0.75, 0.08);
// Funnel and mast.
boatPart(boat, boatMaterials.dark, 0, 4.05, -2.25, 1.15, 2.2, 1.2);
boatPart(boat, boatMaterials.red, 0, 4.76, -2.25, 1.25, 0.42, 1.3);
boatPart(boat, boatMaterials.dark, 0, 5.15, -2.25, 1.35, 0.18, 1.4);
boatPart(boat, boatMaterials.metal, 0, 5.45, 1.5, 0.12, 1.45, 0.12);
// Lifeboats.
for (const side of [-1, 1]) {
boatPart(
boat,
boatMaterials.orange,
side * 2.15,
2.15,
-3.7,
0.85,
0.65,
2.15
);
}
// Railings.
for (const side of [-1, 1]) {
boatPart(boat, boatMaterials.cream, side * 2.55, 2.45, -0.6, 0.09, 0.1, 10.4);
for (let z = -5.5; z <= 4.5; z += 1.25) {
boatPart(boat, boatMaterials.cream, side * 2.55, 2.12, z, 0.08, 0.72, 0.08);
}
}
boatPart(boat, boatMaterials.cream, 0, 2.45, -5.7, 5.1, 0.1, 0.09);
for (const x of [-2.5, -1.25, 0, 1.25, 2.5]) {
boatPart(boat, boatMaterials.cream, x, 2.12, -5.7, 0.08, 0.72, 0.08);
}
// -------------------------------------------------------------------------
// Wake and smoke
// -------------------------------------------------------------------------
const wakeMaterial = new THREE.MeshBasicMaterial({
color: 0xe1f3ec,
transparent: true,
opacity: 0.76,
depthWrite: false
});
const wakePool = [];
for (let i = 0; i < 52; i++) {
const mesh = new THREE.Mesh(unitBox, wakeMaterial);
mesh.visible = false;
mesh.renderOrder = 3;
scene.add(mesh);
wakePool.push({
mesh,
age: 10,
life: 2.6 + Math.random() * 0.7,
side: i % 2 === 0 ? -1 : 1
});
}
let wakeCursor = 0;
function spawnWake(side) {
const wake = wakePool[wakeCursor];
wakeCursor = (wakeCursor + 1) % wakePool.length;
const localX = side * (1.9 + Math.random() * 0.4);
const localZ = -6.8 - Math.random() * 1.1;
const angle = boat.rotation.y;
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
wake.age = 0;
wake.side = side;
wake.mesh.visible = true;
wake.mesh.position.set(
boat.position.x + localX * cosine + localZ * sine,
0.09,
boat.position.z - localX * sine + localZ * cosine
);
wake.mesh.scale.set(0.4, 0.04, 0.7);
}
const smokePuffs = [];
for (let i = 0; i < 9; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xd5ddda,
transparent: true,
opacity: 0.4,
depthWrite: false
});
const mesh = new THREE.Mesh(unitBox, material);
mesh.renderOrder = 2;
scene.add(mesh);
smokePuffs.push({ mesh, material });
}
// -------------------------------------------------------------------------
// Camera controls implemented directly without addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-2, 3.8, -10);
let cameraYaw = 0.72;
let cameraPitch = 1.16;
let cameraRadius = 78;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const horizontal = Math.sin(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + Math.cos(cameraPitch) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= deltaX * 0.005;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.004,
0.58,
1.46
);
});
function stopDragging(event) {
dragging = false;
if (event?.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
34,
125
);
},
{ passive: false }
);
updateCamera();
// -------------------------------------------------------------------------
// Ferry arrival timeline
// -------------------------------------------------------------------------
const statusText = document.getElementById("status-text");
const statusDot = document.getElementById("status-dot");
let currentStatus = "";
let wakeTimer = 0;
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function easeOutCubic(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return 1 - Math.pow(1 - t, 3);
}
function updateStatus(status, color) {
if (status === currentStatus) return;
currentStatus = status;
statusText.textContent = status;
statusDot.style.background = color;
statusDot.style.boxShadow = `0 0 0 4px ${color}28`;
}
function getBoatState(time) {
const cycle = time % 26;
if (cycle < 11) {
const u = easeOutCubic(cycle / 10);
const x = THREE.MathUtils.lerp(0.4, -7, u);
const z = THREE.MathUtils.lerp(-62, 7, u);
const angle = Math.atan2(-7.4, 69) * u;
return {
x,
z,
angle,
visible: true,
moving: true,
dock: false,
status: "Approaching the dock",
color: "#f3c45e"
};
}
if (cycle < 15) {
return {
x: -7,
z: 7,
angle: Math.atan2(-7.4, 69),
visible: true,
moving: false,
dock: true,
status: "Moored at berth 03",
color: "#7ed49b"
};
}
if (cycle < 23.5) {
const u = smoothstep((cycle - 15) / 8.5);
const x = THREE.MathUtils.lerp(-7, 4, u);
const z = THREE.MathUtils.lerp(7, 61, u);
const angle = THREE.MathUtils.lerp(
Math.atan2(-7.4, 69),
0.08,
u
);
return {
x,
z,
angle,
visible: true,
moving: true,
dock: false,
status: "Leaving the fjord",
color: "#e98972"
};
}
return {
x: 4,
z: 64,
angle: 0.08,
visible: false,
moving: false,
dock: false,
status: "Beyond the fjord",
color: "#b8c6ca"
};
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const waveDummy = new THREE.Object3D();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.05);
const state = getBoatState(elapsed);
updateStatus(state.status, state.color);
boat.visible = state.visible;
boat.position.set(state.x, 0.06, state.z);
boat.rotation.y = state.angle;
const bobAmount = state.dock ? 0.035 : 0.095;
const rollAmount = state.dock ? 0.004 : 0.012;
boat.position.y += Math.sin(elapsed * 2.05) * bobAmount;
boat.rotation.z = Math.sin(elapsed * 1.35) * rollAmount;
boat.rotation.x = Math.sin(elapsed * 1.7 + 0.8) * rollAmount * 0.55;
// Wake.
if (state.moving && state.visible) {
wakeTimer += delta;
while (wakeTimer > 0.11) {
wakeTimer -= 0.11;
spawnWake(-1);
spawnWake(1);
}
}
for (const wake of wakePool) {
if (!wake.mesh.visible) continue;
wake.age += delta;
if (wake.age >= wake.life) {
wake.mesh.visible = false;
continue;
}
const life = wake.age / wake.life;
wake.mesh.position.x += wake.side * delta * 0.18;
wake.mesh.position.y = 0.075 + Math.sin(elapsed * 2 + wake.age) * 0.008;
wake.mesh.scale.set(
0.4 + life * 1.65,
0.035,
0.65 + life * 1.25
);
}
// Smoke from the ferry funnel.
const funnelLocalZ = -2.25;
const cosine = Math.cos(state.angle);
const sine = Math.sin(state.angle);
const funnelX =
boat.position.x + funnelLocalZ * sine;
const funnelZ =
boat.position.z + funnelLocalZ * cosine;
smokePuffs.forEach((puff, index) => {
if (!state.visible) {
puff.mesh.visible = false;
return;
}
const age = (elapsed * 0.43 + index * 0.137) % 1;
const size = 0.24 + age * 1.05;
puff.mesh.visible = true;
puff.mesh.position.set(
funnelX + age * 1.45,
4.85 + age * 5.2,
funnelZ + age * 0.25
);
puff.mesh.rotation.y = age * 0.4;
puff.mesh.scale.set(size, size, size);
puff.material.opacity = 0.38 * (1 - age) * (1 - age);
});
// Water highlights.
waveSeeds.forEach((wave, index) => {
const wrappedZ =
((wave.z + elapsed * wave.speed + 90) % 150) - 90;
waveDummy.position.set(
wave.x + Math.sin(elapsed * 0.55 + wave.phase) * 0.28,
0.055,
wrappedZ
);
waveDummy.rotation.set(0, wave.rotation, 0);
waveDummy.scale.set(
wave.width,
0.025,
wave.depth * (0.8 + Math.sin(elapsed + wave.phase) * 0.2)
);
waveDummy.updateMatrix();
waveMesh.setMatrixAt(index, waveDummy.matrix);
});
waveMesh.instanceMatrix.needsUpdate = true;
updateCamera();
renderer.render(scene, camera);
});
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```