Prompt specification
Build or customize the Three.js 3D scene "River pier flow teaching diorama" 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: River pier flow teaching diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/river-pier-flow-teaching-diorama-x1467
Scene Description Prompt:
A voxel river pier with consistent main-flow tracers and clearly illustrative wakes and eddies. One HTML file using Three.js from cdnjs.
Working Three.js Starter (river-pier-flow-teaching-diorama-x1467):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Current — River Pier Flow Study</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: #c8e3e8;
}
#app,
#app canvas {
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 10;
pointer-events: none;
color: #effcff;
}
.panel {
position: absolute;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 18px 19px 16px;
border: 1px solid rgba(207, 247, 255, 0.22);
border-radius: 16px;
background:
linear-gradient(145deg, rgba(8, 31, 43, 0.88), rgba(9, 46, 58, 0.72));
box-shadow:
0 18px 50px rgba(4, 24, 31, 0.24),
inset 0 1px rgba(255, 255, 255, 0.07);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: #86eaff;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #6df5ba;
box-shadow: 0 0 12px rgba(109, 245, 186, 0.95);
}
h1 {
margin: 0;
font-size: clamp(24px, 4vw, 32px);
line-height: 1.03;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 16px;
color: rgba(223, 248, 252, 0.68);
font-size: 12px;
line-height: 1.45;
}
.flow-direction {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 14px;
padding: 10px 12px;
border: 1px solid rgba(93, 228, 255, 0.18);
border-radius: 10px;
background: rgba(72, 205, 235, 0.08);
color: #bcefff;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
}
.flow-arrow {
font-size: 20px;
line-height: 0;
color: #61e7ff;
text-shadow: 0 0 14px rgba(97, 231, 255, 0.7);
}
.legend {
display: grid;
gap: 9px;
}
.legend-row {
display: grid;
grid-template-columns: 13px 1fr auto;
align-items: center;
gap: 9px;
color: rgba(237, 252, 255, 0.88);
font-size: 11px;
}
.legend-row small {
color: rgba(218, 245, 250, 0.42);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 2px;
}
.swatch.main {
background: #58e5ff;
box-shadow: 0 0 10px rgba(88, 229, 255, 0.75);
}
.swatch.wake {
background: #ffb45e;
box-shadow: 0 0 10px rgba(255, 180, 94, 0.7);
}
.swatch.eddy {
background: #ff6fc3;
box-shadow: 0 0 10px rgba(255, 111, 195, 0.7);
}
.status {
position: absolute;
top: 22px;
right: 22px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid rgba(216, 249, 255, 0.2);
border-radius: 999px;
background: rgba(9, 37, 48, 0.72);
color: rgba(235, 252, 255, 0.82);
box-shadow: 0 10px 32px rgba(5, 27, 35, 0.17);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.status strong {
color: #8df0bb;
}
.controls {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 12px;
padding: 10px 12px;
border: 1px solid rgba(216, 249, 255, 0.18);
border-radius: 13px;
background: rgba(9, 37, 48, 0.74);
box-shadow: 0 12px 35px rgba(5, 27, 35, 0.18);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
pointer-events: auto;
}
button {
border: 1px solid rgba(137, 235, 255, 0.25);
border-radius: 8px;
padding: 8px 11px;
background: rgba(74, 205, 234, 0.12);
color: #d8faff;
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 750;
}
button:hover {
background: rgba(74, 205, 234, 0.22);
}
.speed {
display: flex;
align-items: center;
gap: 8px;
color: rgba(226, 250, 254, 0.66);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
input[type="range"] {
width: 88px;
accent-color: #58e5ff;
cursor: pointer;
}
.hint {
position: absolute;
left: 22px;
bottom: 22px;
padding: 9px 12px;
border: 1px solid rgba(216, 249, 255, 0.16);
border-radius: 10px;
background: rgba(9, 37, 48, 0.63);
color: rgba(226, 250, 254, 0.56);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
font-size: 10px;
letter-spacing: 0.035em;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #c8e3e8;
color: #225565;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 0.35s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: min(290px, calc(100vw - 24px));
padding: 14px;
}
.subtitle {
display: none;
}
.status {
top: auto;
right: auto;
left: 12px;
bottom: 61px;
}
.controls {
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div class="hud">
<section class="panel">
<div class="eyebrow">
<span class="live-dot"></span>
Live flow visualization
</div>
<h1>Voxel Current</h1>
<p class="subtitle">
A river-pier flow study using directional tracers, highlighted wake shear,
and two clearly separated recirculation cells.
</p>
<div class="flow-direction">
<span>Main flow direction</span>
<span class="flow-arrow">⟶</span>
</div>
<div class="legend">
<div class="legend-row">
<span class="swatch main"></span>
<span>Main-flow tracers</span>
<small>96</small>
</div>
<div class="legend-row">
<span class="swatch wake"></span>
<span>Wake shear</span>
<small>60</small>
</div>
<div class="legend-row">
<span class="swatch eddy"></span>
<span>Recirculation eddies</span>
<small>44</small>
</div>
</div>
</section>
<div class="status">
<span class="live-dot"></span>
<strong>200 tracers</strong>
<span>· +X current</span>
</div>
<div class="hint">
Drag to orbit · Wheel to zoom · Shift-drag or right-drag to pan · R to reset
</div>
<div class="controls">
<button id="pause" type="button">Pause flow</button>
<label class="speed">
Speed
<input id="speed" type="range" min="0.25" max="2.2" step="0.05" value="1" />
</label>
</div>
</div>
<div id="loading">Building voxel river…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
const loading = document.getElementById("loading");
// -------------------------------------------------------------------------
// Renderer and camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0xc8e3e8);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc8e3e8);
scene.fog = new THREE.Fog(0xc8e3e8, 43, 82);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const controls = {
theta: -0.69,
phi: 0.73,
radius: 31,
target: new THREE.Vector3(0, 0.15, 0),
home: {
theta: -0.69,
phi: 0.73,
radius: 31,
target: new THREE.Vector3(0, 0.15, 0)
}
};
function updateCamera() {
const horizontal = controls.radius * Math.cos(controls.phi);
camera.position.set(
controls.target.x + horizontal * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.sin(controls.phi),
controls.target.z + horizontal * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemi = new THREE.HemisphereLight(0xeafaff, 0x60786c, 2.2);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d5, 3.15);
sun.position.set(-13, 23, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9adfff, 0.8);
fill.position.set(15, 9, -15);
scene.add(fill);
// -------------------------------------------------------------------------
// Utilities
// -------------------------------------------------------------------------
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
function mulberry32(seed) {
return function random() {
let t = (seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const staticRandom = mulberry32(240519);
function setInstanceTransform(
mesh,
index,
x,
y,
z,
sx,
sy,
sz,
rotationY = 0,
color = null
) {
dummy.position.set(x, y, z);
dummy.rotation.set(0, rotationY, 0);
dummy.scale.set(sx, sy, sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (color) {
mesh.setColorAt(index, color);
}
}
function standardBox(color, roughness = 0.75) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0.02
});
}
// -------------------------------------------------------------------------
// River bed and voxel water
// -------------------------------------------------------------------------
const bed = new THREE.Mesh(
new THREE.BoxGeometry(40, 0.28, 25),
standardBox(0x315d62, 0.92)
);
bed.position.y = -0.43;
bed.receiveShadow = true;
scene.add(bed);
const TILE = 1.25;
const WATER_X = 31;
const WATER_Z = 15;
const waterGeometry = new THREE.BoxGeometry(1, 1, 1);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.31,
metalness: 0.08
});
const water = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
WATER_X * WATER_Z
);
water.receiveShadow = true;
water.instanceMatrix.setUsage(THREE.StaticDrawUsage);
let waterIndex = 0;
for (let ix = 0; ix < WATER_X; ix++) {
for (let iz = 0; iz < WATER_Z; iz++) {
const x = (ix - (WATER_X - 1) / 2) * TILE;
const z = (iz - (WATER_Z - 1) / 2) * TILE;
const color = new THREE.Color().setHSL(
0.535 + (staticRandom() - 0.5) * 0.025,
0.63,
0.365 + staticRandom() * 0.055
);
setInstanceTransform(
water,
waterIndex++,
x,
-0.12,
z,
TILE * 0.985,
0.24,
TILE * 0.985,
0,
color
);
}
}
water.instanceMatrix.needsUpdate = true;
water.instanceColor.needsUpdate = true;
scene.add(water);
// -------------------------------------------------------------------------
// Voxel banks
// -------------------------------------------------------------------------
const bankRows = 5;
const bankCount = WATER_X * bankRows * 2;
const bankBase = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
standardBox(0x8c7754, 0.94),
bankCount
);
const bankCap = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
standardBox(0xffffff, 0.92),
bankCount
);
bankBase.receiveShadow = true;
bankCap.receiveShadow = true;
bankBase.castShadow = true;
bankCap.castShadow = true;
let bankIndex = 0;
for (const side of [-1, 1]) {
for (let row = 0; row < bankRows; row++) {
for (let ix = 0; ix < WATER_X; ix++) {
const x = (ix - (WATER_X - 1) / 2) * TILE;
const z = side * (10 + row * TILE);
setInstanceTransform(
bankBase,
bankIndex,
x,
-0.17,
z,
TILE * 0.985,
0.84,
TILE * 0.985
);
const capColor = new THREE.Color(
row === 0 ? 0xd7c18f : 0x789d65
);
const variation = 0.9 + staticRandom() * 0.16;
capColor.multiplyScalar(variation);
setInstanceTransform(
bankCap,
bankIndex,
x,
0.31,
z,
TILE * 0.985,
0.14,
TILE * 0.985,
0,
capColor
);
bankIndex++;
}
}
}
bankBase.instanceMatrix.needsUpdate = true;
bankCap.instanceMatrix.needsUpdate = true;
bankCap.instanceColor.needsUpdate = true;
scene.add(bankBase, bankCap);
// -------------------------------------------------------------------------
// Voxel pier
// -------------------------------------------------------------------------
const woodLight = new THREE.Color(0xc89455);
const woodMid = new THREE.Color(0xb27a3d);
const woodDark = new THREE.Color(0x76502e);
const plankCount = 23;
const planks = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
standardBox(0xffffff, 0.79),
plankCount
);
planks.castShadow = true;
planks.receiveShadow = true;
for (let i = 0; i < plankCount; i++) {
const z = -4.0 + i * 0.62;
const color = woodLight.clone().lerp(woodMid, staticRandom() * 0.7);
setInstanceTransform(
planks,
i,
0,
0.6,
z,
2.45,
0.18,
0.54,
0,
color
);
}
planks.instanceMatrix.needsUpdate = true;
planks.instanceColor.needsUpdate = true;
scene.add(planks);
const stringerMaterial = standardBox(0x805630, 0.86);
for (const x of [-1.08, 1.08]) {
const stringer = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.25, 14.35),
stringerMaterial
);
stringer.position.set(x, 0.42, 3.08);
stringer.castShadow = true;
stringer.receiveShadow = true;
scene.add(stringer);
}
const postPositions = [];
for (let z = -3.7; z <= 9.8; z += 1.9) {
postPositions.push([-1.05, z], [1.05, z]);
}
const posts = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
standardBox(0x6e4b2d, 0.9),
postPositions.length
);
posts.castShadow = true;
posts.receiveShadow = true;
postPositions.forEach(([x, z], index) => {
const color = woodDark.clone().multiplyScalar(0.9 + staticRandom() * 0.2);
setInstanceTransform(
posts,
index,
x,
0.12,
z,
0.3,
1.72,
0.3,
0,
color
);
});
posts.instanceMatrix.needsUpdate = true;
posts.instanceColor.needsUpdate = true;
scene.add(posts);
// Small square bollards at the pier entrance.
for (const x of [-1.05, 1.05]) {
for (const z of [-3.75, 9.75]) {
const bollard = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.55, 0.34),
standardBox(0x6b492d, 0.84)
);
bollard.position.set(x, 0.96, z);
bollard.castShadow = true;
scene.add(bollard);
}
}
// -------------------------------------------------------------------------
// Bank details: trees, rocks, reeds
// -------------------------------------------------------------------------
function addVoxelTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.38, z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 1.65, 0.38),
standardBox(0x775036, 0.95)
);
trunk.position.y = 0.82;
trunk.castShadow = true;
group.add(trunk);
const leafMaterial = standardBox(0x517d51, 0.94);
const leafMaterialLight = standardBox(0x69965b, 0.94);
const leaves = [
[-0.46, 1.72, 0, 1.35, 1.15, 1.2, leafMaterial],
[0.42, 1.78, 0.08, 1.3, 1.2, 1.25, leafMaterialLight],
[0, 2.2, -0.12, 1.45, 1.05, 1.35, leafMaterial],
[-0.34, 2.45, 0.22, 0.95, 0.82, 0.95, leafMaterialLight],
[0.48, 2.38, -0.28, 0.85, 0.85, 0.88, leafMaterial]
];
for (const [lx, ly, lz, sx, sy, sz, material] of leaves) {
const leaf = new THREE.Mesh(
new THREE.BoxGeometry(sx, sy, sz),
material
);
leaf.position.set(lx, ly, lz);
leaf.castShadow = true;
leaf.receiveShadow = true;
group.add(leaf);
}
scene.add(group);
}
addVoxelTree(-15.5, 11.8, 1.1);
addVoxelTree(-8.4, 13.0, 0.85);
addVoxelTree(5.6, 13.3, 1.0);
addVoxelTree(15.0, 11.7, 1.15);
addVoxelTree(-13.5, -11.7, 0.9);
addVoxelTree(-0.7, -13.2, 1.05);
addVoxelTree(12.3, -11.8, 0.9);
const rockMaterial = standardBox(0x87908c, 0.96);
for (let i = 0; i < 15; i++) {
const side = staticRandom() > 0.5 ? 1 : -1;
const rock = new THREE.Mesh(
new THREE.BoxGeometry(
0.3 + staticRandom() * 0.55,
0.2 + staticRandom() * 0.32,
0.3 + staticRandom() * 0.55
),
rockMaterial
);
rock.position.set(
-18 + staticRandom() * 36,
0.43,
side * (9.55 + staticRandom() * 0.55)
);
rock.rotation.y = staticRandom() * Math.PI;
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
const reedMaterial = standardBox(0x638a4d, 0.98);
const reedGeometry = new THREE.BoxGeometry(0.09, 0.8, 0.09);
for (let i = 0; i < 38; i++) {
const side = staticRandom() > 0.5 ? 1 : -1;
const reed = new THREE.Mesh(reedGeometry, reedMaterial);
reed.position.set(
-18.5 + staticRandom() * 37,
0.67,
side * (9.4 + staticRandom() * 0.45)
);
reed.rotation.z = (staticRandom() - 0.5) * 0.22;
reed.castShadow = true;
scene.add(reed);
}
// -------------------------------------------------------------------------
// Static illustrative details: flow arrows and foam
// -------------------------------------------------------------------------
const arrowGeometry = new THREE.BoxGeometry(1, 1, 1);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0xb8f5ff,
transparent: true,
opacity: 0.26,
depthWrite: false,
toneMapped: false
});
const arrowLocations = [
[-14.2, -7.1],
[-9.2, -7.1],
[-14.2, -0.2],
[-9.2, -0.2],
[-14.2, 6.6],
[-9.2, 6.6]
];
const arrows = new THREE.InstancedMesh(
arrowGeometry,
arrowMaterial,
arrowLocations.length * 5
);
let arrowIndex = 0;
for (const [x, z] of arrowLocations) {
const pixels = [
[x - 0.17, z, 0.65, 0.1],
[x + 0.25, z, 0.13, 0.13],
[x + 0.11, z + 0.17, 0.13, 0.13],
[x + 0.11, z - 0.17, 0.13, 0.13],
[x - 0.03, z + 0.29, 0.13, 0.13]
];
for (const [px, pz, sx, sz] of pixels) {
setInstanceTransform(
arrows,
arrowIndex++,
px,
0.035,
pz,
sx,
0.025,
sz
);
}
}
arrows.instanceMatrix.needsUpdate = true;
scene.add(arrows);
const foamCount = 52;
const foam = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: 0xdffff4,
transparent: true,
opacity: 0.42,
depthWrite: false,
toneMapped: false
}),
foamCount
);
for (let i = 0; i < foamCount; i++) {
const alongPier = staticRandom() > 0.38;
const x = alongPier
? (staticRandom() > 0.5 ? 1.45 : -1.45) + (staticRandom() - 0.5) * 0.7
: -1.7 + staticRandom() * 3.2;
const z = -4.4 + staticRandom() * 14;
setInstanceTransform(
foam,
i,
x,
0.03,
z,
0.08 + staticRandom() * 0.13,
0.025,
0.08 + staticRandom() * 0.13
);
}
foam.instanceMatrix.needsUpdate = true;
scene.add(foam);
// -------------------------------------------------------------------------
// Flow particles
// -------------------------------------------------------------------------
const MAIN_COUNT = 96;
const WAKE_COUNT = 60;
const EDDY_COUNT = 44;
const TRAIL_POINTS = 8;
const TRAIL_SAMPLE = 0.065;
const PIER_HALF_WIDTH = 1.38;
const PIER_MIN_Z = -4.05;
const PIER_MAX_Z = 9.1;
const vortices = [
{
x: 2.85,
z: -5.15,
radius: 1.55,
sign: 1,
strength: 1.08
},
{
x: 3.0,
z: 6.7,
radius: 1.28,
sign: -1,
strength: 0.9
}
];
const particles = [];
function makeTrail(x, z) {
const trail = new Float32Array(TRAIL_POINTS * 2);
for (let i = 0; i < TRAIL_POINTS; i++) {
trail[i * 2] = x;
trail[i * 2 + 1] = z;
}
return trail;
}
function randomMainPosition(initial = false) {
let x = initial
? -17.4 + Math.random() * 34.8
: -18.2 - Math.random() * 1.1;
let z = -8.25 + Math.random() * 16.5;
if (!initial && z > PIER_MIN_Z) {
// Leave room upstream for the main stream to split around the pier tip.
x = -18.2 - Math.random() * 1.1;
}
if (Math.abs(x) < 2.2 && z > PIER_MIN_Z) {
x = -2.2;
}
return { x, z };
}
for (let i = 0; i < MAIN_COUNT; i++) {
const position = randomMainPosition(true);
particles.push({
kind: 0,
x: position.x,
z: position.z,
homeZ: position.z,
vx: 1.55,
vz: 0,
trail: makeTrail(position.x, position.z)
});
}
for (let i = 0; i < WAKE_COUNT; i++) {
const x = 1.65 + Math.random() * 12.8;
const z = -6.1 + Math.random() * 14.7;
particles.push({
kind: 1,
x,
z,
vx: 0.7,
vz: 0,
trail: makeTrail(x, z)
});
}
for (let i = 0; i < EDDY_COUNT; i++) {
const vortex = vortices[i % vortices.length];
const radius = 0.35 + Math.random() * (vortex.radius - 0.45);
const angle = Math.random() * Math.PI * 2;
const x = vortex.x + Math.cos(angle) * radius;
const z = vortex.z + Math.sin(angle) * radius;
particles.push({
kind: 2,
x,
z,
vx: 0,
vz: 0,
vortex,
radius,
baseRadius: radius,
angle,
angularSpeed:
vortex.sign * (0.48 + Math.random() * 0.3),
phase: Math.random() * Math.PI * 2,
trail: makeTrail(x, z)
});
}
const TRACE_COLORS = [
new THREE.Color(0x58e5ff),
new THREE.Color(0xffb45e),
new THREE.Color(0xff6fc3)
];
const tracerGeometry = new THREE.BoxGeometry(1, 1, 1);
const tracerMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.88,
depthWrite: false,
toneMapped: false
});
const tracerMesh = new THREE.InstancedMesh(
tracerGeometry,
tracerMaterial,
particles.length * TRAIL_POINTS
);
tracerMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
tracerMesh.frustumCulled = false;
tracerMesh.renderOrder = 4;
scene.add(tracerMesh);
let simulationTime = 0;
let trailAccumulator = 0;
function updateMainParticle(particle, dt) {
particle.vx = 1.55;
particle.vz = 0;
// The main stream approaches from -X and bends around the free end
// of the pier before continuing toward +X.
if (
particle.x < -PIER_HALF_WIDTH &&
particle.x > -10.5 &&
particle.z > PIER_MIN_Z &&
particle.z < PIER_MAX_Z
) {
const approach = clamp(
(-10.5 - particle.x) / (10.5 - PIER_HALF_WIDTH),
0,
1
);
const targetZ = PIER_MIN_Z - 1.05;
const desiredVz = (targetZ - particle.z) * 0.55 * approach;
particle.vz += clamp(desiredVz, -5.5, 0.7);
}
// Downstream, the divided stream gradually spreads back across the river.
if (particle.x > PIER_HALF_WIDTH) {
const recovery = smoothstep(PIER_HALF_WIDTH, 12.5, particle.x);
particle.vz += (particle.homeZ - particle.z) * 0.03 * recovery;
}
particle.x += particle.vx * dt;
particle.z += particle.vz * dt;
// Conservative collision fallback for a few nearly aligned particles.
if (
Math.abs(particle.x) < PIER_HALF_WIDTH &&
particle.z > PIER_MIN_Z &&
particle.z < PIER_MAX_Z
) {
particle.z = PIER_MIN_Z - 0.5;
}
if (particle.x > 18.3) {
const position = randomMainPosition(false);
particle.x = position.x;
particle.z = position.z;
particle.homeZ = position.z;
particle.vx = 1.55;
particle.vz = 0;
particle.trail = makeTrail(position.x, position.z);
}
}
function updateWakeParticle(particle, dt) {
particle.vx = 0.72;
particle.vz = 0;
for (const vortex of vortices) {
const dx = particle.x - vortex.x;
const dz = particle.z - vortex.z;
const radius = Math.max(0.22, Math.hypot(dx, dz));
const falloff = Math.exp(
-(radius * radius) / (vortex.radius * vortex.radius * 2.8)
);
const tangentX = -dz / radius;
const tangentZ = dx / radius;
particle.vx +=
tangentX * vortex.strength * falloff * vortex.sign;
particle.vz +=
tangentZ * vortex.strength * falloff * vortex.sign;
// A mild inward component makes the wake curl visibly.
particle.vx -= (dx / radius) * 0.18 * falloff;
particle.vz -= (dz / radius) * 0.18 * falloff;
}
particle.x += particle.vx * dt;
particle.z += particle.vz * dt;
if (
particle.x > 16.2 ||
particle.x < 1.35 ||
particle.z < -7.5 ||
particle.z > 9
) {
particle.x = 1.6 + Math.random() * 2.2;
particle.z = -6 + Math.random() * 14.2;
particle.trail = makeTrail(particle.x, particle.z);
}
}
function updateEddyParticle(particle, dt) {
particle.angle += particle.angularSpeed * dt;
const radius =
particle.baseRadius +
Math.sin(simulationTime * 0.85 + particle.phase) * 0.075;
const x =
particle.vortex.x + Math.cos(particle.angle) * radius;
const z =
particle.vortex.z + Math.sin(particle.angle) * radius;
particle.vx = (x - particle.x) / Math.max(dt, 0.0001);
particle.vz = (z - particle.z) / Math.max(dt, 0.0001);
particle.x = x;
particle.z = z;
}
function updateParticles(dt) {
simulationTime += dt;
for (const particle of particles) {
if (particle.kind === 0) {
updateMainParticle(particle, dt);
} else if (particle.kind === 1) {
updateWakeParticle(particle, dt);
} else {
updateEddyParticle(particle, dt);
}
}
trailAccumulator += dt;
if (trailAccumulator >= TRAIL_SAMPLE) {
const samples = Math.floor(trailAccumulator / TRAIL_SAMPLE);
trailAccumulator %= TRAIL_SAMPLE;
for (const particle of particles) {
for (let sample = 0; sample < samples; sample++) {
particle.trail.copyWithin(
2,
0,
(TRAIL_POINTS - 1) * 2
);
particle.trail[0] = particle.x;
particle.trail[1] = particle.z;
}
}
}
}
function renderTracers() {
let instance = 0;
for (const particle of particles) {
particle.trail[0] = particle.x;
particle.trail[1] = particle.z;
const baseColor = TRACE_COLORS[particle.kind];
const width = particle.kind === 1 ? 0.085 : 0.075;
for (let point = 0; point < TRAIL_POINTS - 1; point++) {
const x0 = particle.trail[point * 2];
const z0 = particle.trail[point * 2 + 1];
const x1 = particle.trail[(point + 1) * 2];
const z1 = particle.trail[(point + 1) * 2 + 1];
const dx = x1 - x0;
const dz = z1 - z0;
const length = Math.max(0.001, Math.hypot(dx, dz));
const angle = Math.atan2(dx, dz);
dummy.position.set(
(x0 + x1) * 0.5,
0.105,
(z0 + z1) * 0.5
);
dummy.quaternion.setFromAxisAngle(Y_AXIS, angle);
dummy.scale.set(width, 0.035, length);
dummy.updateMatrix();
tracerMesh.setMatrixAt(instance, dummy.matrix);
const fade = 1 - point / TRAIL_POINTS * 0.72;
tempColor.copy(baseColor).multiplyScalar(fade);
tracerMesh.setColorAt(instance, tempColor);
instance++;
}
// Brighter, slightly longer head.
const velocityLength = Math.max(
0.001,
Math.hypot(particle.vx, particle.vz)
);
const headLength =
particle.kind === 2
? 0.17
: particle.kind === 1
? 0.24
: 0.3;
const headAngle = Math.atan2(
particle.vx / velocityLength,
particle.vz / velocityLength
);
dummy.position.set(
particle.x + (particle.vx / velocityLength) * headLength * 0.35,
0.11,
particle.z + (particle.vz / velocityLength) * headLength * 0.35
);
dummy.quaternion.setFromAxisAngle(Y_AXIS, headAngle);
dummy.scale.set(width * 1.38, 0.045, headLength);
dummy.updateMatrix();
tracerMesh.setMatrixAt(instance, dummy.matrix);
tempColor.copy(baseColor).lerp(new THREE.Color(0xffffff), 0.43);
tracerMesh.setColorAt(instance, tempColor);
instance++;
}
tracerMesh.instanceMatrix.needsUpdate = true;
tracerMesh.instanceColor.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Pixel rings marking the two illustrative recirculation cells
// -------------------------------------------------------------------------
const ringCount = vortices.length * 44;
const rings = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: 0xff76c6,
transparent: true,
opacity: 0.38,
depthWrite: false,
toneMapped: false
}),
ringCount
);
let ringIndex = 0;
for (const vortex of vortices) {
for (let i = 0; i < 44; i++) {
const angle = (i / 44) * Math.PI * 2;
const x = vortex.x + Math.cos(angle) * vortex.radius;
const z = vortex.z + Math.sin(angle) * vortex.radius;
setInstanceTransform(
rings,
ringIndex++,
x,
0.045,
z,
0.11,
0.025,
0.11
);
}
}
rings.instanceMatrix.needsUpdate = true;
scene.add(rings);
// -------------------------------------------------------------------------
// Custom camera controls — no external controls addon
// -------------------------------------------------------------------------
const pointer = {
active: false,
mode: "orbit",
id: null,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.mode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) {
return;
}
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointer.mode === "orbit") {
controls.theta -= dx * 0.006;
controls.phi = clamp(controls.phi + dy * 0.005, 0.25, 1.28);
} else {
const scale = controls.radius * 0.0015;
const right = new THREE.Vector3(
Math.cos(controls.theta),
0,
-Math.sin(controls.theta)
);
const forward = new THREE.Vector3(
-Math.sin(controls.theta),
0,
-Math.cos(controls.theta)
);
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(forward, dy * scale);
}
updateCamera();
});
function endPointer(event) {
if (event.pointerId !== pointer.id) {
return;
}
pointer.active = false;
pointer.id = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
17,
54
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
controls.theta = controls.home.theta;
controls.phi = controls.home.phi;
controls.radius = controls.home.radius;
controls.target.copy(controls.home.target);
updateCamera();
}
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
let running = true;
let simulationSpeed = 1;
const pauseButton = document.getElementById("pause");
const speedInput = document.getElementById("speed");
pauseButton.addEventListener("click", () => {
running = !running;
pauseButton.textContent = running ? "Pause flow" : "Resume flow";
});
speedInput.addEventListener("input", () => {
simulationSpeed = Number(speedInput.value);
});
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = width < 700 ? 49 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
let previousTime = performance.now();
function animate(now) {
const frameDelta = Math.min(0.04, (now - previousTime) / 1000);
previousTime = now;
if (running) {
updateParticles(frameDelta * simulationSpeed);
}
renderTracers();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 450);
</script>
</body>
</html>
```