Prompt specification
Build or customize the Three.js 3D scene "Shallow coastal nursery meadow" 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: Shallow coastal nursery meadow
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/shallow-coastal-nursery-meadow-x0775
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Build a bounded seagrass meadow with sandy gaps, small fish, and gentle surface light; habitat structure stays visible as animals move between protected and open areas.
Working Three.js Starter (shallow-coastal-nursery-meadow-x0775):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Seagrass Refuge — Voxel Habitat</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: #082f3a;
}
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 42%, rgba(0, 19, 25, 0.28) 100%),
linear-gradient(to bottom, rgba(67, 190, 190, 0.04), transparent 30%);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: #e8fff5;
text-shadow: 0 2px 12px rgba(0, 20, 24, 0.6);
}
.title {
position: absolute;
top: clamp(20px, 4vw, 42px);
left: clamp(20px, 4vw, 48px);
max-width: 360px;
}
.eyebrow {
margin: 0 0 7px;
color: #7de0c2;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 43px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
max-width: 310px;
margin: 12px 0 0;
color: rgba(226, 255, 246, 0.68);
font-size: 13px;
line-height: 1.5;
}
.legend {
position: absolute;
left: clamp(20px, 4vw, 48px);
bottom: clamp(20px, 4vw, 42px);
display: flex;
flex-wrap: wrap;
gap: 17px;
padding: 11px 14px;
border: 1px solid rgba(173, 255, 229, 0.12);
border-radius: 10px;
background: rgba(3, 31, 38, 0.48);
backdrop-filter: blur(8px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: rgba(232, 255, 245, 0.78);
font-size: 11px;
letter-spacing: 0.02em;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.swatch.green {
color: #58c98d;
background: #58c98d;
}
.swatch.sand {
color: #d9bc82;
background: #d9bc82;
}
.swatch.fish {
color: #ffd36c;
background: #ffd36c;
}
.controls {
position: absolute;
right: clamp(20px, 4vw, 48px);
bottom: clamp(22px, 4vw, 44px);
color: rgba(225, 255, 246, 0.5);
font-size: 11px;
letter-spacing: 0.05em;
text-align: right;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #082f3a;
color: #bdf9e8;
font-size: 12px;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 0.65s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 620px) {
.subtitle {
max-width: 250px;
font-size: 12px;
}
.legend {
gap: 10px;
padding: 9px 10px;
}
.legend-item {
font-size: 10px;
}
.controls {
display: none;
}
}
</style>
</head>
<body>
<div id="loading">Growing the meadow…</div>
<div class="vignette"></div>
<div class="hud">
<div class="title">
<p class="eyebrow">Shallow Habitat / 07</p>
<h1>Seagrass<br />Refuge</h1>
<p class="subtitle">
Small fish weave between dense protective thickets and open sandy clearings.
</p>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch green"></span>
Protected thicket
</div>
<div class="legend-item">
<span class="swatch sand"></span>
Open sand
</div>
<div class="legend-item">
<span class="swatch fish"></span>
Foraging fish
</div>
</div>
<div class="controls">Drag to orbit · Scroll to zoom · Double-click to reset</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, scene, and camera
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x082f3a);
scene.fog = new THREE.FogExp2(0x082f3a, 0.019);
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel seagrass meadow with fish moving between protected and open habitat"
);
document.body.prepend(renderer.domElement);
// ---------------------------------------------------------------------
// Deterministic random helpers
// ---------------------------------------------------------------------
let randomSeed = 84371;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function range(min, max) {
return min + (max - min) * random();
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function ellipseValue(x, z, cx, cz, rx, rz) {
const dx = (x - cx) / rx;
const dz = (z - cz) / rz;
return dx * dx + dz * dz;
}
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xbceff1, 0x314b3d, 2.05);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xffedc7, 3.35);
sunlight.position.set(3.5, 10.5, 4.5);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -11;
sunlight.shadow.camera.right = 11;
sunlight.shadow.camera.top = 10;
sunlight.shadow.camera.bottom = -10;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 30;
sunlight.shadow.bias = -0.0004;
sunlight.shadow.normalBias = 0.025;
scene.add(sunlight);
scene.add(sunlight.target);
const coolFill = new THREE.DirectionalLight(0x72d9dc, 0.8);
coolFill.position.set(-7, 5, -6);
scene.add(coolFill);
const movingLightA = new THREE.PointLight(0x8df4dc, 12, 9, 2);
movingLightA.position.set(-3, 3, 1);
scene.add(movingLightA);
const movingLightB = new THREE.PointLight(0x66c8ef, 9, 8, 2);
movingLightB.position.set(4, 2.5, -2);
scene.add(movingLightB);
// ---------------------------------------------------------------------
// Bounded voxel island and sandy floor
// ---------------------------------------------------------------------
const island = new THREE.Group();
scene.add(island);
const lowerBase = new THREE.Mesh(
new THREE.BoxGeometry(15.35, 0.38, 10.85),
new THREE.MeshStandardMaterial({
color: 0x655f45,
roughness: 0.98
})
);
lowerBase.position.y = -0.56;
lowerBase.receiveShadow = true;
lowerBase.castShadow = true;
island.add(lowerBase);
const upperBase = new THREE.Mesh(
new THREE.BoxGeometry(15.1, 0.48, 10.6),
new THREE.MeshStandardMaterial({
color: 0xa88c62,
roughness: 1
})
);
upperBase.position.y = -0.15;
upperBase.receiveShadow = true;
upperBase.castShadow = true;
island.add(upperBase);
const tileColumns = 30;
const tileRows = 21;
const tileGeometry = new THREE.BoxGeometry(0.49, 0.12, 0.49);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
});
const sandTiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileColumns * tileRows
);
const dummy = new THREE.Object3D();
const instanceColor = new THREE.Color();
let tileIndex = 0;
for (let row = 0; row < tileRows; row++) {
for (let column = 0; column < tileColumns; column++) {
const x = -7.25 + column * 0.5;
const z = -5 + row * 0.5;
dummy.position.set(x, 0.035 + range(-0.008, 0.008), z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, range(0.85, 1.08), 1);
dummy.updateMatrix();
sandTiles.setMatrixAt(tileIndex, dummy.matrix);
const lightness = range(0.63, 0.76);
instanceColor.setHSL(
range(0.095, 0.125),
range(0.38, 0.58),
lightness
);
sandTiles.setColorAt(tileIndex, instanceColor);
tileIndex++;
}
}
sandTiles.receiveShadow = true;
sandTiles.instanceMatrix.needsUpdate = true;
if (sandTiles.instanceColor) sandTiles.instanceColor.needsUpdate = true;
island.add(sandTiles);
// ---------------------------------------------------------------------
// Border stones and scattered voxel rubble
// ---------------------------------------------------------------------
const rockGeometry = new THREE.BoxGeometry(1, 1, 1);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
});
const rockData = [];
for (let i = 0; i < 43; i++) {
const x = -7.25 + (i / 42) * 14.5;
rockData.push({
x,
z: -5.05 + range(-0.13, 0.13),
w: range(0.22, 0.43),
h: range(0.13, 0.3),
d: range(0.22, 0.42)
});
rockData.push({
x,
z: 5.05 + range(-0.13, 0.13),
w: range(0.22, 0.43),
h: range(0.13, 0.3),
d: range(0.22, 0.42)
});
}
for (let i = 0; i < 30; i++) {
const z = -4.85 + (i / 29) * 9.7;
rockData.push({
x: -7.18 + range(-0.12, 0.12),
z,
w: range(0.22, 0.42),
h: range(0.13, 0.28),
d: range(0.22, 0.4)
});
rockData.push({
x: 7.18 + range(-0.12, 0.12),
z,
w: range(0.22, 0.42),
h: range(0.13, 0.28),
d: range(0.22, 0.4)
});
}
for (let i = 0; i < 65; i++) {
const area = random();
let x;
let z;
if (area < 0.42) {
x = range(-1.8, 1.4);
z = range(-1.1, 2.0);
} else if (area < 0.75) {
x = range(2.5, 5.3);
z = range(-3.0, -0.2);
} else {
x = range(-0.8, 1.1);
z = range(-4.2, -2.6);
}
const size = range(0.06, 0.17);
rockData.push({
x,
z,
w: size * range(0.8, 1.8),
h: size * range(0.45, 1.1),
d: size * range(0.8, 1.6)
});
}
const rocks = new THREE.InstancedMesh(
rockGeometry,
rockMaterial,
rockData.length
);
rockData.forEach((rock, index) => {
dummy.position.set(rock.x, 0.1 + rock.h * 0.5, rock.z);
dummy.rotation.set(
range(-0.13, 0.13),
range(0, Math.PI),
range(-0.12, 0.12)
);
dummy.scale.set(rock.w, rock.h, rock.d);
dummy.updateMatrix();
rocks.setMatrixAt(index, dummy.matrix);
const isBorder = index < rockData.length - 65;
instanceColor.setHSL(
isBorder ? range(0.11, 0.16) : range(0.12, 0.2),
isBorder ? range(0.13, 0.24) : range(0.24, 0.42),
isBorder ? range(0.4, 0.56) : range(0.5, 0.66)
);
rocks.setColorAt(index, instanceColor);
});
rocks.castShadow = true;
rocks.receiveShadow = true;
rocks.instanceMatrix.needsUpdate = true;
if (rocks.instanceColor) rocks.instanceColor.needsUpdate = true;
island.add(rocks);
// ---------------------------------------------------------------------
// Seagrass: instanced voxel blades arranged into thickets and gaps
// ---------------------------------------------------------------------
const grassGeometry = new THREE.BoxGeometry(1, 1, 1);
grassGeometry.translate(0, 0.5, 0);
const grassMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.83,
side: THREE.DoubleSide
});
grassMaterial.onBeforeCompile = (shader) => {
shader.uniforms.uTime = { value: 0 };
shader.vertexShader = shader.vertexShader
.replace(
"#include <common>",
`#include <common>
uniform float uTime;`
)
.replace(
"#include <begin_vertex>",
`#include <begin_vertex>
#ifdef USE_INSTANCING
float bladePhase = instanceMatrix[3].x * 0.73
+ instanceMatrix[3].z * 0.91;
float bladeHeight = clamp(position.y, 0.0, 1.0);
float swayStrength = bladeHeight * bladeHeight;
transformed.x += sin(uTime * 0.72 + bladePhase)
* 0.055 * swayStrength;
transformed.z += cos(uTime * 0.57 + bladePhase * 1.13)
* 0.042 * swayStrength;
#endif`
);
grassMaterial.userData.shader = shader;
};
grassMaterial.customProgramCacheKey = () => "voxel-seagrass-sway-v1";
const grassBlades = [];
function insideOpenGap(x, z) {
const central = Math.hypot(x + 0.55, z - 0.65) < 1.88;
const eastern = Math.hypot(x - 3.9, z + 1.65) < 1.55;
const southern = Math.hypot(x - 0.15, z + 3.5) < 1.12;
return central || eastern || southern;
}
function protectedStrength(x, z) {
const left = ellipseValue(x, z, -4.8, 2.6, 2.0, 1.75);
const back = ellipseValue(x, z, -1.4, 4.05, 3.15, 1.1);
const right = ellipseValue(x, z, 5.25, 2.75, 1.45, 1.85);
return clamp(
Math.max(
1 - left,
Math.max(1 - back, 1 - right)
),
0,
1
);
}
const step = 0.3;
for (let x = -7.05; x <= 7.05; x += step) {
for (let z = -4.8; z <= 4.8; z += step) {
if (insideOpenGap(x, z)) continue;
const protection = protectedStrength(x, z);
const edgeDistance = Math.min(
7.2 - Math.abs(x),
4.75 - Math.abs(z)
);
const edgeFactor = clamp(1 - edgeDistance / 2.3, 0, 1);
let density = 0.23 + protection * 0.58 + edgeFactor * 0.25;
density *= range(0.68, 1.16);
let blades = Math.floor(density * 5.3);
if (random() < density) blades++;
for (let blade = 0; blade < blades; blade++) {
const px = x + range(-0.115, 0.115);
const pz = z + range(-0.115, 0.115);
const height = range(0.34, 0.78) + protection * range(0.05, 0.48);
const width = range(0.038, 0.075);
grassBlades.push({
x: px,
z: pz,
height,
width,
rotation: range(0, Math.PI * 2),
lean: range(-0.09, 0.09),
color: new THREE.Color().setHSL(
range(0.31, 0.38),
range(0.48, 0.76),
range(0.28, 0.49)
)
});
}
}
}
const grass = new THREE.InstancedMesh(
grassGeometry,
grassMaterial,
grassBlades.length
);
grassBlades.forEach((blade, index) => {
dummy.position.set(blade.x, 0.095, blade.z);
dummy.rotation.set(0, blade.rotation, blade.lean);
dummy.scale.set(
blade.width,
blade.height,
blade.width * range(0.55, 0.85)
);
dummy.updateMatrix();
grass.setMatrixAt(index, dummy.matrix);
grass.setColorAt(index, blade.color);
});
grass.castShadow = true;
grass.receiveShadow = true;
grass.instanceMatrix.needsUpdate = true;
if (grass.instanceColor) grass.instanceColor.needsUpdate = true;
island.add(grass);
// ---------------------------------------------------------------------
// Moving surface-light caustics over the sand
// ---------------------------------------------------------------------
const causticUniforms = {
uTime: { value: 0 }
};
const causticMaterial = new THREE.ShaderMaterial({
uniforms: causticUniforms,
transparent: true,
depthWrite: false,
blending: THREE.AdditiveBlending,
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix
* modelViewMatrix
* vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uTime;
varying vec2 vUv;
void main() {
vec2 p = (vUv - 0.5) * vec2(18.0, 12.5);
float waveA = sin(
p.x * 1.24
+ sin(p.y * 0.74 + uTime * 0.24) * 1.15
+ uTime * 0.37
);
float waveB = sin(
p.y * 1.48
+ sin(p.x * 0.62 - uTime * 0.19) * 1.25
- uTime * 0.29
);
float interference = abs(waveA + waveB);
float line = 1.0 - smoothstep(0.025, 0.16, abs(interference - 0.38));
float softLine = 1.0 - smoothstep(0.03, 0.38, abs(interference - 0.38));
float shimmer = 0.5 + 0.5 * sin(
p.x * 0.31 - p.y * 0.27 + uTime * 0.45
);
vec3 color = mix(
vec3(0.26, 0.72, 0.67),
vec3(0.90, 1.0, 0.79),
line
);
float alpha = line * 0.105 + softLine * 0.025 + shimmer * 0.012;
gl_FragColor = vec4(color, alpha);
}
`
});
const causticPlane = new THREE.Mesh(
new THREE.PlaneGeometry(14.95, 10.45),
causticMaterial
);
causticPlane.rotation.x = -Math.PI / 2;
causticPlane.position.y = 0.104;
causticPlane.renderOrder = 2;
island.add(causticPlane);
// A subtle water surface overhead catches the same slow movement.
const surfaceUniforms = {
uTime: { value: 0 }
};
const surfaceMaterial = new THREE.ShaderMaterial({
uniforms: surfaceUniforms,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix
* modelViewMatrix
* vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float uTime;
varying vec2 vUv;
void main() {
vec2 p = vUv * vec2(12.0, 9.0);
float ripple = sin(
p.x * 0.8
+ sin(p.y * 0.65 + uTime * 0.22) * 1.4
+ uTime * 0.25
);
float glint = pow(max(ripple, 0.0), 12.0);
gl_FragColor = vec4(
vec3(0.45, 0.82, 0.86),
0.012 + glint * 0.035
);
}
`
});
const surface = new THREE.Mesh(
new THREE.PlaneGeometry(34, 26),
surfaceMaterial
);
surface.rotation.x = -Math.PI / 2;
surface.position.y = 6.4;
surface.renderOrder = 8;
scene.add(surface);
// ---------------------------------------------------------------------
// Suspended particles and bubbles
// ---------------------------------------------------------------------
const particleCount = 260;
const particlePositions = new Float32Array(particleCount * 3);
const particleSpeeds = new Float32Array(particleCount);
const particlePhases = new Float32Array(particleCount);
for (let i = 0; i < particleCount; i++) {
particlePositions[i * 3] = range(-8, 8);
particlePositions[i * 3 + 1] = range(-0.1, 6.2);
particlePositions[i * 3 + 2] = range(-5.8, 5.8);
particleSpeeds[i] = range(0.035, 0.13);
particlePhases[i] = range(0, Math.PI * 2);
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0xd8fff5,
size: 0.038,
transparent: true,
opacity: 0.48,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(particles);
// ---------------------------------------------------------------------
// Small voxel fish
// ---------------------------------------------------------------------
const fishBodyGeometry = new THREE.BoxGeometry(0.5, 0.23, 0.21);
const fishHeadGeometry = new THREE.BoxGeometry(0.18, 0.2, 0.2);
const fishFinGeometry = new THREE.BoxGeometry(0.15, 0.16, 0.055);
const fishTailGeometry = new THREE.BoxGeometry(0.23, 0.13, 0.055);
const eyeGeometry = new THREE.BoxGeometry(0.045, 0.045, 0.025);
const fishMaterials = [
new THREE.MeshStandardMaterial({
color: 0xf6c85f,
roughness: 0.55,
metalness: 0.03
}),
new THREE.MeshStandardMaterial({
color: 0x6bd5cb,
roughness: 0.52,
metalness: 0.04
}),
new THREE.MeshStandardMaterial({
color: 0xf08c69,
roughness: 0.58,
metalness: 0.02
}),
new THREE.MeshStandardMaterial({
color: 0xa7d8ef,
roughness: 0.54,
metalness: 0.03
})
];
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0x102b31,
roughness: 0.3
});
const finMaterial = new THREE.MeshStandardMaterial({
color: 0xf5a65b,
roughness: 0.65
});
const routeCoordinates = [
[
[-0.55, 0.65],
[-4.75, 2.55],
[-6.25, 1.15],
[-2.6, 0.45],
[-0.55, 0.65],
[3.9, -1.65],
[6.15, 0.15],
[3.9, -1.65],
[0.15, -3.5],
[-0.55, 0.65]
],
[
[-0.55, 0.65],
[-1.4, 4.05],
[-4.75, 2.55],
[-3.0, 1.2],
[-0.55, 0.65],
[3.9, -1.65],
[5.25, 2.75],
[6.15, 0.15],
[3.9, -1.65],
[0.15, -3.5],
[-0.55, 0.65]
],
[
[3.9, -1.65],
[5.25, 2.75],
[3.8, 3.55],
[1.4, 1.4],
[-0.55, 0.65],
[-4.75, 2.55],
[-6.25, 1.15],
[-0.55, 0.65],
[0.15, -3.5],
[3.9, -1.65]
]
];
const fish = [];
function makeFish(materialIndex, routeIndex, scale, phase, speed) {
const group = new THREE.Group();
group.rotation.order = "YXZ";
group.scale.setScalar(scale);
const material = fishMaterials[materialIndex % fishMaterials.length];
const body = new THREE.Mesh(fishBodyGeometry, material);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const head = new THREE.Mesh(fishHeadGeometry, material);
head.position.x = 0.27;
head.castShadow = true;
group.add(head);
const topFin = new THREE.Mesh(fishFinGeometry, finMaterial);
topFin.position.set(-0.02, 0.17, 0);
topFin.rotation.z = -0.22;
topFin.castShadow = true;
group.add(topFin);
const sideFin = new THREE.Mesh(fishFinGeometry, finMaterial);
sideFin.position.set(-0.01, -0.03, 0.135);
sideFin.scale.set(0.8, 0.75, 0.7);
sideFin.rotation.x = Math.PI / 2;
sideFin.rotation.z = -0.35;
group.add(sideFin);
for (const side of [-1, 1]) {
const eye = new THREE.Mesh(eyeGeometry, eyeMaterial);
eye.position.set(0.345, 0.035, side * 0.108);
group.add(eye);
}
const tailPivot = new THREE.Group();
tailPivot.position.x = -0.25;
group.add(tailPivot);
const upperTail = new THREE.Mesh(fishTailGeometry, material);
upperTail.position.set(-0.1, 0.075, 0);
upperTail.rotation.z = 0.36;
upperTail.castShadow = true;
tailPivot.add(upperTail);
const lowerTail = new THREE.Mesh(fishTailGeometry, material);
lowerTail.position.set(-0.1, -0.075, 0);
lowerTail.rotation.z = -0.36;
lowerTail.castShadow = true;
tailPivot.add(lowerTail);
const route = routeCoordinates[routeIndex % routeCoordinates.length].map(
([x, z]) => new THREE.Vector3(x, 0, z)
);
const curve = new THREE.CatmullRomCurve3(
route,
true,
"catmullrom",
0.32
);
island.add(group);
fish.push({
group,
tailPivot,
curve,
phase,
speed,
lateral: range(-0.16, 0.16),
bobPhase: range(0, Math.PI * 2)
});
}
for (let i = 0; i < 22; i++) {
makeFish(
Math.floor(i / 4) % fishMaterials.length,
i % routeCoordinates.length,
range(0.58, 1.05),
random(),
range(0.011, 0.021)
);
}
// ---------------------------------------------------------------------
// Habitat labels
// ---------------------------------------------------------------------
function createLabel(text, accent, position) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(4, 34, 40, 0.76)";
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = accent;
context.fillRect(0, 0, 8, canvas.height);
context.strokeStyle = "rgba(205, 255, 239, 0.22)";
context.lineWidth = 2;
context.strokeRect(1, 1, canvas.width - 2, canvas.height - 2);
context.fillStyle = "#e9fff6";
context.font = "600 28px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2 + 4, canvas.height / 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
opacity: 0.86
})
);
sprite.position.copy(position);
sprite.scale.set(2.65, 0.5, 1);
sprite.renderOrder = 20;
island.add(sprite);
}
createLabel(
"DENSE REFUGE",
"#61d698",
new THREE.Vector3(-4.75, 1.8, 2.75)
);
createLabel(
"OPEN SAND",
"#ddbd7e",
new THREE.Vector3(0.3, 0.34, -0.35)
);
createLabel(
"EDGE THICKET",
"#61d698",
new THREE.Vector3(5.25, 1.65, 3.0)
);
// ---------------------------------------------------------------------
// Ground shadow catcher
// ---------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x082a32,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.78;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Direct camera controls: orbit and zoom
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.15, 0);
const desiredTarget = cameraTarget.clone();
let yaw = 0.62;
let pitch = 0.68;
let radius = 16.6;
let desiredYaw = yaw;
let desiredPitch = pitch;
let desiredRadius = radius;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function resetCamera() {
desiredYaw = 0.62;
desiredPitch = 0.68;
desiredRadius = 16.6;
desiredTarget.set(0, 0.15, 0);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
desiredYaw -= deltaX * 0.006;
desiredPitch = clamp(desiredPitch + deltaY * 0.0045, 0.34, 1.18);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredRadius = clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
10.5,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const fishPosition = new THREE.Vector3();
const fishTangent = new THREE.Vector3();
const lateralDirection = new THREE.Vector3();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Camera damping.
yaw += (desiredYaw - yaw) * (1 - Math.exp(-delta * 10));
pitch += (desiredPitch - pitch) * (1 - Math.exp(-delta * 10));
radius += (desiredRadius - radius) * (1 - Math.exp(-delta * 9));
cameraTarget.lerp(desiredTarget, 1 - Math.exp(-delta * 8));
camera.position.set(
cameraTarget.x + radius * Math.sin(pitch) * Math.sin(yaw),
cameraTarget.y + radius * Math.cos(pitch),
cameraTarget.z + radius * Math.sin(pitch) * Math.cos(yaw)
);
camera.lookAt(cameraTarget);
// Grass sway.
if (grassMaterial.userData.shader) {
grassMaterial.userData.shader.uniforms.uTime.value = elapsed;
}
// Moving light and surface patterns.
causticUniforms.uTime.value = elapsed;
surfaceUniforms.uTime.value = elapsed;
sunlight.position.x = 3.5 + Math.sin(elapsed * 0.16) * 1.1;
sunlight.position.z = 4.5 + Math.cos(elapsed * 0.13) * 0.8;
movingLightA.position.x = -3.2 + Math.sin(elapsed * 0.27) * 2.1;
movingLightA.position.z = 0.8 + Math.cos(elapsed * 0.23) * 1.6;
movingLightB.position.x = 3.7 + Math.cos(elapsed * 0.21) * 1.7;
movingLightB.position.z = -1.8 + Math.sin(elapsed * 0.3) * 1.8;
// Fish follow curved routes through protected and exposed habitat.
fish.forEach((entry, index) => {
const progress = (
entry.phase +
elapsed * entry.speed
) % 1;
entry.curve.getPointAt(progress, fishPosition);
entry.curve.getTangentAt(progress, fishTangent);
lateralDirection.set(
-fishTangent.z,
0,
fishTangent.x
).normalize();
fishPosition.addScaledVector(lateralDirection, entry.lateral);
fishPosition.y =
0.58 +
Math.sin(elapsed * 1.9 + entry.bobPhase + index) * 0.075;
entry.group.position.copy(fishPosition);
const heading = -Math.atan2(fishTangent.z, fishTangent.x);
entry.group.rotation.y = heading;
entry.group.rotation.z =
Math.sin(elapsed * 2.3 + entry.bobPhase) * 0.035;
entry.group.rotation.x =
Math.sin(elapsed * 1.4 + entry.bobPhase) * 0.018;
entry.tailPivot.rotation.y =
Math.sin(elapsed * 8.2 + entry.bobPhase + index) * 0.38;
});
// Suspended particles drift upward.
const positions = particleGeometry.attributes.position.array;
for (let i = 0; i < particleCount; i++) {
const offset = i * 3;
positions[offset + 1] += particleSpeeds[i] * delta;
if (positions[offset + 1] > 6.25) {
positions[offset + 1] = -0.1;
}
positions[offset] +=
Math.sin(elapsed * 0.35 + particlePhases[i]) * delta * 0.012;
positions[offset + 2] +=
Math.cos(elapsed * 0.29 + particlePhases[i]) * delta * 0.009;
}
particleGeometry.attributes.position.needsUpdate = true;
renderer.render(scene, camera);
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", resize);
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```