Prompt specification
Build or customize the Three.js 3D scene "Boat and river velocity addition" 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: Boat and river velocity addition
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/boat-and-river-velocity-addition-x1426
Scene Description Prompt:
A stylized boat crosses a uniform river current; separate water-relative and ground-relative velocity arrows and trace its path. Three.js from cdnjs, one HTML file.
Working Three.js Starter (boat-and-river-velocity-addition-x1426):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>River Vector Lab</title>
<style>
:root {
--ink: #102a36;
--muted: #55717b;
--panel: rgba(248, 253, 253, 0.88);
--border: rgba(16, 42, 54, 0.12);
--current: #5eead4;
--water: #38bdf8;
--ground: #fbbf24;
--track: #f472b6;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cce9ed;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 14px 40px rgba(17, 55, 68, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 6px;
color: #188a83;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 34px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 10px 0 0;
max-width: 330px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 14px;
}
#status {
min-width: 135px;
color: #24515e;
font-size: 12px;
font-weight: 750;
}
.progress {
position: relative;
flex: 1;
height: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(16, 42, 54, 0.1);
}
#progressFill {
position: absolute;
inset: 0 auto 0 0;
width: 0%;
border-radius: inherit;
background: linear-gradient(90deg, #fbbf24, #f472b6);
transition: width 80ms linear;
}
.legend {
top: 22px;
right: 22px;
width: 245px;
padding: 16px 17px;
border-radius: 18px;
}
.legend-title {
margin-bottom: 11px;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.legend-item {
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 8px;
min-height: 32px;
font-size: 12px;
}
.legend-item strong {
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.legend-line {
position: relative;
height: 3px;
border-radius: 99px;
background: var(--c);
}
.legend-line::after {
content: "";
position: absolute;
top: 50%;
right: -1px;
width: 7px;
height: 7px;
border-top: 2px solid var(--c);
border-right: 2px solid var(--c);
transform: translateY(-50%) rotate(45deg);
}
.legend-line.dashed {
background: repeating-linear-gradient(
90deg,
var(--c) 0 6px,
transparent 6px 10px
);
}
.controls {
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border-radius: 16px;
transform: translateX(-50%);
}
button {
appearance: none;
border: 0;
border-radius: 10px;
padding: 10px 14px;
color: white;
background: #173f4a;
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 800;
transition:
transform 140ms ease,
background 140ms ease;
}
button:hover {
background: #225766;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#replay {
color: #8a3c16;
background: #ffe2ad;
}
#replay:hover {
background: #ffd58a;
}
.rate {
display: flex;
align-items: center;
gap: 8px;
padding: 0 4px 0 8px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
white-space: nowrap;
}
input[type="range"] {
width: 92px;
accent-color: #f472b6;
}
#rateValue {
width: 34px;
color: var(--ink);
font-variant-numeric: tabular-nums;
}
.hint {
position: fixed;
right: 23px;
bottom: 24px;
z-index: 5;
padding: 8px 10px;
border-radius: 9px;
color: rgba(16, 42, 54, 0.66);
background: rgba(248, 253, 253, 0.66);
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
max-width: none;
}
.legend {
top: auto;
right: 12px;
bottom: 82px;
width: 205px;
padding: 12px 14px;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
justify-content: center;
}
.hint {
display: none;
}
.rate {
padding-left: 3px;
}
input[type="range"] {
width: 70px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section class="panel title-panel">
<div class="eyebrow">Kinematics illustration</div>
<h1>River Vector Lab</h1>
<p class="subtitle">
The boat points directly across the river, but the uniform current
carries it downstream. Its ground-relative velocity is the vector sum
of the boat and current velocities.
</p>
<div class="status-row">
<span id="status">Crossing · 0%</span>
<div class="progress">
<div id="progressFill"></div>
</div>
</div>
</section>
<aside class="panel legend">
<div class="legend-title">Velocity vectors</div>
<div class="legend-item">
<span class="legend-line" style="--c: var(--current)"></span>
<span>Uniform current</span>
<strong>1.20</strong>
</div>
<div class="legend-item">
<span class="legend-line" style="--c: var(--water)"></span>
<span>Boat / water</span>
<strong>2.40</strong>
</div>
<div class="legend-item">
<span class="legend-line" style="--c: var(--ground)"></span>
<span>Boat / ground</span>
<strong>2.68</strong>
</div>
<div class="legend-item">
<span class="legend-line dashed" style="--c: var(--track)"></span>
<span>Ground track</span>
<strong>m/s</strong>
</div>
</aside>
<div class="panel controls">
<button id="play" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
<label class="rate">
Speed
<input
id="rate"
type="range"
min="0.5"
max="1.5"
step="0.25"
value="1"
>
<span id="rateValue">1.0×</span>
</label>
</div>
<div class="hint">Drag to orbit · 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";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcce9ed);
scene.fog = new THREE.Fog(0xcce9ed, 32, 62);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xe8fbff, 0x52765c, 2.1);
scene.add(hemisphere);
const sunLight = new THREE.DirectionalLight(0xfff4d8, 3.1);
sunLight.position.set(-12, 22, 10);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -28;
sunLight.shadow.camera.right = 28;
sunLight.shadow.camera.top = 22;
sunLight.shadow.camera.bottom = -22;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 55;
sunLight.shadow.normalBias = 0.025;
scene.add(sunLight);
const COLORS = {
current: 0x5eead4,
water: 0x38bdf8,
ground: 0xfbbf24,
track: 0xf472b6,
hull: 0xf06449,
deck: 0xfff0cf,
cabin: 0xf6c453,
window: 0x164e63,
bank: 0x7cae69,
sand: 0xe8d29e
};
// -------------------------------------------------------------------------
// Motion model
// River flows toward +X. The boat points toward -Z.
// -------------------------------------------------------------------------
const CURRENT_SPEED = 1.2;
const BOAT_SPEED = 2.4;
const START = new THREE.Vector3(-5, 0, 6.7);
const GROUND_VELOCITY = new THREE.Vector3(
CURRENT_SPEED,
0,
-BOAT_SPEED
);
const CROSSING_TIME =
(START.z - -START.z) / BOAT_SPEED;
const GROUND_SPEED = GROUND_VELOCITY.length();
const END = START.clone().addScaledVector(
GROUND_VELOCITY,
CROSSING_TIME
);
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function createLabel(text, color, width = 4.5) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 160;
const context = canvas.getContext("2d");
const radius = 28;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(radius, 8);
context.lineTo(canvas.width - radius, 8);
context.quadraticCurveTo(
canvas.width - 8, 8,
canvas.width - 8, 8 + radius
);
context.lineTo(
canvas.width - 8,
canvas.height - 8 - radius
);
context.quadraticCurveTo(
canvas.width - 8,
canvas.height - 8,
canvas.width - radius,
canvas.height - 8
);
context.lineTo(radius, canvas.height - 8);
context.quadraticCurveTo(
8,
canvas.height - 8,
8,
canvas.height - 8 - radius
);
context.lineTo(8, 8 + radius);
context.quadraticCurveTo(8, 8, radius, 8);
context.closePath();
context.fillStyle = "rgba(13, 38, 49, 0.91)";
context.fill();
context.strokeStyle = color;
context.lineWidth = 5;
context.stroke();
context.fillStyle = color;
context.font = "800 36px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function createArrow(
length,
color,
{
opacity = 1,
thickness = 0.045,
headLength = 0.34,
headRadius = 0.14
} = {}
) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.3,
roughness: 0.42,
metalness: 0.05,
transparent: opacity < 1,
opacity,
depthWrite: opacity >= 1
});
const shaftLength = Math.max(0.05, length - headLength);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
thickness,
thickness,
shaftLength,
10
),
material
);
shaft.position.y = shaftLength / 2;
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(
headRadius,
headLength,
12
),
material
);
head.position.y = length - headLength / 2;
head.castShadow = true;
group.add(head);
group.userData.material = material;
return group;
}
function orientArrow(arrow, direction) {
const up = new THREE.Vector3(0, 1, 0);
arrow.quaternion.setFromUnitVectors(
up,
direction.clone().normalize()
);
}
function seededRandom(seed) {
return function () {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let value = Math.imul(
seed ^ (seed >>> 15),
1 | seed
);
value =
value +
Math.imul(value ^ (value >>> 7), 61 | value) ^
value;
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(1927);
// -------------------------------------------------------------------------
// River and banks
// -------------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x6f9f61,
roughness: 1
});
const ground = new THREE.Mesh(
new THREE.BoxGeometry(60, 0.6, 42),
groundMaterial
);
ground.position.y = -0.52;
ground.receiveShadow = true;
scene.add(ground);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x39a9c1,
roughness: 0.3,
metalness: 0.12
});
const water = new THREE.Mesh(
new THREE.PlaneGeometry(40, 16),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.y = 0;
water.receiveShadow = true;
scene.add(water);
const sandMaterial = new THREE.MeshStandardMaterial({
color: COLORS.sand,
roughness: 0.95
});
const bankMaterial = new THREE.MeshStandardMaterial({
color: COLORS.bank,
roughness: 1
});
for (const side of [-1, 1]) {
const sand = new THREE.Mesh(
new THREE.BoxGeometry(40, 0.22, 1.05),
sandMaterial
);
sand.position.set(0, 0.06, side * 8.42);
sand.receiveShadow = true;
scene.add(sand);
const bank = new THREE.Mesh(
new THREE.BoxGeometry(40, 0.72, 5.5),
bankMaterial
);
bank.position.set(0, 0.05, side * 11.7);
bank.receiveShadow = true;
scene.add(bank);
}
// Subtle animated wave dashes.
const waveCount = 42;
const wavePositions = new Float32Array(waveCount * 2 * 3);
const waveData = [];
for (let i = 0; i < waveCount; i++) {
waveData.push({
x: random() * 38,
z: -7.4 + random() * 14.8,
length: 0.25 + random() * 0.75
});
}
const waveGeometry = new THREE.BufferGeometry();
waveGeometry.setAttribute(
"position",
new THREE.BufferAttribute(wavePositions, 3)
);
const waveMaterial = new THREE.LineBasicMaterial({
color: 0xb7eff5,
transparent: true,
opacity: 0.36
});
const waves = new THREE.LineSegments(
waveGeometry,
waveMaterial
);
waves.position.y = 0.09;
scene.add(waves);
// -------------------------------------------------------------------------
// Stylized trees and rocks
// -------------------------------------------------------------------------
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x815738,
roughness: 1
});
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x3f8158,
roughness: 0.95
}),
new THREE.MeshStandardMaterial({
color: 0x62a768,
roughness: 0.95
}),
new THREE.MeshStandardMaterial({
color: 0x2f7656,
roughness: 0.95
})
];
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x819092,
roughness: 1
});
for (let i = 0; i < 25; i++) {
const side = i % 2 === 0 ? -1 : 1;
const tree = new THREE.Group();
tree.position.set(
-18 + random() * 36,
0.4,
side * (9.3 + random() * 3.3)
);
const scale = 0.75 + random() * 0.85;
tree.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(
0.13,
0.18,
1.35,
7
),
trunkMaterial
);
trunk.position.y = 0.68;
trunk.castShadow = true;
tree.add(trunk);
const crownMaterial =
leafMaterials[Math.floor(random() * leafMaterials.length)];
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(
0.9 + random() * 0.25,
1
),
crownMaterial
);
crown.position.y = 1.75;
crown.scale.set(1, 1.15, 0.92);
crown.castShadow = true;
tree.add(crown);
const top = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.58, 1),
crownMaterial
);
top.position.set(0.12, 2.5, -0.08);
top.castShadow = true;
tree.add(top);
scene.add(tree);
}
for (let i = 0; i < 13; i++) {
const side = i % 2 === 0 ? -1 : 1;
const rock = new THREE.Mesh(
new THREE.IcosahedronGeometry(
0.22 + random() * 0.28,
0
),
rockMaterial
);
rock.position.set(
-17 + random() * 34,
0.28,
side * (8.7 + random() * 0.45)
);
rock.scale.y = 0.6 + random() * 0.45;
rock.rotation.set(
random(),
random(),
random()
);
rock.castShadow = true;
scene.add(rock);
}
// -------------------------------------------------------------------------
// Current arrows
// -------------------------------------------------------------------------
const flowArrows = [];
const flowRows = [-5.4, -1.8, 1.8, 5.3];
for (let row = 0; row < flowRows.length; row++) {
for (let i = 0; i < 4; i++) {
const arrow = createArrow(1.35, COLORS.current, {
opacity: 0.32,
thickness: 0.032,
headLength: 0.28,
headRadius: 0.11
});
orientArrow(
arrow,
new THREE.Vector3(1, 0, 0)
);
arrow.position.set(
-16 + i * 8 + row * 1.7,
0.14,
flowRows[row]
);
arrow.userData.baseX =
0.5 + i * 9 + row * 2.2;
scene.add(arrow);
flowArrows.push(arrow);
}
}
const currentKey = createArrow(
CURRENT_SPEED,
COLORS.current,
{
thickness: 0.05,
headLength: 0.28,
headRadius: 0.14
}
);
orientArrow(
currentKey,
new THREE.Vector3(1, 0, 0)
);
currentKey.position.set(-7.8, 0.22, 4.4);
scene.add(currentKey);
const currentLabel = createLabel(
"CURRENT 1.20 m/s",
"#5eead4",
3.8
);
currentLabel.position.set(-6.9, 0.9, 4.4);
scene.add(currentLabel);
// -------------------------------------------------------------------------
// Ground trajectory and revealing trace
// -------------------------------------------------------------------------
const pathPoints = [];
for (let i = 0; i <= 100; i++) {
const point = START.clone().addScaledVector(
GROUND_VELOCITY,
CROSSING_TIME * (i / 100)
);
point.y = 0.12;
pathPoints.push(point);
}
const guideGeometry = new THREE.BufferGeometry().setFromPoints(
pathPoints
);
const guideMaterial = new THREE.LineDashedMaterial({
color: COLORS.track,
dashSize: 0.28,
gapSize: 0.2,
transparent: true,
opacity: 0.38
});
const guide = new THREE.Line(
guideGeometry,
guideMaterial
);
guide.computeLineDistances();
scene.add(guide);
const pathCurve = new THREE.CatmullRomCurve3(
pathPoints,
false,
"centripetal"
);
const tubularSegments = 96;
const radialSegments = 8;
const traceGeometry = new THREE.TubeGeometry(
pathCurve,
tubularSegments,
0.052,
radialSegments,
false
);
const traceMaterial = new THREE.MeshBasicMaterial({
color: COLORS.track,
transparent: true,
opacity: 0.96
});
const trace = new THREE.Mesh(
traceGeometry,
traceMaterial
);
scene.add(trace);
const trackLabel = createLabel(
"GROUND TRACK",
"#f472b6",
3.1
);
trackLabel.position.set(
END.x + 0.3,
0.8,
END.z - 0.75
);
scene.add(trackLabel);
// -------------------------------------------------------------------------
// Boat
// -------------------------------------------------------------------------
const boat = new THREE.Group();
scene.add(boat);
function makeHullShape(scale = 1) {
const shape = new THREE.Shape();
shape.moveTo(0, 1.65 * scale);
shape.bezierCurveTo(
0.62 * scale,
0.95 * scale,
0.78 * scale,
0.15 * scale,
0.62 * scale,
-0.95 * scale
);
shape.lineTo(
0.42 * scale,
-1.15 * scale
);
shape.lineTo(
-0.42 * scale,
-1.15 * scale
);
shape.lineTo(
-0.62 * scale,
-0.95 * scale
);
shape.bezierCurveTo(
-0.78 * scale,
0.15 * scale,
-0.62 * scale,
0.95 * scale,
0,
1.65 * scale
);
shape.closePath();
return shape;
}
const hullMaterial = new THREE.MeshStandardMaterial({
color: COLORS.hull,
roughness: 0.44,
metalness: 0.06
});
const hullGeometry = new THREE.ExtrudeGeometry(
makeHullShape(),
{
depth: 0.42,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.08,
bevelThickness: 0.08,
curveSegments: 16
}
);
hullGeometry.rotateX(-Math.PI / 2);
const hull = new THREE.Mesh(
hullGeometry,
hullMaterial
);
hull.position.y = 0.03;
hull.castShadow = true;
hull.receiveShadow = true;
boat.add(hull);
const deckGeometry = new THREE.ExtrudeGeometry(
makeHullShape(0.88),
{
depth: 0.055,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.025,
bevelThickness: 0.025,
curveSegments: 16
}
);
deckGeometry.rotateX(-Math.PI / 2);
const deck = new THREE.Mesh(
deckGeometry,
new THREE.MeshStandardMaterial({
color: COLORS.deck,
roughness: 0.72
})
);
deck.position.y = 0.49;
deck.castShadow = true;
boat.add(deck);
const cabin = new THREE.Mesh(
new THREE.BoxGeometry(0.9, 0.62, 0.92),
new THREE.MeshStandardMaterial({
color: COLORS.cabin,
roughness: 0.68
})
);
cabin.position.set(0, 0.82, -0.08);
cabin.castShadow = true;
boat.add(cabin);
const roof = new THREE.Mesh(
new THREE.BoxGeometry(1.08, 0.13, 1.08),
new THREE.MeshStandardMaterial({
color: 0xfff4d8,
roughness: 0.75
})
);
roof.position.set(0, 1.18, -0.08);
roof.castShadow = true;
boat.add(roof);
const windowMaterial = new THREE.MeshStandardMaterial({
color: COLORS.window,
roughness: 0.18,
metalness: 0.28
});
const frontWindow = new THREE.Mesh(
new THREE.BoxGeometry(0.56, 0.26, 0.035),
windowMaterial
);
frontWindow.position.set(0, 0.88, -0.56);
boat.add(frontWindow);
for (const side of [-1, 1]) {
const sideWindow = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.25, 0.5),
windowMaterial
);
sideWindow.position.set(
side * 0.468,
0.88,
-0.09
);
boat.add(sideWindow);
}
const mast = new THREE.Mesh(
new THREE.CylinderGeometry(
0.027,
0.035,
1.05,
8
),
trunkMaterial
);
mast.position.set(0, 1.68, 0.22);
mast.castShadow = true;
boat.add(mast);
const flagGeometry = new THREE.BufferGeometry();
flagGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
0, 0, 0,
0.72, -0.18, 0,
0, -0.4, 0
],
3
)
);
flagGeometry.computeVertexNormals();
const flag = new THREE.Mesh(
flagGeometry,
new THREE.MeshBasicMaterial({
color: COLORS.hull,
side: THREE.DoubleSide
})
);
flag.position.set(0, 2.12, 0.22);
boat.add(flag);
// -------------------------------------------------------------------------
// Boat-relative vector arrows
// -------------------------------------------------------------------------
const waterArrow = createArrow(
BOAT_SPEED * 0.9,
COLORS.water,
{
thickness: 0.055,
headLength: 0.38,
headRadius: 0.17
}
);
orientArrow(
waterArrow,
new THREE.Vector3(0, 0, -1)
);
scene.add(waterArrow);
const groundArrow = createArrow(
GROUND_SPEED * 0.9,
COLORS.ground,
{
thickness: 0.055,
headLength: 0.38,
headRadius: 0.17
}
);
orientArrow(
groundArrow,
GROUND_VELOCITY
);
scene.add(groundArrow);
const waterLabel = createLabel(
"WATER-RELATIVE 2.40 m/s",
"#38bdf8",
4.8
);
scene.add(waterLabel);
const groundLabel = createLabel(
"GROUND-RELATIVE 2.68 m/s",
"#fbbf24",
5.1
);
scene.add(groundLabel);
// -------------------------------------------------------------------------
// Wake particles
// -------------------------------------------------------------------------
const WAKE_COUNT = 64;
const wakeGeometry = new THREE.SphereGeometry(
0.075,
7,
5
);
const wakeMaterial = new THREE.MeshBasicMaterial({
color: 0xeaffff,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const wakeMesh = new THREE.InstancedMesh(
wakeGeometry,
wakeMaterial,
WAKE_COUNT
);
wakeMesh.instanceMatrix.setUsage(
THREE.DynamicDrawUsage
);
wakeMesh.frustumCulled = false;
scene.add(wakeMesh);
const wakeParticles = Array.from(
{ length: WAKE_COUNT },
() => ({
active: false,
x: 0,
y: 0.14,
z: 0,
vx: 0,
vz: 0,
life: 0,
maxLife: 1
})
);
const dummy = new THREE.Object3D();
let wakeCursor = 0;
let wakeSpawnAccumulator = 0;
function spawnWake() {
const particle = wakeParticles[wakeCursor];
wakeCursor = (wakeCursor + 1) % WAKE_COUNT;
particle.active = true;
particle.x = boat.position.x + (Math.random() - 0.5) * 0.65;
particle.y = 0.15;
particle.z = boat.position.z + 0.9;
particle.vx = CURRENT_SPEED;
particle.vz = 0.45 + Math.random() * 0.65;
particle.life = 1.25 + Math.random() * 0.8;
particle.maxLife = particle.life;
}
function updateWake(dt) {
for (let i = 0; i < WAKE_COUNT; i++) {
const particle = wakeParticles[i];
if (particle.active) {
particle.life -= dt;
if (particle.life <= 0) {
particle.active = false;
} else {
particle.x += particle.vx * dt;
particle.z += particle.vz * dt;
}
}
if (particle.active) {
const normalizedLife = particle.life / particle.maxLife;
const scale =
0.4 + normalizedLife * 0.9;
dummy.position.set(
particle.x,
particle.y,
particle.z
);
dummy.scale.set(
scale,
0.32 * scale,
scale
);
} else {
dummy.position.set(0, -20, 0);
dummy.scale.setScalar(0);
}
dummy.updateMatrix();
wakeMesh.setMatrixAt(i, dummy.matrix);
}
wakeMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Decorative sky
// -------------------------------------------------------------------------
const sun = new THREE.Mesh(
new THREE.SphereGeometry(2.1, 24, 16),
new THREE.MeshBasicMaterial({
color: 0xfff2bd
})
);
sun.position.set(-15, 15, -24);
scene.add(sun);
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const clouds = [];
for (let i = 0; i < 5; i++) {
const cloud = new THREE.Group();
cloud.position.set(
-18 + i * 8 + random() * 3,
10 + random() * 3,
-18 - random() * 10
);
for (let j = 0; j < 4; j++) {
const puff = new THREE.Mesh(
new THREE.IcosahedronGeometry(
1.15 + random() * 0.6,
2
),
cloudMaterial
);
puff.position.set(
j * 1.15 - 1.7,
random() * 0.45,
random() * 0.5
);
puff.scale.y = 0.65;
cloud.add(puff);
}
cloud.userData.baseX = cloud.position.x;
scene.add(cloud);
clouds.push(cloud);
}
// -------------------------------------------------------------------------
// Custom camera controls — no external control add-ons
// -------------------------------------------------------------------------
const orbit = {
yaw: 0.67,
pitch: 0.78,
radius: 23,
target: new THREE.Vector3(-1.5, 0, 0)
};
function updateCamera() {
const sinPitch = Math.sin(orbit.pitch);
const cosPitch = Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x +
orbit.radius * sinPitch * Math.sin(orbit.yaw),
orbit.target.y +
orbit.radius * cosPitch,
orbit.target.z +
orbit.radius * sinPitch * Math.cos(orbit.yaw)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.yaw = 0.67;
orbit.pitch = 0.78;
orbit.radius = 23;
orbit.target.set(-1.5, 0, 0);
updateCamera();
}
let dragState = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragState = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY
};
renderer.domElement.setPointerCapture(
event.pointerId
);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragState || dragState.pointerId !== event.pointerId) {
return;
}
const deltaX = event.clientX - dragState.x;
const deltaY = event.clientY - dragState.y;
orbit.yaw -= deltaX * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + deltaY * 0.004,
0.28,
1.38
);
dragState.x = event.clientX;
dragState.y = event.clientY;
updateCamera();
});
function endDrag(event) {
if (
dragState &&
dragState.pointerId === event.pointerId
) {
dragState = null;
}
}
renderer.domElement.addEventListener(
"pointerup",
endDrag
);
renderer.domElement.addEventListener(
"pointercancel",
endDrag
);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
12,
38
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener(
"dblclick",
resetCamera
);
renderer.domElement.addEventListener(
"contextmenu",
(event) => event.preventDefault()
);
updateCamera();
// -------------------------------------------------------------------------
// Simulation and UI
// -------------------------------------------------------------------------
const playButton = document.querySelector("#play");
const replayButton = document.querySelector("#replay");
const rateInput = document.querySelector("#rate");
const rateValue = document.querySelector("#rateValue");
const status = document.querySelector("#status");
const progressFill = document.querySelector("#progressFill");
let simulationTime = 0;
let playbackRate = 1;
let running = true;
let completed = false;
let flowTime = 0;
function resetCrossing(autoRun = true) {
simulationTime = 0;
running = autoRun;
completed = false;
playButton.textContent = autoRun ? "Pause" : "Paused";
for (const particle of wakeParticles) {
particle.active = false;
}
updateWake(0);
}
playButton.addEventListener("click", () => {
if (completed) {
resetCrossing(true);
return;
}
running = !running;
playButton.textContent = running ? "Pause" : "Resume";
});
replayButton.addEventListener("click", () => {
resetCrossing(true);
});
rateInput.addEventListener("input", () => {
playbackRate = Number(rateInput.value);
rateValue.textContent =
playbackRate.toFixed(1) + "×";
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
flowTime += dt;
if (running && !completed) {
simulationTime += dt * playbackRate;
if (simulationTime >= CROSSING_TIME) {
simulationTime = CROSSING_TIME;
completed = true;
running = false;
playButton.textContent = "Replay";
}
}
const progress = THREE.MathUtils.clamp(
simulationTime / CROSSING_TIME,
0,
1
);
// Boat position follows the vector sum:
// v_ground = v_boat/water + v_current.
boat.position.set(
START.x + GROUND_VELOCITY.x * simulationTime,
0.13 + Math.sin(flowTime * 2.1) * 0.025,
START.z + GROUND_VELOCITY.z * simulationTime
);
boat.rotation.z =
Math.sin(flowTime * 1.7) * 0.022;
boat.rotation.x =
Math.sin(flowTime * 2.3 + 0.8) * 0.01;
// Update the boat-relative arrows.
waterArrow.position.set(
boat.position.x,
1.65,
boat.position.z
);
groundArrow.position.set(
boat.position.x,
2.72,
boat.position.z
);
waterLabel.position.set(
boat.position.x - 1.55,
1.68,
boat.position.z - 1.25
);
groundLabel.position.set(
boat.position.x + 1.7,
2.7,
boat.position.z - 1.18
);
// Reveal the solid trace behind the moving boat.
const visibleSegments = Math.floor(
progress * tubularSegments
);
const indicesPerSegment = radialSegments * 6;
traceGeometry.setDrawRange(
0,
visibleSegments * indicesPerSegment
);
// Current field arrows.
for (const arrow of flowArrows) {
arrow.position.x =
-18 +
((arrow.userData.baseX + flowTime * CURRENT_SPEED) % 36);
}
// Animate wave dashes.
for (let i = 0; i < waveCount; i++) {
const wave = waveData[i];
const x =
((wave.x + flowTime * 0.38) % 38) - 19;
const offset = i * 6;
wavePositions[offset] = x - wave.length / 2;
wavePositions[offset + 1] = 0;
wavePositions[offset + 2] = wave.z;
wavePositions[offset + 3] = x + wave.length / 2;
wavePositions[offset + 4] = 0;
wavePositions[offset + 5] = wave.z;
}
waveGeometry.attributes.position.needsUpdate = true;
// Wake particles.
if (running && !completed) {
wakeSpawnAccumulator += dt * playbackRate * 26;
while (wakeSpawnAccumulator >= 1) {
spawnWake();
wakeSpawnAccumulator -= 1;
}
for (const particle of wakeParticles) {
if (!particle.active) continue;
particle.x += particle.vx * dt * playbackRate;
particle.z += particle.vz * dt * playbackRate;
particle.life -= dt * playbackRate;
}
}
updateWake(dt * playbackRate);
// Gentle cloud drift.
for (let i = 0; i < clouds.length; i++) {
clouds[i].position.x =
clouds[i].userData.baseX +
Math.sin(flowTime * 0.08 + i) * 0.65;
}
progressFill.style.width = `${progress * 100}%`;
if (completed) {
status.textContent = "Crossing complete";
} else if (running) {
status.textContent =
`Crossing · ${Math.round(progress * 100)}%`;
} else {
status.textContent =
`Paused · ${Math.round(progress * 100)}%`;
}
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)
);
});
resetCrossing(true);
animate();
</script>
</body>
</html>
```