Prompt specification
Build or customize the Three.js 3D scene "Harbor tide staff comparison" 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: Harbor tide staff comparison
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/harbor-tide-staff-comparison-x1258
Scene Description Prompt:
A stylized harbor tide staff, fixed quay, and floating pontoon; the pontoon rises with water while the quay stays still. Three.js from cdnjs, one HTML file.
Working Three.js Starter (harbor-tide-staff-comparison-x1258):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Harbor Tide — Three.js</title>
<style>
:root {
color-scheme: dark;
--ink: #f4f0e7;
--muted: #b9cbc9;
--accent: #ffb45d;
--sea: #73d4d7;
--panel: rgba(16, 35, 42, 0.78);
--line: rgba(255, 255, 255, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #a8cbd0;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
color: var(--ink);
}
.header {
position: absolute;
top: 30px;
left: 32px;
max-width: 430px;
text-shadow: 0 2px 18px rgba(16, 35, 42, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: var(--accent);
box-shadow: 0 0 12px rgba(255, 180, 93, 0.6);
}
h1 {
margin: 0;
font-size: clamp(31px, 4vw, 54px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin: 12px 0 0;
max-width: 350px;
color: rgba(244, 240, 231, 0.78);
font-size: 13px;
line-height: 1.55;
}
.tide-card {
position: absolute;
top: 28px;
right: 28px;
width: 190px;
padding: 17px 17px 15px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(15, 34, 40, 0.2);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.card-title {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.tide-value {
margin-top: 7px;
font-size: 34px;
font-weight: 650;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.tide-value small {
margin-left: 4px;
color: var(--muted);
font-size: 13px;
font-weight: 600;
letter-spacing: 0;
}
.tide-track {
position: relative;
height: 5px;
margin: 13px 0 12px;
overflow: hidden;
border-radius: 10px;
background: rgba(255, 255, 255, 0.12);
}
.tide-fill {
position: absolute;
inset: 0 auto 0 0;
width: 20%;
border-radius: inherit;
background: linear-gradient(90deg, var(--sea), var(--accent));
transition: width 80ms linear;
}
.trend-row {
display: flex;
justify-content: space-between;
align-items: center;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#trend {
color: var(--accent);
}
.reset {
width: 100%;
margin-top: 14px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--ink);
background: rgba(255, 255, 255, 0.06);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
pointer-events: auto;
transition: background 150ms ease, border-color 150ms ease;
}
.reset:hover {
border-color: rgba(255, 180, 93, 0.55);
background: rgba(255, 180, 93, 0.12);
}
.status {
position: absolute;
right: 28px;
bottom: 26px;
display: flex;
gap: 18px;
align-items: center;
padding: 12px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 12px 30px rgba(15, 34, 40, 0.16);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
font-size: 11px;
}
.status-item {
display: flex;
align-items: center;
gap: 8px;
white-space: nowrap;
}
.status-item strong {
font-variant-numeric: tabular-nums;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--muted);
}
.dot.fixed {
background: #e8dfca;
}
.dot.floating {
background: var(--sea);
box-shadow: 0 0 10px rgba(115, 212, 215, 0.65);
}
.controls {
position: absolute;
left: 32px;
bottom: 28px;
color: rgba(244, 240, 231, 0.76);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
text-shadow: 0 2px 10px rgba(10, 30, 35, 0.45);
}
.controls span {
margin: 0 7px;
color: rgba(244, 240, 231, 0.35);
}
.world-label {
position: absolute;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
background: rgba(18, 38, 44, 0.73);
color: #f5f1e8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -100%);
box-shadow: 0 7px 18px rgba(10, 28, 34, 0.18);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
transition: opacity 150ms ease;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 16px;
background: rgba(255, 255, 255, 0.48);
}
.world-label.floating {
border-color: rgba(115, 212, 215, 0.42);
color: #baf4f4;
}
.world-label.staff {
border-color: rgba(255, 180, 93, 0.42);
color: #ffd09a;
}
@media (max-width: 700px) {
.header {
top: 20px;
left: 20px;
max-width: 270px;
}
h1 {
font-size: 34px;
}
.subtitle {
display: none;
}
.tide-card {
top: 18px;
right: 16px;
width: 145px;
padding: 13px;
}
.tide-value {
font-size: 27px;
}
.status {
right: 16px;
bottom: 16px;
gap: 11px;
padding: 10px 12px;
}
.status-item {
font-size: 9px;
}
.controls {
left: 18px;
bottom: 72px;
max-width: 180px;
line-height: 1.7;
}
.world-label {
display: none;
}
}
</style>
</head>
<body>
<div class="ui">
<header class="header">
<div class="eyebrow">Harbor tide study / Basin 04</div>
<h1>A quay that stays.<br>A pontoon that floats.</h1>
<p class="subtitle">
Watch the water level move through the harbor while the masonry quay,
tide staff, and mooring points remain fixed.
</p>
</header>
<section class="tide-card" aria-live="polite">
<div class="card-title">Current tide</div>
<div class="tide-value">
<span id="tideValue">0.58</span><small>m</small>
</div>
<div class="tide-track">
<div class="tide-fill" id="tideFill"></div>
</div>
<div class="trend-row">
<span>Water level</span>
<span id="trend">Rising</span>
</div>
<button class="reset" id="resetView">Reset camera</button>
</section>
<div class="status">
<div class="status-item">
<i class="dot fixed"></i>
<span>Quay <strong>0.00 m</strong></span>
</div>
<div class="status-item">
<i class="dot floating"></i>
<span>Pontoon <strong id="pontoonValue">+0.58 m</strong></span>
</div>
</div>
<div class="controls">
Drag <span>·</span> Orbit
<span>·</span> Shift/right drag <span>·</span> Pan
<span>·</span> Scroll/pinch <span>·</span> Zoom
</div>
<div class="world-label" id="quayLabel">Fixed quay</div>
<div class="world-label floating" id="pontoonLabel">Floating pontoon</div>
<div class="world-label staff" id="staffLabel">Tide staff</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, camera, and lighting
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xa8cbd0);
scene.fog = new THREE.Fog(0xb6ced0, 27, 68);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xcfe9ed, 0x667064, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffefd2, 3.2);
sun.position.set(-9, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 19;
sun.shadow.camera.bottom = -19;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
sun.target.position.set(1, 0, 0);
scene.add(sun, sun.target);
const fill = new THREE.DirectionalLight(0x80bfd1, 0.85);
fill.position.set(10, 5, -12);
scene.add(fill);
// ------------------------------------------------------------
// Materials and reusable helpers
// ------------------------------------------------------------
const mat = {
stone: new THREE.MeshStandardMaterial({
color: 0x7c8580, roughness: 0.93, metalness: 0
}),
stoneDark: new THREE.MeshStandardMaterial({
color: 0x65716f, roughness: 0.96, metalness: 0
}),
coping: new THREE.MeshStandardMaterial({
color: 0xb8b5a7, roughness: 0.9, metalness: 0
}),
steel: new THREE.MeshStandardMaterial({
color: 0x27383b, roughness: 0.42, metalness: 0.72
}),
darkSteel: new THREE.MeshStandardMaterial({
color: 0x15292d, roughness: 0.5, metalness: 0.65
}),
rope: new THREE.MeshStandardMaterial({
color: 0xc7a86e, roughness: 1, metalness: 0
}),
red: new THREE.MeshStandardMaterial({
color: 0xc94e3d, roughness: 0.76, metalness: 0
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf1ead8, roughness: 0.82, metalness: 0
}),
orange: new THREE.MeshStandardMaterial({
color: 0xffa73e,
roughness: 0.45,
metalness: 0.05,
emissive: 0x7a2f00,
emissiveIntensity: 0.18
}),
blue: new THREE.MeshStandardMaterial({
color: 0x365c67, roughness: 0.7, metalness: 0.06
}),
roof: new THREE.MeshStandardMaterial({
color: 0x9f4f3e, roughness: 0.86, metalness: 0
}),
glass: new THREE.MeshBasicMaterial({ color: 0xffd88d }),
buoy: new THREE.MeshStandardMaterial({
color: 0xdb6148, roughness: 0.55, metalness: 0.08
})
};
const woodMaterials = [
new THREE.MeshStandardMaterial({ color: 0xa97845, roughness: 0.91 }),
new THREE.MeshStandardMaterial({ color: 0xb78650, roughness: 0.9 }),
new THREE.MeshStandardMaterial({ color: 0x996a3d, roughness: 0.94 }),
new THREE.MeshStandardMaterial({ color: 0xc09159, roughness: 0.89 })
];
function box(parent, sx, sy, sz, x, y, z, material, shadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(sx, sy, sz),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, radius, length, x, y, z, material, segments = 16) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function hash(n) {
const x = Math.sin(n * 127.1 + 311.7) * 43758.5453;
return x - Math.floor(x);
}
function makeTextSprite(text, color = "#f6f0df") {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.font = "700 52px ui-sans-serif, system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 64);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(0.58, 0.29, 1);
return sprite;
}
function makeSignTexture(title, subtitle) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#24424a";
ctx.fillRect(0, 0, 512, 160);
ctx.strokeStyle = "#d9d2bd";
ctx.lineWidth = 8;
ctx.strokeRect(12, 12, 488, 136);
ctx.fillStyle = "#f3ead5";
ctx.font = "800 46px ui-sans-serif, system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText(title, 256, 73);
ctx.fillStyle = "#ef9d55";
ctx.font = "700 25px ui-sans-serif, system-ui, sans-serif";
ctx.letterSpacing = "5px";
ctx.fillText(subtitle, 256, 116);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
function makeDeckLabelTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 512, 128);
ctx.fillStyle = "rgba(31, 47, 46, 0.7)";
ctx.fillRect(72, 27, 368, 74);
ctx.strokeStyle = "rgba(246, 235, 209, 0.75)";
ctx.lineWidth = 3;
ctx.strokeRect(72, 27, 368, 74);
ctx.fillStyle = "#f3e8d0";
ctx.font = "800 41px ui-sans-serif, system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("PONTOON 02", 256, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
// ------------------------------------------------------------
// Stylized water with a moving surface
// ------------------------------------------------------------
const waterMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0 }
},
vertexShader: `
uniform float uTime;
varying vec3 vWorldPosition;
varying float vWave;
void main() {
vec3 p = position;
float waveA = sin(p.x * 0.54 + p.y * 0.21 + uTime * 0.85) * 0.060;
float waveB = sin(p.y * 0.78 - uTime * 0.62 + p.x * 0.13) * 0.035;
float waveC = cos((p.x + p.y) * 0.31 - uTime * 0.40) * 0.020;
p.z += waveA + waveB + waveC;
vec4 worldPosition = modelMatrix * vec4(p, 1.0);
vWorldPosition = worldPosition.xyz;
vWave = waveA + waveB + waveC;
gl_Position = projectionMatrix * viewMatrix * worldPosition;
}
`,
fragmentShader: `
uniform float uTime;
varying vec3 vWorldPosition;
varying float vWave;
void main() {
vec3 deep = vec3(0.075, 0.34, 0.39);
vec3 shallow = vec3(0.18, 0.55, 0.57);
float broad = sin(
vWorldPosition.x * 0.13 +
vWorldPosition.z * 0.09 +
uTime * 0.20
) * 0.5 + 0.5;
vec3 color = mix(deep, shallow, broad * 0.55 + vWave * 2.0);
float ribbon = sin(
vWorldPosition.x * 1.55 +
vWorldPosition.z * 0.62 -
uTime * 0.80
);
float crest = smoothstep(0.94, 1.0, ribbon);
color += crest * vec3(0.13, 0.19, 0.17);
float distanceToCamera = distance(cameraPosition, vWorldPosition);
float fogFactor = smoothstep(29.0, 67.0, distanceToCamera);
color = mix(color, vec3(0.64, 0.76, 0.77), fogFactor);
gl_FragColor = vec4(color, 1.0);
}
`
});
const water = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90, 100, 100),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.y = 0.58;
water.receiveShadow = true;
scene.add(water);
// Transparent layer that catches the directional-light shadows.
const waterShadow = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
new THREE.ShadowMaterial({
color: 0x173b40,
opacity: 0.16,
depthWrite: false
})
);
waterShadow.rotation.x = -Math.PI / 2;
waterShadow.position.y = 0.69;
waterShadow.receiveShadow = true;
waterShadow.renderOrder = 2;
scene.add(waterShadow);
// ------------------------------------------------------------
// Fixed quay
// ------------------------------------------------------------
const quay = new THREE.Group();
scene.add(quay);
box(quay, 7.0, 5.2, 18.0, 7.0, 0.55, 0, mat.stoneDark);
box(quay, 7.2, 0.28, 18.2, 7.0, 3.22, 0, mat.coping);
// Individual masonry blocks on the water-facing wall.
const blockMats = [
new THREE.MeshStandardMaterial({ color: 0x78827d, roughness: 0.96 }),
new THREE.MeshStandardMaterial({ color: 0x828b84, roughness: 0.96 }),
new THREE.MeshStandardMaterial({ color: 0x6f7a77, roughness: 0.98 }),
new THREE.MeshStandardMaterial({ color: 0x89918a, roughness: 0.95 })
];
for (let row = 0; row < 6; row++) {
for (let column = 0; column < 12; column++) {
const material = blockMats[Math.floor(hash(row * 19 + column) * 4)];
const y = -1.72 + row * 0.70;
const z = -7.85 + column * 1.43;
box(
quay,
0.15,
0.63,
1.33,
3.45,
y,
z,
material
);
}
}
// Small harbor office on the back half of the quay.
const office = new THREE.Group();
office.position.set(7.7, 3.36, -5.45);
quay.add(office);
box(office, 3.8, 2.55, 4.2, 0, 1.275, 0, mat.blue);
box(office, 4.25, 0.25, 4.65, 0, 2.68, 0, mat.roof);
box(office, 0.55, 1.35, 0.10, -0.9, 0.675, 2.13, mat.darkSteel);
box(office, 1.25, 0.80, 0.08, 0.75, 1.55, 2.13, mat.glass);
for (const x of [-0.95, 0.75]) {
box(office, 0.09, 0.90, 0.09, x, 1.55, 2.18, mat.cream, false);
box(office, 0.09, 0.09, 0.09, x, 1.55, 2.18, mat.cream, false);
}
// Crates and barrels.
for (let i = 0; i < 3; i++) {
const crate = new THREE.Group();
crate.position.set(6.2 + i * 0.95, 3.37, 4.8 + (i % 2) * 0.25);
crate.rotation.y = -0.12 + i * 0.08;
quay.add(crate);
box(crate, 0.85, 0.78, 0.85, 0, 0.39, 0, woodMaterials[i]);
box(crate, 0.90, 0.08, 0.09, 0, 0.38, 0.43, mat.cream);
box(crate, 0.09, 0.08, 0.90, 0.43, 0.38, 0, mat.cream);
}
// Bollards, including the fixed rope connection points.
function addBollard(parent, x, z) {
cylinder(parent, 0.18, 0.46, x, 3.57, z, mat.darkSteel);
cylinder(parent, 0.29, 0.13, x, 3.84, z, mat.darkSteel);
const anchor = new THREE.Object3D();
anchor.position.set(x, 3.78, z);
parent.add(anchor);
return anchor;
}
const quayRopeAnchors = [
addBollard(quay, 4.25, -4.9),
addBollard(quay, 4.25, 4.9)
];
// Rope coils on the quay.
for (let i = 0; i < 3; i++) {
const coil = new THREE.Mesh(
new THREE.TorusGeometry(0.31 + i * 0.055, 0.035, 8, 36),
mat.rope
);
coil.rotation.x = Math.PI / 2;
coil.position.set(5.35, 3.40 + i * 0.025, 5.9);
coil.castShadow = true;
quay.add(coil);
}
// Lamps.
function addLamp(x, z) {
cylinder(quay, 0.055, 2.6, x, 4.66, z, mat.darkSteel, 12);
box(quay, 0.58, 0.10, 0.58, x, 6.03, z, mat.darkSteel);
box(
quay,
0.34,
0.25,
0.34,
x,
5.89,
z,
new THREE.MeshStandardMaterial({
color: 0xffe1a6,
emissive: 0xffad4a,
emissiveIntensity: 0.65,
roughness: 0.5
}),
false
);
}
addLamp(6.15, -7.3);
addLamp(6.15, 6.8);
// Wall ladder.
for (const z of [3.25, 3.95]) {
cylinder(quay, 0.045, 4.65, 3.28, 0.95, z, mat.steel, 10);
}
for (let y = -1.1; y <= 3.0; y += 0.38) {
const rung = cylinder(quay, 0.04, 0.70, 3.27, y, 3.60, mat.steel, 10);
rung.rotation.x = Math.PI / 2;
}
// Sign attached to the quay face.
const quaySign = new THREE.Mesh(
new THREE.PlaneGeometry(2.65, 0.83),
new THREE.MeshBasicMaterial({
map: makeSignTexture("BASIN 04", "FIXED QUAY")
})
);
quaySign.rotation.y = -Math.PI / 2;
quaySign.position.set(3.355, 1.75, 1.25);
quay.add(quaySign);
// ------------------------------------------------------------
// Fixed tide staff and graduated scale
// ------------------------------------------------------------
const staff = new THREE.Group();
scene.add(staff);
const staffZ = -2.30;
box(
staff,
0.16,
5.1,
0.16,
3.34,
1.05,
staffZ,
mat.cream
);
box(
staff,
0.18,
4.25,
0.56,
3.28,
1.20,
staffZ,
mat.cream
);
for (let i = 0; i < 15; i++) {
const y = -0.5 + i * 0.25;
box(
staff,
0.20,
0.245,
0.56,
3.27,
y,
staffZ,
i % 2 === 0 ? mat.red : mat.cream
);
box(
staff,
0.025,
0.025,
0.17,
3.155,
y,
staffZ + 0.16,
mat.darkSteel,
false
);
}
for (let i = 0; i <= 6; i++) {
const label = makeTextSprite(`${i * 0.5.toFixed(1)}`);
label.position.set(3.02, i * 0.5, staffZ + 0.70);
label.scale.set(0.50, 0.25, 1);
staff.add(label);
}
const meterLabel = makeTextSprite("m", "#ffd09a");
meterLabel.position.set(3.02, 3.48, staffZ + 0.70);
meterLabel.scale.set(0.33, 0.18, 1);
staff.add(meterLabel);
// The indicator follows the water rather than the fixed staff.
const tideIndicator = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.045, 10, 36),
mat.orange
);
tideIndicator.rotation.x = Math.PI / 2;
tideIndicator.position.set(3.28, 0.58, staffZ);
tideIndicator.castShadow = true;
scene.add(tideIndicator);
// ------------------------------------------------------------
// Floating pontoon
// ------------------------------------------------------------
const pontoon = new THREE.Group();
pontoon.position.set(1.35, 0.58, 0);
scene.add(pontoon);
box(pontoon, 3.24, 0.22, 6.88, 0, 0.13, 0, mat.darkSteel);
// Wooden deck planks.
const plankCount = 11;
const plankWidth = 3.14 / plankCount;
for (let i = 0; i < plankCount; i++) {
const x = -3.14 / 2 + plankWidth * (i + 0.5);
const material = woodMaterials[Math.floor(hash(i * 7.3) * woodMaterials.length)];
box(
pontoon,
plankWidth - 0.018,
0.16,
6.60,
x,
0.31,
0,
material
);
}
// Edge and end beams.
for (const x of [-1.62, 1.62]) {
box(pontoon, 0.14, 0.31, 6.92, x, 0.25, 0, mat.darkSteel);
}
for (const z of [-3.40, 3.40]) {
box(pontoon, 3.32, 0.31, 0.14, 0, 0.25, z, mat.darkSteel);
}
// Floating drums below the deck.
for (const z of [-2.60, 0, 2.60]) {
const barrel = cylinder(
pontoon,
0.29,
2.82,
0,
-0.10,
z,
mat.blue,
18
);
barrel.rotation.z = Math.PI / 2;
for (const x of [-0.95, 0.95]) {
const band = cylinder(
pontoon,
0.305,
0.07,
x,
-0.10,
z,
mat.darkSteel,
18
);
band.rotation.z = Math.PI / 2;
}
}
// Rubber fenders on the camera-facing side.
for (const z of [-2.45, 0, 2.45]) {
const fender = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.105, 10, 28),
mat.darkSteel
);
fender.rotation.y = Math.PI / 2;
fender.position.set(-1.72, 0.02, z);
fender.castShadow = true;
pontoon.add(fender);
const strap = box(
pontoon,
0.055,
0.58,
0.10,
-1.66,
0.33,
z,
mat.rope
);
strap.castShadow = true;
}
// Deck cleats and their moving anchor points.
function addPontoonCleat(x, z) {
box(pontoon, 0.34, 0.08, 0.22, x, 0.43, z, mat.steel);
const horn = cylinder(
pontoon,
0.055,
0.63,
x,
0.51,
z,
mat.steel,
10
);
horn.rotation.z = Math.PI / 2;
for (const dx of [-0.24, 0.24]) {
cylinder(
pontoon,
0.045,
0.13,
x + dx,
0.48,
z,
mat.steel,
10
);
}
const anchor = new THREE.Object3D();
anchor.position.set(x, 0.53, z);
pontoon.add(anchor);
return anchor;
}
const pontoonRopeAnchors = [
addPontoonCleat(1.18, -2.58),
addPontoonCleat(1.18, 2.58)
];
// Stencil-like label on the deck.
const deckLabel = new THREE.Mesh(
new THREE.PlaneGeometry(2.55, 0.64),
new THREE.MeshBasicMaterial({
map: makeDeckLabelTexture(),
transparent: true,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -2
})
);
deckLabel.rotation.x = -Math.PI / 2;
deckLabel.position.set(0, 0.405, 0.9);
pontoon.add(deckLabel);
// Small safety post.
cylinder(pontoon, 0.065, 1.05, -1.26, 0.90, -3.08, mat.orange, 12);
cylinder(pontoon, 0.15, 0.08, -1.26, 1.43, -3.08, mat.cream, 12);
// ------------------------------------------------------------
// Dynamic mooring ropes
// ------------------------------------------------------------
const ropeLinks = [];
for (let i = 0; i < 2; i++) {
const upper = new THREE.Mesh(
new THREE.CylinderGeometry(0.026, 0.026, 1, 8),
mat.rope
);
const lower = upper.clone();
upper.castShadow = lower.castShadow = true;
scene.add(upper, lower);
ropeLinks.push({ upper, lower });
}
const upAxis = new THREE.Vector3(0, 1, 0);
const tempDirection = new THREE.Vector3();
function positionLink(mesh, a, b) {
tempDirection.subVectors(b, a);
const length = tempDirection.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(upAxis, tempDirection.normalize());
mesh.scale.set(1, length, 1);
}
function updateRopes() {
pontoon.updateMatrixWorld(true);
for (let i = 0; i < ropeLinks.length; i++) {
const a = new THREE.Vector3();
const b = new THREE.Vector3();
pontoonRopeAnchors[i].getWorldPosition(a);
quayRopeAnchors[i].getWorldPosition(b);
const middle = a.clone().add(b).multiplyScalar(0.5);
middle.y -= 0.25;
positionLink(ropeLinks[i].upper, a, middle);
positionLink(ropeLinks[i].lower, middle, b);
}
}
// ------------------------------------------------------------
// Small floating buoys
// ------------------------------------------------------------
const buoys = [];
function addBuoy(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.58, z);
group.scale.setScalar(scale);
scene.add(group);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.29, 0.56, 16),
mat.buoy
);
body.position.y = 0.20;
body.castShadow = true;
group.add(body);
const top = new THREE.Mesh(
new THREE.ConeGeometry(0.20, 0.38, 16),
mat.cream
);
top.position.y = 0.63;
top.castShadow = true;
group.add(top);
cylinder(group, 0.025, 0.52, 0, 0.96, 0, mat.darkSteel, 8);
buoys.push({
group,
phase: hash(x * 3.1 + z) * Math.PI * 2,
baseX: x,
baseZ: z
});
}
addBuoy(-6.1, -3.8, 1.1);
addBuoy(-3.6, 6.0, 0.85);
addBuoy(8.6, -9.2, 0.72);
// ------------------------------------------------------------
// Camera controls implemented directly
// ------------------------------------------------------------
const canvas = renderer.domElement;
const target = new THREE.Vector3(1.4, 0.9, 0);
let theta = 2.34;
let phi = 1.18;
let radius = 20.0;
const initialCamera = {
theta,
phi,
radius,
target: target.clone()
};
function updateCamera() {
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.cos(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.sin(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetCamera() {
theta = initialCamera.theta;
phi = initialCamera.phi;
radius = initialCamera.radius;
target.copy(initialCamera.target);
updateCamera();
}
const pointers = new Map();
let dragMode = "orbit";
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
if (dragMode === "orbit") {
theta -= dx * 0.005;
phi = THREE.MathUtils.clamp(phi - dy * 0.004, 0.42, 1.47);
} else {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const panScale = radius * 0.0014;
target.addScaledVector(right, -dx * panScale);
target.y += dy * panScale;
target.x = THREE.MathUtils.clamp(target.x, -7, 11);
target.y = THREE.MathUtils.clamp(target.y, -1, 7);
target.z = THREE.MathUtils.clamp(target.z, -12, 12);
}
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
const center = {
x: (points[0].x + points[1].x) * 0.5,
y: (points[0].y + points[1].y) * 0.5
};
if (canvas._pinchDistance) {
radius *= canvas._pinchDistance / Math.max(distance, 1);
radius = THREE.MathUtils.clamp(radius, 8.5, 34);
}
canvas._pinchDistance = distance;
canvas._pinchCenter = center;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
canvas._pinchDistance = 0;
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
pointers.clear();
pointers.set(event.pointerId, remaining);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
radius *= Math.exp(event.deltaY * 0.001);
radius = THREE.MathUtils.clamp(radius, 8.5, 34);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetView").addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const quayLabel = document.getElementById("quayLabel");
const pontoonLabel = document.getElementById("pontoonLabel");
const staffLabel = document.getElementById("staffLabel");
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, verticalOffset = 0) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight + verticalOffset;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Animation and tide cycle
// ------------------------------------------------------------
const clock = new THREE.Clock();
const tideValue = document.getElementById("tideValue");
const tideFill = document.getElementById("tideFill");
const trend = document.getElementById("trend");
const pontoonValue = document.getElementById("pontoonValue");
const quayLabelPosition = new THREE.Vector3(4.4, 3.55, -1.1);
const pontoonLabelPosition = new THREE.Vector3();
const staffLabelPosition = new THREE.Vector3(3.1, 3.75, -2.3);
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
// Approximately 20 seconds for a complete tide cycle.
const phase = elapsed * 0.32;
const tide = 0.58 + Math.sin(phase) * 0.56;
const derivative = Math.cos(phase);
water.position.y = tide;
waterShadow.position.y = tide + 0.105;
waterMaterial.uniforms.uTime.value = elapsed;
// The pontoon follows the water surface.
pontoon.position.y = tide;
pontoon.rotation.z = Math.sin(elapsed * 0.72) * 0.008;
pontoon.rotation.x = Math.cos(elapsed * 0.61) * 0.006;
// The tide pointer moves with the water while the staff remains fixed.
tideIndicator.position.y = tide;
for (const buoy of buoys) {
buoy.group.position.y = tide + Math.sin(elapsed * 1.2 + buoy.phase) * 0.035;
buoy.group.position.x = buoy.baseX + Math.sin(elapsed * 0.32 + buoy.phase) * 0.07;
buoy.group.position.z = buoy.baseZ + Math.cos(elapsed * 0.29 + buoy.phase) * 0.05;
buoy.group.rotation.z = Math.sin(elapsed * 1.05 + buoy.phase) * 0.055;
buoy.group.rotation.x = Math.cos(elapsed * 0.88 + buoy.phase) * 0.04;
}
updateRopes();
const displayTide = Math.max(0, tide);
tideValue.textContent = displayTide.toFixed(2);
pontoonValue.textContent = `+${displayTide.toFixed(2)} m`;
tideFill.style.width = `${Math.min(displayTide / 3, 1) * 100}%`;
trend.textContent = derivative >= 0 ? "Rising" : "Falling";
trend.style.color = derivative >= 0 ? "#ffb45d" : "#73d4d7";
pontoonLabelPosition.set(0.15, tide + 0.95, 1.4);
placeLabel(quayLabel, quayLabelPosition, -4);
placeLabel(pontoonLabel, pontoonLabelPosition, -4);
placeLabel(staffLabel, staffLabelPosition, -4);
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera();
animate();
</script>
</body>
</html>
```