Prompt specification
Build or customize the Three.js 3D scene "Coastal basalt walkway 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: Coastal basalt walkway diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/coastal-basalt-walkway-diorama-x1127
Scene Description Prompt:
A voxel basalt coast has stepped columns, tide pools, and a visitor walkway above the water. One HTML file using Three.js from cdnjs.
Working Three.js Starter (coastal-basalt-walkway-diorama-x1127):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basalt Tideline</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #a9d4dc;
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;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 48%, rgba(8, 31, 37, 0.22) 100%),
linear-gradient(to bottom, rgba(255, 255, 255, 0.05), transparent 30%);
}
.title-card {
position: fixed;
top: 28px;
left: 30px;
max-width: min(430px, calc(100vw - 60px));
padding: 17px 20px 18px;
color: #f5fbfa;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 14px;
background: rgba(12, 37, 43, 0.66);
box-shadow: 0 12px 38px rgba(5, 27, 34, 0.18);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #8edbd5;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 4vw, 37px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1;
}
.subtitle {
margin: 10px 0 0;
max-width: 330px;
color: rgba(239, 251, 250, 0.72);
font-size: 12px;
line-height: 1.5;
}
.controls {
position: fixed;
right: 24px;
bottom: 23px;
display: flex;
align-items: center;
gap: 12px;
color: rgba(245, 252, 251, 0.82);
font-size: 11px;
pointer-events: none;
}
button {
padding: 9px 13px;
color: #f4fbfa;
font: inherit;
font-size: 11px;
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 999px;
background: rgba(12, 37, 43, 0.7);
cursor: pointer;
pointer-events: auto;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(19, 58, 65, 0.9);
transform: translateY(-1px);
}
.hint {
padding: 9px 12px;
border-radius: 999px;
background: rgba(12, 37, 43, 0.48);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
@media (max-width: 700px) {
.title-card {
top: 16px;
left: 16px;
max-width: calc(100vw - 32px);
}
.subtitle {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div class="vignette"></div>
<section class="title-card">
<div class="eyebrow">North Atlantic · Low Tide</div>
<h1>Basalt Tideline</h1>
<p class="subtitle">
Hexagonal volcanic columns, sheltered tide pools, and an elevated coastal
walkway above the restless water.
</p>
</section>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom · Shift-drag to pan</span>
<button id="resetCamera" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xa9d7df);
scene.fog = new THREE.Fog(0xa9d7df, 38, 100);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
180
);
const clock = new THREE.Clock();
const up = new THREE.Vector3(0, 1, 0);
const forwardAxis = new THREE.Vector3(0, 0, 1);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xc9efff,
0x30463e,
1.75
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.25);
sun.position.set(-12, 24, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -32;
sun.shadow.camera.right = 32;
sun.shadow.camera.top = 32;
sun.shadow.camera.bottom = -32;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 75;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x77c7d8, 0.65);
fill.position.set(16, 9, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const basaltMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0.025,
flatShading: true
});
const poolFloorMaterial = new THREE.MeshStandardMaterial({
color: 0x314a3d,
roughness: 1,
metalness: 0
});
const poolWaterMaterial = new THREE.MeshPhysicalMaterial({
color: 0x2aa6a5,
roughness: 0.15,
metalness: 0.12,
clearcoat: 1,
clearcoatRoughness: 0.12,
transparent: true,
opacity: 0.86,
depthWrite: false,
side: THREE.DoubleSide
});
const oceanMaterial = new THREE.MeshPhysicalMaterial({
color: 0x16798c,
roughness: 0.27,
metalness: 0.1,
clearcoat: 0.9,
clearcoatRoughness: 0.2,
transparent: true,
opacity: 0.88,
depthWrite: false,
side: THREE.DoubleSide
});
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.78,
metalness: 0
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
color: 0x4a3026,
roughness: 0.88,
metalness: 0
});
const poolRockMaterial = new THREE.MeshStandardMaterial({
color: 0x343d3d,
roughness: 0.98,
metalness: 0,
flatShading: true
});
const mossMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0
});
// -------------------------------------------------------------------------
// Ocean
// -------------------------------------------------------------------------
const oceanGeometry = new THREE.PlaneGeometry(120, 120, 72, 72);
oceanGeometry.rotateX(-Math.PI / 2);
const oceanPositions = oceanGeometry.attributes.position;
const oceanBase = new Float32Array(oceanPositions.array);
const ocean = new THREE.Mesh(oceanGeometry, oceanMaterial);
ocean.receiveShadow = true;
ocean.renderOrder = 0;
scene.add(ocean);
const seaFloor = new THREE.Mesh(
new THREE.PlaneGeometry(120, 120),
new THREE.MeshStandardMaterial({
color: 0x163e42,
roughness: 1,
metalness: 0
})
);
seaFloor.rotation.x = -Math.PI / 2;
seaFloor.position.y = -3.35;
seaFloor.receiveShadow = true;
scene.add(seaFloor);
// -------------------------------------------------------------------------
// Tide pools
// -------------------------------------------------------------------------
const pools = [
{
x: -4.8,
z: 3.0,
r: 2.25,
level: 1.05,
phase: 0.1,
rings: []
},
{
x: -2.2,
z: -5.8,
r: 1.9,
level: 2.05,
phase: 0.42,
rings: []
},
{
x: -10.2,
z: 5.2,
r: 1.65,
level: 1.55,
phase: 0.72,
rings: []
}
];
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const rockGeometry = new THREE.BoxGeometry(1, 1, 1);
for (const pool of pools) {
const floor = new THREE.Mesh(
new THREE.CircleGeometry(pool.r * 1.04, 32),
poolFloorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(pool.x, pool.level - 0.16, pool.z);
floor.receiveShadow = true;
scene.add(floor);
const water = new THREE.Mesh(
new THREE.CircleGeometry(pool.r * 0.96, 40),
poolWaterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.set(pool.x, pool.level, pool.z);
water.receiveShadow = true;
water.renderOrder = 2;
scene.add(water);
pool.water = water;
for (let i = 0; i < 15; i++) {
const angle = (i / 15) * Math.PI * 2 + (i % 3) * 0.09;
const radius = pool.r * (0.86 + (i % 2) * 0.06);
const rock = new THREE.Mesh(rockGeometry, poolRockMaterial);
rock.position.set(
pool.x + Math.cos(angle) * radius,
pool.level + 0.12 + (i % 3) * 0.025,
pool.z + Math.sin(angle) * radius
);
rock.scale.set(
0.38 + (i % 4) * 0.055,
0.22 + (i % 2) * 0.07,
0.35 + (i % 3) * 0.07
);
rock.rotation.set(
(i % 3) * 0.08,
angle + (i % 2) * 0.25,
(i % 4) * 0.025
);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
for (let i = 0; i < 3; i++) {
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xc6ffff,
transparent: true,
opacity: 0.3,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.5, 0.018, 6, 48),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(pool.x, pool.level + 0.025, pool.z);
ring.renderOrder = 4;
scene.add(ring);
pool.rings.push(ring);
}
}
// -------------------------------------------------------------------------
// Stepped basalt columns
// -------------------------------------------------------------------------
function hash(x, z) {
const value = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return value - Math.floor(value);
}
const columnTransforms = [];
const columnColors = [];
const columnTops = [];
const basaltPalette = [
0x273236,
0x303a3d,
0x394244,
0x222b2e,
0x41494a
];
const columnBottom = -3.15;
const spacing = 2.13;
for (let gx = -17; gx <= 12; gx++) {
for (let gz = -14; gz <= 12; gz++) {
const x = gx * spacing + (hash(gx, gz) - 0.5) * 0.28;
const z = gz * spacing + (hash(gz + 4, gx - 8) - 0.5) * 0.28;
let rawHeight =
5.4 -
x * 0.27 -
z * 0.21 +
(hash(gx * 0.7, gz * 1.1) - 0.5) * 0.9;
rawHeight = THREE.MathUtils.clamp(rawHeight, 0, 7.2);
if (rawHeight < 0.16) {
continue;
}
let top = Math.max(0.36, Math.round(rawHeight / 0.72) * 0.72);
// Pool basins cut down through the surrounding columns.
let insidePool = false;
for (const pool of pools) {
const distance = Math.hypot(x - pool.x, z - pool.z);
if (distance < pool.r * 0.72) {
insidePool = true;
break;
}
if (distance < pool.r * 1.38) {
top = Math.min(
top,
pool.level +
0.48 +
Math.max(0, distance - pool.r * 0.72) * 0.42
);
}
}
if (insidePool) {
continue;
}
const baseRadius = 0.96 + hash(gx + 19, gz - 13) * 0.09;
const totalHeight = top - columnBottom;
const segmentCount = Math.ceil(totalHeight / 1.05);
const segmentLength = totalHeight / segmentCount;
for (let segment = 0; segment < segmentCount; segment++) {
const random = hash(gx * 13 + segment, gz * 17 - segment);
const radius =
baseRadius *
(1 + (random - 0.5) * 0.075 - segment * 0.004);
const visibleHeight = Math.max(0.25, segmentLength - 0.045);
const y = columnBottom + (segment + 0.5) * segmentLength;
const rotation =
Math.floor(hash(gx + segment, gz) * 6) * (Math.PI / 3) +
(random - 0.5) * 0.08;
const dummy = new THREE.Object3D();
dummy.position.set(x, y, z);
dummy.rotation.set(0, rotation, 0);
dummy.scale.set(radius, visibleHeight, radius);
dummy.updateMatrix();
columnTransforms.push(dummy.matrix.clone());
const color = new THREE.Color(
basaltPalette[
Math.floor(hash(gx + segment * 2, gz - segment) * basaltPalette.length)
]
);
color.multiplyScalar(0.87 + random * 0.2);
if (segment === segmentCount - 1) {
color.lerp(new THREE.Color(0x667174), 0.12);
}
columnColors.push(color);
}
columnTops.push({
x,
y: top - 0.03,
z,
random: hash(gx - 7, gz + 5)
});
}
}
const columnGeometry = new THREE.CylinderGeometry(
1,
1,
1,
6,
1,
false
);
columnGeometry.rotateY(Math.PI / 6);
const columns = new THREE.InstancedMesh(
columnGeometry,
basaltMaterial,
columnTransforms.length
);
for (let i = 0; i < columnTransforms.length; i++) {
columns.setMatrixAt(i, columnTransforms[i]);
columns.setColorAt(i, columnColors[i]);
}
columns.instanceMatrix.needsUpdate = true;
columns.instanceColor.needsUpdate = true;
columns.castShadow = true;
columns.receiveShadow = true;
scene.add(columns);
// Sparse voxel moss on the high terraces.
const mossTransforms = [];
const mossColors = [];
for (const top of columnTops) {
if (top.y < 3.8 || top.random < 0.28) {
continue;
}
const count = top.random > 0.76 ? 2 : 1;
for (let i = 0; i < count; i++) {
const dummy = new THREE.Object3D();
const offsetX = (hash(top.x + i, top.z) - 0.5) * 0.8;
const offsetZ = (hash(top.z - i, top.x) - 0.5) * 0.8;
dummy.position.set(top.x + offsetX, top.y + 0.035, top.z + offsetZ);
dummy.rotation.y = hash(top.z + i, top.x) * Math.PI;
dummy.scale.set(
0.22 + hash(top.x, top.z + i) * 0.3,
0.055 + hash(top.z, top.x + i) * 0.065,
0.22 + hash(top.x + i, top.z) * 0.3
);
dummy.updateMatrix();
mossTransforms.push(dummy.matrix.clone());
const mossColor = new THREE.Color(0x657c4d);
mossColor.multiplyScalar(0.78 + hash(top.x + i, top.z) * 0.36);
mossColors.push(mossColor);
}
}
if (mossTransforms.length) {
const moss = new THREE.InstancedMesh(
unitBox,
mossMaterial,
mossTransforms.length
);
for (let i = 0; i < mossTransforms.length; i++) {
moss.setMatrixAt(i, mossTransforms[i]);
moss.setColorAt(i, mossColors[i]);
}
moss.instanceMatrix.needsUpdate = true;
moss.instanceColor.needsUpdate = true;
moss.castShadow = true;
scene.add(moss);
}
// -------------------------------------------------------------------------
// Elevated visitor walkway
// -------------------------------------------------------------------------
const walkway = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(19.5, 3.05, 13),
new THREE.Vector3(18.0, 3.15, 8),
new THREE.Vector3(16.8, 3.35, 3),
new THREE.Vector3(16.2, 3.55, -2),
new THREE.Vector3(16.0, 3.8, -7),
new THREE.Vector3(15.2, 4.1, -12)
],
false,
"catmullrom",
0.32
);
const plankCount = Math.floor(walkway.getLength() / 0.52);
const plankWidth = 2.45;
const plankThickness = 0.17;
const plankDepth = 0.47;
const planks = new THREE.InstancedMesh(
unitBox,
woodMaterial,
plankCount
);
const plankPalette = [
0x9d6845,
0xaa7650,
0x8e5d40,
0xb27b53,
0x966143
];
const matrix = new THREE.Matrix4();
const basis = new THREE.Matrix4();
const quaternion = new THREE.Quaternion();
const correctedUp = new THREE.Vector3();
const right = new THREE.Vector3();
const tangent = new THREE.Vector3();
for (let i = 0; i < plankCount; i++) {
const t = i / (plankCount - 1);
const point = walkway.getPointAt(t);
walkway.getTangentAt(t, tangent).normalize();
right.crossVectors(up, tangent).normalize();
correctedUp.crossVectors(tangent, right).normalize();
basis.makeBasis(right, correctedUp, tangent);
quaternion.setFromRotationMatrix(basis);
matrix.compose(
point,
quaternion,
new THREE.Vector3(plankWidth, plankThickness, plankDepth)
);
planks.setMatrixAt(i, matrix);
const color = new THREE.Color(
plankPalette[i % plankPalette.length]
);
color.multiplyScalar(0.88 + hash(i, 7) * 0.18);
planks.setColorAt(i, color);
}
planks.instanceMatrix.needsUpdate = true;
planks.instanceColor.needsUpdate = true;
planks.castShadow = true;
planks.receiveShadow = true;
scene.add(planks);
const railPosts = [[], []];
const supportBottom = -3.05;
for (let i = 0; i < plankCount; i++) {
const t = i / (plankCount - 1);
const point = walkway.getPointAt(t);
walkway.getTangentAt(t, tangent).normalize();
right.crossVectors(up, tangent).normalize();
if (i % 5 === 0) {
for (let sideIndex = 0; sideIndex < 2; sideIndex++) {
const side = sideIndex === 0 ? -1 : 1;
const postPosition = point
.clone()
.addScaledVector(right, side * (plankWidth * 0.5 + 0.08));
const post = new THREE.Mesh(unitBox, darkWoodMaterial);
post.position.copy(postPosition);
post.position.y += 0.91;
post.scale.set(0.13, 1.82, 0.13);
post.castShadow = true;
scene.add(post);
railPosts[sideIndex].push(postPosition.clone());
}
}
if (i % 10 === 2) {
for (const side of [-1, 1]) {
const supportPosition = point
.clone()
.addScaledVector(right, side * (plankWidth * 0.5 - 0.16));
const height = supportPosition.y - plankThickness * 0.5 - supportBottom;
const support = new THREE.Mesh(unitBox, darkWoodMaterial);
support.position.set(
supportPosition.x,
supportBottom + height * 0.5,
supportPosition.z
);
support.scale.set(0.27, height, 0.27);
support.castShadow = true;
support.receiveShadow = true;
scene.add(support);
}
}
}
function addRailSegment(a, b, height, thickness) {
const direction = b.clone().sub(a);
const length = direction.length();
const midpoint = a.clone().add(b).multiplyScalar(0.5);
midpoint.y += height;
const rail = new THREE.Mesh(unitBox, darkWoodMaterial);
rail.position.copy(midpoint);
rail.scale.set(thickness, thickness, length);
rail.quaternion.setFromUnitVectors(
forwardAxis,
direction.normalize()
);
rail.castShadow = true;
scene.add(rail);
}
for (const posts of railPosts) {
for (let i = 0; i < posts.length - 1; i++) {
addRailSegment(posts[i], posts[i + 1], 1.62, 0.12);
addRailSegment(posts[i], posts[i + 1], 0.82, 0.085);
}
}
// -------------------------------------------------------------------------
// Small voxel visitors
// -------------------------------------------------------------------------
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xd9a782,
roughness: 0.9
});
const darkClothesMaterial = new THREE.MeshStandardMaterial({
color: 0x263b46,
roughness: 0.88
});
const jacketMaterials = [
new THREE.MeshStandardMaterial({ color: 0xe66b45, roughness: 0.85 }),
new THREE.MeshStandardMaterial({ color: 0x4aa6a6, roughness: 0.85 }),
new THREE.MeshStandardMaterial({ color: 0xe6b74c, roughness: 0.85 })
];
function createVisitor(t, jacketMaterial, scale = 1) {
const group = new THREE.Group();
const point = walkway.getPointAt(t);
walkway.getTangentAt(t, tangent).normalize();
group.position.copy(point);
group.rotation.y = Math.atan2(tangent.x, tangent.z);
group.scale.setScalar(scale);
const leftLeg = new THREE.Mesh(unitBox, darkClothesMaterial);
leftLeg.position.set(-0.12, 0.29, 0);
leftLeg.scale.set(0.15, 0.57, 0.19);
leftLeg.castShadow = true;
group.add(leftLeg);
const rightLeg = leftLeg.clone();
rightLeg.position.x = 0.12;
group.add(rightLeg);
const body = new THREE.Mesh(unitBox, jacketMaterial);
body.position.y = 0.88;
body.scale.set(0.47, 0.68, 0.31);
body.castShadow = true;
group.add(body);
const head = new THREE.Mesh(unitBox, skinMaterial);
head.position.y = 1.39;
head.scale.set(0.3, 0.3, 0.3);
head.castShadow = true;
group.add(head);
const hair = new THREE.Mesh(unitBox, darkClothesMaterial);
hair.position.set(0, 1.53, -0.015);
hair.scale.set(0.32, 0.09, 0.31);
group.add(hair);
const backpack = new THREE.Mesh(unitBox, jacketMaterial);
backpack.position.set(0, 0.92, -0.22);
backpack.scale.set(0.34, 0.43, 0.17);
backpack.castShadow = true;
group.add(backpack);
scene.add(group);
}
createVisitor(0.2, jacketMaterials[0]);
createVisitor(0.42, jacketMaterials[1], 0.92);
createVisitor(0.68, jacketMaterials[2], 0.75);
// -------------------------------------------------------------------------
// Stylized foam fragments along the diagonal shoreline
// -------------------------------------------------------------------------
const foamCount = 130;
const foamMaterial = new THREE.MeshBasicMaterial({
color: 0xe9ffff,
transparent: true,
opacity: 0.38,
depthWrite: false
});
const foam = new THREE.InstancedMesh(
unitBox,
foamMaterial,
foamCount
);
let foamIndex = 0;
for (let i = 0; i < foamCount * 3 && foamIndex < foamCount; i++) {
const x = -5 + hash(i, 1) * 30;
const z = 20 - x + (hash(i, 2) - 0.5) * 3.2;
const shorelineHeight = 5.4 - x * 0.27 - z * 0.21;
if (shorelineHeight < -0.35 || shorelineHeight > 1.05) {
continue;
}
const dummy = new THREE.Object3D();
dummy.position.set(x, 0.09, z);
dummy.rotation.y = Math.PI * 0.25 + (hash(i, 3) - 0.5) * 0.7;
dummy.scale.set(
0.25 + hash(i, 4) * 0.85,
0.025,
0.045 + hash(i, 5) * 0.1
);
dummy.updateMatrix();
foam.setMatrixAt(foamIndex++, dummy.matrix);
}
foam.count = foamIndex;
foam.instanceMatrix.needsUpdate = true;
foam.renderOrder = 3;
scene.add(foam);
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(-1.5, 1.1, 0),
yaw: 0.76,
pitch: 0.98,
radius: 37,
targetYaw: 0.76,
targetPitch: 0.98,
targetRadius: 37
};
function updateCamera(dt) {
const smoothing = 1 - Math.pow(0.0008, dt);
cameraState.yaw +=
(cameraState.targetYaw - cameraState.yaw) * smoothing;
cameraState.pitch +=
(cameraState.targetPitch - cameraState.pitch) * smoothing;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * smoothing;
const sinPitch = Math.sin(cameraState.pitch);
camera.position.set(
cameraState.target.x +
cameraState.radius *
sinPitch *
Math.sin(cameraState.yaw),
cameraState.target.y +
cameraState.radius *
Math.cos(cameraState.pitch),
cameraState.target.z +
cameraState.radius *
sinPitch *
Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
const pointers = new Map();
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
renderer.domElement.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 (event.shiftKey) {
const right = new THREE.Vector3();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
right.crossVectors(forward, up).normalize();
const panScale = cameraState.radius * 0.00125;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, dy * panScale);
} else {
cameraState.targetYaw -= dx * 0.005;
cameraState.targetPitch = THREE.MathUtils.clamp(
cameraState.targetPitch + dy * 0.004,
0.36,
1.42
);
}
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0) {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius *
(previousPinchDistance / Math.max(distance, 1)),
14,
68
);
}
previousPinchDistance = distance;
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
14,
68
);
},
{ passive: false }
);
document.getElementById("resetCamera").addEventListener("click", () => {
cameraState.target.set(-1.5, 1.1, 0);
cameraState.targetYaw = 0.76;
cameraState.targetPitch = 0.98;
cameraState.targetRadius = 37;
});
// -------------------------------------------------------------------------
// 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);
});
let frame = 0;
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera(dt);
// Gentle geometric ocean waves.
for (let i = 0; i < oceanPositions.array.length; i += 3) {
const x = oceanBase[i];
const z = oceanBase[i + 2];
oceanPositions.array[i + 1] =
Math.sin(x * 0.27 + elapsed * 0.85) * 0.085 +
Math.cos(z * 0.23 + elapsed * 0.62) * 0.065 +
Math.sin((x + z) * 0.12 + elapsed * 0.38) * 0.035;
}
oceanPositions.needsUpdate = true;
if (frame % 3 === 0) {
oceanGeometry.computeVertexNormals();
}
// Tide-pool surface motion and expanding ripple rings.
for (const pool of pools) {
pool.water.position.y =
pool.level + Math.sin(elapsed * 0.85 + pool.phase * 6) * 0.018;
pool.rings.forEach((ring, index) => {
const progress =
(elapsed * 0.2 + index / pool.rings.length + pool.phase) % 1;
const scale = 0.55 + progress * pool.r * 1.85;
ring.scale.set(scale, scale, 1);
ring.material.opacity = (1 - progress) * 0.34;
});
}
foamMaterial.opacity = 0.31 + Math.sin(elapsed * 0.7) * 0.045;
renderer.render(scene, camera);
frame++;
});
</script>
</body>
</html>
```