Prompt specification
Build or customize the Three.js 3D scene "Intertidal rock pool tile" 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: Intertidal rock pool tile
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/intertidal-rock-pool-tile-x1257
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing coastal tides. Model layered shoreline rocks with several connected depressions and a movable water plane; changing level must reveal pools and dry ledges without moving the rocks. Low-poly.
Working Three.js Starter (intertidal-rock-pool-tile-x1257):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coastal Tides — Linked Rock Basins</title>
<style>
:root {
--ink: #10363a;
--muted: #527176;
--paper: rgba(246, 241, 224, 0.91);
--line: rgba(16, 54, 58, 0.17);
--accent: #0d7880;
--accent-light: #9bd4cc;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d7d8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#scene.dragging {
cursor: grabbing;
}
.title-card {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
pointer-events: none;
text-shadow: 0 1px 18px rgba(240, 248, 242, 0.5);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--ink);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 2px;
content: "";
background: var(--accent);
}
h1 {
max-width: 440px;
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(36px, 5vw, 68px);
font-weight: 500;
line-height: 0.92;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 360px;
margin: 13px 0 0;
color: #315c62;
font-size: 13px;
line-height: 1.55;
}
.control-panel {
position: fixed;
right: 26px;
bottom: 26px;
z-index: 10;
width: min(340px, calc(100vw - 40px));
padding: 20px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 60px rgba(18, 52, 52, 0.19),
inset 0 1px 0 rgba(255, 255, 255, 0.65);
backdrop-filter: blur(16px) saturate(1.08);
-webkit-backdrop-filter: blur(16px) saturate(1.08);
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 15px;
}
.panel-heading h2 {
margin: 0;
font-size: 14px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
}
.status-dot {
flex: 0 0 auto;
width: 10px;
height: 10px;
margin-top: 3px;
border: 2px solid rgba(13, 120, 128, 0.22);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(13, 120, 128, 0.09);
}
.readout {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-top: 19px;
}
#tide-value {
font-family: Georgia, "Times New Roman", serif;
font-size: 35px;
line-height: 1;
letter-spacing: -0.035em;
}
#tide-state {
max-width: 155px;
padding-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
line-height: 1.35;
text-align: right;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.range-wrap {
margin-top: 16px;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
appearance: none;
background:
linear-gradient(
90deg,
#125b64 0%,
#2a9194 48%,
#a4c9bd 100%
);
cursor: pointer;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
border: 3px solid #f5f1df;
border-radius: 50%;
appearance: none;
background: #0c727a;
box-shadow: 0 2px 8px rgba(10, 48, 51, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #f5f1df;
border-radius: 50%;
background: #0c727a;
box-shadow: 0 2px 8px rgba(10, 48, 51, 0.35);
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
button {
min-height: 39px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.44);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease,
border-color 150ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(13, 120, 128, 0.35);
background: rgba(255, 255, 255, 0.78);
}
button:active {
transform: translateY(0);
}
button.active {
border-color: rgba(13, 120, 128, 0.35);
background: var(--accent-light);
}
.locked-note {
display: flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
}
.lock-icon {
position: relative;
width: 11px;
height: 9px;
border-radius: 2px;
background: #64817c;
}
.lock-icon::before {
position: absolute;
bottom: 7px;
left: 2px;
width: 7px;
height: 7px;
border: 2px solid #64817c;
border-bottom: 0;
border-radius: 5px 5px 0 0;
content: "";
}
.control-hint {
position: fixed;
bottom: 30px;
left: 30px;
z-index: 5;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.34);
border-radius: 9px;
background: rgba(239, 241, 226, 0.66);
color: #315c62;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
backdrop-filter: blur(9px);
pointer-events: none;
}
.basin-key {
position: fixed;
top: 29px;
right: 29px;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.34);
border-radius: 9px;
background: rgba(239, 241, 226, 0.66);
color: #315c62;
font-size: 10px;
font-weight: 700;
backdrop-filter: blur(9px);
pointer-events: none;
}
.basin-key span {
width: 19px;
height: 5px;
border-radius: 50%;
background: #238a91;
box-shadow: 0 0 0 3px rgba(35, 138, 145, 0.12);
}
@media (max-width: 760px) {
.title-card {
top: 18px;
left: 20px;
}
h1 {
font-size: 39px;
}
.subtitle {
display: none;
}
.control-panel {
right: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 16px;
}
.control-hint,
.basin-key {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly coastal tide model"></canvas>
<header class="title-card">
<div class="eyebrow">Interactive tide study</div>
<h1>Coastal<br>Basins</h1>
<p class="subtitle">
Three linked rock pools cut into a layered shelf.
Lower the tide to expose dry ledges and connected pools.
</p>
</header>
<div class="basin-key">
<span></span>
Three connected depressions
</div>
<section class="control-panel" aria-label="Tide controls">
<div class="panel-heading">
<div>
<h2>Water level</h2>
<p>Only the water plane moves.</p>
</div>
<div class="status-dot" aria-hidden="true"></div>
</div>
<div class="readout">
<output id="tide-value" for="tide-slider">+0.85 m</output>
<span id="tide-state">Mid tide · pools visible</span>
</div>
<div class="range-wrap">
<input
id="tide-slider"
type="range"
min="-0.25"
max="2.55"
step="0.01"
value="0.85"
aria-label="Tide level"
>
<div class="range-labels">
<span>Low / exposed</span>
<span>High / flooded</span>
</div>
</div>
<div class="actions">
<button id="auto-tide" type="button" aria-pressed="false">
▶ Animate tide
</button>
<button id="reset-view" type="button">
Reset view
</button>
</div>
<div class="locked-note">
<span class="lock-icon" aria-hidden="true"></span>
Rock geometry remains fixed at every tide level
</div>
</section>
<div class="control-hint">Drag to orbit · Scroll or pinch 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";
const canvas = document.querySelector("#scene");
const slider = document.querySelector("#tide-slider");
const tideValue = document.querySelector("#tide-value");
const tideState = document.querySelector("#tide-state");
const autoButton = document.querySelector("#auto-tide");
const resetButton = document.querySelector("#reset-view");
// ---------------------------------------------------------------------
// Renderer, scene, lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d7d8);
scene.fog = new THREE.Fog(0xb9d7d8, 24, 62);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
130
);
const cameraTarget = new THREE.Vector3(0, -0.08, 0);
const initialCamera = {
radius: 16.4,
theta: 0.67,
phi: 1.02
};
const cameraState = { ...initialCamera };
const hemisphere = new THREE.HemisphereLight(
0xdaf4f4,
0x514536,
2.0
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe8bd, 3.0);
sun.position.set(-9, 15, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.bias = -0.00045;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0x8fd4df, 0.72);
coolFill.position.set(10, 5, -12);
scene.add(coolFill);
// ---------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------
function hash(x, z) {
const n = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return n - Math.floor(n);
}
function segmentDistance(px, pz, ax, az, bx, bz) {
const abx = bx - ax;
const abz = bz - az;
const apx = px - ax;
const apz = pz - az;
const lengthSquared = abx * abx + abz * abz;
const t = lengthSquared > 0
? THREE.MathUtils.clamp(
(apx * abx + apz * abz) / lengthSquared,
0,
1
)
: 0;
const dx = px - (ax + abx * t);
const dz = pz - (az + abz * t);
return Math.hypot(dx, dz);
}
const basins = [
{ x: -3.65, z: 1.15, rx: 1.48, rz: 1.18, depth: 2.38 },
{ x: -0.20, z: 0.25, rx: 1.62, rz: 1.28, depth: 2.48 },
{ x: 3.15, z: -0.72, rx: 1.50, rz: 1.16, depth: 2.40 }
];
/**
* The entire rock shelf is generated once. Its geometry is never animated
* or translated. Only the water mesh's Y position changes.
*/
function rockHeight(x, z) {
const broadNoise =
0.15 * Math.sin(x * 0.73 + z * 0.38) +
0.09 * Math.sin(z * 1.12 - x * 0.27) +
0.055 * Math.sin((x + z) * 1.43);
const rawHeight = 1.38 + broadNoise;
const terrace = Math.round(rawHeight * 3) / 3;
let height = THREE.MathUtils.lerp(rawHeight, terrace, 0.43);
height += 0.035 * Math.sin(x * 2.7 - z * 2.1);
// Three broad, overlapping depressions.
for (const basin of basins) {
const q =
Math.pow((x - basin.x) / basin.rx, 2) +
Math.pow((z - basin.z) / basin.rz, 2);
height -= basin.depth * Math.exp(-1.65 * q);
// Slightly raised, irregular rock rims make the pools legible.
const radius = Math.sqrt(q);
height +=
0.17 *
Math.exp(-Math.pow((radius - 1.30) / 0.25, 2));
}
// Narrow channels physically connect the three basins.
const channelOne = segmentDistance(
x, z,
basins[0].x, basins[0].z,
basins[1].x, basins[1].z
);
const channelTwo = segmentDistance(
x, z,
basins[1].x, basins[1].z,
basins[2].x, basins[2].z
);
height -= 1.80 * Math.exp(-Math.pow(channelOne / 0.64, 2));
height -= 1.88 * Math.exp(-Math.pow(channelTwo / 0.66, 2));
return height;
}
function topRockColor(y, x, z) {
let color;
if (y < -0.15) {
color = new THREE.Color(0x183e40);
} else if (y < 0.38) {
color = new THREE.Color(0x315854);
} else if (y < 0.90) {
color = new THREE.Color(0x53645c);
} else if (y < 1.35) {
color = new THREE.Color(0x747568);
} else if (y < 1.70) {
color = new THREE.Color(0x918a73);
} else {
color = new THREE.Color(0xaaa184);
}
const variation = (hash(x * 1.31 + 7.2, z * 0.87 - 3.4) - 0.5) * 0.10;
color.offsetHSL(0, 0, variation);
return color;
}
function makeTriangleGeometry(positions, colors) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
// ---------------------------------------------------------------------
// Main low-poly rock shelf
// ---------------------------------------------------------------------
const rockGroup = new THREE.Group();
scene.add(rockGroup);
const ringCount = 18;
const segmentCount = 56;
const rings = [];
const center = new THREE.Vector3(0, rockHeight(0, 0), 0);
for (let ring = 1; ring <= ringCount; ring++) {
const radius = ring / ringCount;
const points = [];
for (let segment = 0; segment < segmentCount; segment++) {
const angle =
(segment / segmentCount) * Math.PI * 2;
const outline =
1 +
0.055 * Math.sin(3 * angle + 0.7) +
0.035 * Math.sin(7 * angle - 1.1) +
0.018 * Math.sin(11 * angle + 0.3);
const x =
8.15 *
radius *
outline *
Math.cos(angle);
const z =
5.05 *
radius *
outline *
Math.sin(angle);
points.push(new THREE.Vector3(x, rockHeight(x, z), z));
}
rings.push(points);
}
const topPositions = [];
const topColors = [];
function pushTopTriangle(a, b, c) {
const x = (a.x + b.x + c.x) / 3;
const y = (a.y + b.y + c.y) / 3;
const z = (a.z + b.z + c.z) / 3;
const color = topRockColor(y, x, z);
topPositions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
for (let i = 0; i < 3; i++) {
topColors.push(color.r, color.g, color.b);
}
}
// Center fan.
for (let i = 0; i < segmentCount; i++) {
const next = (i + 1) % segmentCount;
pushTopTriangle(center, rings[0][next], rings[0][i]);
}
// Annular, triangulated rock surface.
for (let ring = 0; ring < rings.length - 1; ring++) {
const inner = rings[ring];
const outer = rings[ring + 1];
for (let i = 0; i < segmentCount; i++) {
const next = (i + 1) % segmentCount;
const a = inner[i];
const b = inner[next];
const c = outer[next];
const d = outer[i];
pushTopTriangle(a, b, c);
pushTopTriangle(a, c, d);
}
}
const topGeometry = makeTriangleGeometry(topPositions, topColors);
const rockMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.94,
metalness: 0.02
});
const topMesh = new THREE.Mesh(topGeometry, rockMaterial);
topMesh.castShadow = true;
topMesh.receiveShadow = true;
rockGroup.add(topMesh);
// ---------------------------------------------------------------------
// Layered vertical rock face
// ---------------------------------------------------------------------
const sidePositions = [];
const sideColors = [];
const strataColors = [
0x95866b,
0x736d5b,
0x4c5c55,
0x304643
];
const boundary = rings[rings.length - 1];
const sideRings = [[], [], [], [], []];
for (let i = 0; i < segmentCount; i++) {
const p = boundary[i];
const y0 = p.y;
const y1 = y0 - 0.38;
const y2 = Math.min(y1 - 0.32, 0.10);
const y3 = -0.62;
const y4 = -1.90;
sideRings[0].push(new THREE.Vector3(p.x, y0, p.z));
sideRings[1].push(new THREE.Vector3(p.x, y1, p.z));
sideRings[2].push(new THREE.Vector3(p.x, y2, p.z));
sideRings[3].push(new THREE.Vector3(p.x, y3, p.z));
sideRings[4].push(new THREE.Vector3(p.x, y4, p.z));
}
function pushSideTriangle(a, b, c, color) {
sidePositions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
for (let i = 0; i < 3; i++) {
sideColors.push(color.r, color.g, color.b);
}
}
for (let band = 0; band < 4; band++) {
for (let i = 0; i < segmentCount; i++) {
const next = (i + 1) % segmentCount;
const upperA = sideRings[band][i];
const upperB = sideRings[band][next];
const lowerB = sideRings[band + 1][next];
const lowerA = sideRings[band + 1][i];
const color = new THREE.Color(strataColors[band]);
const variation =
(hash(i * 0.73 + band * 4.1, band * 2.3) - 0.5) * 0.055;
color.offsetHSL(0, 0, variation);
pushSideTriangle(upperA, upperB, lowerB, color);
pushSideTriangle(upperA, lowerB, lowerA, color);
}
}
// Bottom cap.
const bottomCenter = new THREE.Vector3(0, -1.90, 0);
for (let i = 0; i < segmentCount; i++) {
const next = (i + 1) % segmentCount;
const a = sideRings[4][i];
const b = sideRings[4][next];
const color = new THREE.Color(0x263b39);
pushSideTriangle(bottomCenter, a, b, color);
}
const sideGeometry = makeTriangleGeometry(sidePositions, sideColors);
const sideMesh = new THREE.Mesh(sideGeometry, rockMaterial);
sideMesh.castShadow = true;
sideMesh.receiveShadow = true;
rockGroup.add(sideMesh);
// ---------------------------------------------------------------------
// Static shoreline boulders
// ---------------------------------------------------------------------
function makeBoulder(x, z, sx, sy, sz, seed) {
const geometry = new THREE.IcosahedronGeometry(1, 1);
const positions = geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const px = positions.getX(i);
const py = positions.getY(i);
const pz = positions.getZ(i);
const factor =
1 +
0.12 *
Math.sin(
px * 4.1 +
py * 5.7 +
pz * 3.8 +
seed
);
positions.setXYZ(
i,
px * factor,
py * factor,
pz * factor
);
}
geometry.computeVertexNormals();
const color = new THREE.Color(0x68716a);
color.offsetHSL(
(hash(seed, x) - 0.5) * 0.025,
-0.05,
(hash(z, seed) - 0.5) * 0.12
);
const material = new THREE.MeshStandardMaterial({
color,
flatShading: true,
roughness: 0.96
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, 0.05, z);
mesh.scale.set(sx, sy, sz);
mesh.rotation.set(
hash(x, z) * 0.25,
hash(z, x) * Math.PI,
hash(x + z, seed) * 0.2
);
mesh.castShadow = true;
mesh.receiveShadow = true;
rockGroup.add(mesh);
}
makeBoulder(-6.1, 2.8, 1.15, 0.90, 1.0, 1.2);
makeBoulder(-5.6, -3.0, 0.95, 0.65, 1.25, 2.4);
makeBoulder(5.7, 2.6, 1.25, 0.75, 0.95, 3.7);
makeBoulder(5.2, -3.3, 0.80, 0.60, 1.05, 4.9);
makeBoulder(-7.0, 0.2, 0.72, 0.62, 0.88, 6.1);
// Distant, layered sea stacks.
function makeSeaStack(x, z, scale) {
const group = new THREE.Group();
const colors = [0x837965, 0x625f55, 0x455551, 0x304541];
let y = -2.0;
for (let i = 0; i < 4; i++) {
const radius = scale * (1 - i * 0.14);
const height = scale * (0.46 + i * 0.07);
const geometry = new THREE.CylinderGeometry(
radius * 0.86,
radius,
height,
7,
1,
false
);
const material = new THREE.MeshStandardMaterial({
color: colors[i],
flatShading: true,
roughness: 1
});
const layer = new THREE.Mesh(geometry, material);
layer.position.y = y + height / 2;
layer.rotation.y = i * 0.19;
layer.castShadow = true;
layer.receiveShadow = true;
group.add(layer);
y += height * 0.86;
}
group.position.set(x, 0, z);
scene.add(group);
}
makeSeaStack(-17, -18, 2.1);
makeSeaStack(16, -21, 1.7);
makeSeaStack(-23, 8, 1.35);
// ---------------------------------------------------------------------
// Seabed and fixed tide staff
// ---------------------------------------------------------------------
const seabed = new THREE.Mesh(
new THREE.PlaneGeometry(110, 110),
new THREE.MeshStandardMaterial({
color: 0xb9a982,
roughness: 1
})
);
seabed.rotation.x = -Math.PI / 2;
seabed.position.y = -2.06;
seabed.receiveShadow = true;
scene.add(seabed);
const staffGroup = new THREE.Group();
const staffLight = new THREE.MeshStandardMaterial({
color: 0xeee4c7,
roughness: 0.78
});
const staffDark = new THREE.MeshStandardMaterial({
color: 0x31595b,
roughness: 0.85
});
const staffGeometry = new THREE.CylinderGeometry(
0.065,
0.065,
0.25,
8
);
for (let i = 0; i < 20; i++) {
const segment = new THREE.Mesh(
staffGeometry,
i % 2 === 0 ? staffDark : staffLight
);
segment.position.y = -1.45 + i * 0.25 + 0.125;
segment.castShadow = true;
staffGroup.add(segment);
}
staffGroup.position.set(6.25, 0, 2.25);
scene.add(staffGroup);
// ---------------------------------------------------------------------
// Movable water plane
// ---------------------------------------------------------------------
const waterGeometry = new THREE.PlaneGeometry(84, 84, 32, 32);
waterGeometry.rotateX(-Math.PI / 2);
const waterMaterial = new THREE.MeshPhongMaterial({
color: 0x29959d,
specular: 0xd9ffff,
shininess: 92,
transparent: true,
opacity: 0.76,
depthWrite: false,
side: THREE.DoubleSide,
flatShading: true
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.position.y = 0.85;
water.renderOrder = 3;
scene.add(water);
let tideLevel = 0.85;
let automaticTide = false;
let automaticTime = 0;
let automaticPhase = 0;
function updateTideUI() {
const sign = tideLevel >= 0 ? "+" : "";
tideValue.textContent = `${sign}${tideLevel.toFixed(2)} m`;
if (tideLevel < 0.25) {
tideState.textContent = "Low tide · ledges exposed";
} else if (tideLevel > 2.10) {
tideState.textContent = "High tide · shelf flooded";
} else {
tideState.textContent = "Mid tide · linked pools visible";
}
slider.value = tideLevel.toFixed(2);
}
function setTide(level) {
tideLevel = THREE.MathUtils.clamp(level, -0.25, 2.55);
// The rock mesh is intentionally untouched.
water.position.y = tideLevel;
updateTideUI();
}
function updateAutomaticButton() {
autoButton.textContent = automaticTide
? "Ⅱ Pause tide"
: "▶ Animate tide";
autoButton.classList.toggle("active", automaticTide);
autoButton.setAttribute("aria-pressed", String(automaticTide));
}
slider.addEventListener("input", () => {
automaticTide = false;
updateAutomaticButton();
setTide(Number(slider.value));
});
autoButton.addEventListener("click", () => {
automaticTide = !automaticTide;
if (automaticTide) {
automaticTime = 0;
automaticPhase = Math.asin(
THREE.MathUtils.clamp(
(tideLevel - 1.15) / 1.40,
-1,
1
)
);
}
updateAutomaticButton();
});
// ---------------------------------------------------------------------
// Direct camera controls: orbit, wheel zoom, and pinch zoom
// ---------------------------------------------------------------------
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius *
sinPhi *
Math.sin(cameraState.theta),
cameraTarget.y +
cameraState.radius *
Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius *
sinPhi *
Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.radius = initialCamera.radius;
cameraState.theta = initialCamera.theta;
cameraState.phi = initialCamera.phi;
updateCamera();
}
resetButton.addEventListener("click", resetCamera);
const pointers = new Map();
let lastPinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.classList.add("dragging");
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
lastPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
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) {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.34,
1.39
);
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (lastPinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius *
(lastPinchDistance / distance),
8.5,
29
);
}
lastPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = 0;
}
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8.5,
29
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
const clock = new THREE.Clock();
const waterPositions = waterGeometry.attributes.position;
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (automaticTide) {
automaticTime += delta;
const level =
1.15 +
1.40 *
Math.sin(automaticTime * 0.30 + automaticPhase);
setTide(level);
}
// Small faceted surface motion while preserving the selected tide level.
for (let i = 0; i < waterPositions.count; i++) {
const x = waterPositions.getX(i);
const z = waterPositions.getZ(i);
const wave =
Math.sin(x * 0.72 + elapsed * 1.10) * 0.025 +
Math.cos(z * 0.61 - elapsed * 0.86) * 0.018;
waterPositions.setY(i, wave);
}
waterPositions.needsUpdate = true;
waterGeometry.computeVertexNormals();
updateCamera();
renderer.render(scene, camera);
}
setTide(tideLevel);
updateAutomaticButton();
animate();
</script>
</body>
</html>
```