Prompt specification
Build or customize the Three.js 3D scene "Hatchling route to the shore" 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: Hatchling route to the shore
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/hatchling-route-to-the-shore-x0710
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing sea-turtle nesting. Animate hatchlings emerging and following a simple downhill route toward water; a caption states that real orientation depends on multiple environmental cues beyond this geometric model. Voxel style.
Working Three.js Starter (hatchling-route-to-the-shore-x0710):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Sea-Turtle Nesting</title>
<style>
:root {
color-scheme: light;
--ink: #173f46;
--paper: rgba(255, 250, 229, 0.91);
--accent: #e76f38;
--sea: #247ba0;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bfe5ec;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#app,
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
canvas {
display: block;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
color: var(--ink);
background: var(--paper);
border: 1px solid rgba(23, 63, 70, 0.14);
box-shadow: 0 10px 30px rgba(23, 63, 70, 0.14);
backdrop-filter: blur(10px);
}
.title-card {
top: 22px;
left: 22px;
max-width: min(390px, calc(100vw - 44px));
padding: 17px 19px 16px;
border-radius: 14px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--accent);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.45rem, 3vw, 2.25rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 9px 0 0;
font-size: 0.84rem;
line-height: 1.4;
opacity: 0.78;
}
.controls {
top: 22px;
right: 22px;
display: flex;
gap: 7px;
padding: 7px;
border-radius: 12px;
}
button {
min-width: 82px;
padding: 9px 13px;
border: 0;
border-radius: 8px;
color: #fff;
background: var(--ink);
font: inherit;
font-size: 0.78rem;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: #24616a;
transform: translateY(-1px);
}
button:focus-visible {
outline: 3px solid rgba(231, 111, 56, 0.45);
outline-offset: 2px;
}
.caption {
left: 50%;
bottom: 24px;
width: min(760px, calc(100vw - 32px));
padding: 15px 19px;
border-radius: 13px;
transform: translateX(-50%);
font-size: clamp(0.82rem, 1.4vw, 0.98rem);
line-height: 1.45;
text-align: center;
}
.caption strong {
color: var(--accent);
}
.hint {
position: fixed;
right: 18px;
bottom: 20px;
z-index: 8;
padding: 7px 10px;
border-radius: 8px;
color: rgba(23, 63, 70, 0.72);
background: rgba(255, 255, 255, 0.56);
font-size: 0.69rem;
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 720px) {
.title-card {
top: 12px;
left: 12px;
padding: 13px 15px;
}
.controls {
top: auto;
right: 12px;
bottom: 105px;
}
.caption {
bottom: 12px;
padding: 12px 14px;
}
.hint {
display: none;
}
.subtitle {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="panel title-card">
<p class="eyebrow">Voxel field study</p>
<h1>Nest to sea</h1>
<p class="subtitle">
Hatchlings leave a sandy nest, descend a simplified route, and enter the surf.
</p>
</header>
<nav class="panel controls" aria-label="Animation controls">
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Restart</button>
</nav>
<section class="panel caption" aria-live="polite">
<strong>Reading the slope:</strong>
This animation uses a simplified geometric downhill route. In reality,
hatchling sea-turtle orientation depends on multiple environmental cues
beyond this geometric model.
</section>
<div class="hint">Drag to orbit · Scroll 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";
// -------------------------------------------------------------------------
// Renderer, scene, and lighting
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbfe5ec);
scene.fog = new THREE.Fog(0xbfe5ec, 29, 60);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xdff7ff, 0x9a754a, 2.1);
scene.add(hemisphere);
const sunLight = new THREE.DirectionalLight(0xfff1cf, 3.2);
sunLight.position.set(-10, 18, 8);
sunLight.castShadow = true;
sunLight.shadow.mapSize.set(2048, 2048);
sunLight.shadow.camera.left = -20;
sunLight.shadow.camera.right = 20;
sunLight.shadow.camera.top = 20;
sunLight.shadow.camera.bottom = -20;
sunLight.shadow.camera.near = 1;
sunLight.shadow.camera.far = 55;
sunLight.shadow.normalBias = 0.025;
scene.add(sunLight);
const rimLight = new THREE.DirectionalLight(0x84d7ea, 1.25);
rimLight.position.set(10, 8, -12);
scene.add(rimLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const identityQuaternion = new THREE.Quaternion();
const tempMatrix = new THREE.Matrix4();
const tempObject = new THREE.Object3D();
const tempColor = new THREE.Color();
const WATER_TOP = -0.04;
const TILE_SIZE = 1;
function sandHeight(z) {
if (z > 6) return WATER_TOP;
return Math.max(0.12, (6 - z) * 0.095);
}
function seededRandom(seed) {
const x = Math.sin(seed * 127.1 + 311.7) * 43758.5453123;
return x - Math.floor(x);
}
function makeMaterial(color, roughness = 0.85, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
sand: makeMaterial(0xffffff, 1),
darkSand: makeMaterial(0x6f5034, 1),
rock: makeMaterial(0x7d8179, 1),
rockLight: makeMaterial(0xa9a396, 1),
shell: makeMaterial(0xfff2cf, 0.86),
grass: makeMaterial(0x617c43, 1),
grassLight: makeMaterial(0x82955a, 1),
post: makeMaterial(0x795331, 1),
sign: makeMaterial(0xf4eee0, 0.8),
signBlue: makeMaterial(0x267da0, 0.75),
eye: makeMaterial(0x101718, 0.35),
body: makeMaterial(0xb7a975, 0.95),
head: makeMaterial(0xc8b985, 0.92),
egg: makeMaterial(0xf5e9c9, 0.8),
foam: new THREE.MeshStandardMaterial({
color: 0xf6ffff,
roughness: 0.65,
transparent: true,
opacity: 0.8
}),
sun: new THREE.MeshStandardMaterial({
color: 0xffd36b,
emissive: 0xffb23e,
emissiveIntensity: 1.1,
roughness: 0.55
}),
cloud: makeMaterial(0xf6fbf7, 1)
};
const shellMaterials = [
makeMaterial(0x465943, 0.92),
makeMaterial(0x566646, 0.92),
makeMaterial(0x3f5948, 0.92),
makeMaterial(0x69704c, 0.92)
];
function addBox(
parent,
sx,
sy,
sz,
x,
y,
z,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel beach
// -------------------------------------------------------------------------
const sandTiles = [];
const nestCell = { x: -2, z: -7 };
for (let z = -11; z <= 6; z++) {
for (let x = -11; x <= 11; x++) {
if (x === nestCell.x && z === nestCell.z) continue;
sandTiles.push({ x, z, height: sandHeight(z) });
}
}
const sandMesh = new THREE.InstancedMesh(
unitBox,
materials.sand,
sandTiles.length
);
sandMesh.receiveShadow = true;
sandMesh.castShadow = true;
const sandBase = new THREE.Color(0xd8b878);
sandTiles.forEach((tile, index) => {
tempObject.position.set(tile.x, tile.height * 0.5, tile.z);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.set(1, tile.height, 1);
tempObject.updateMatrix();
sandMesh.setMatrixAt(index, tempObject.matrix);
const variation = (seededRandom(index + 4) - 0.5) * 0.07;
tempColor.copy(sandBase).offsetHSL(variation * 0.4, variation, variation);
sandMesh.setColorAt(index, tempColor);
});
sandMesh.instanceMatrix.needsUpdate = true;
if (sandMesh.instanceColor) sandMesh.instanceColor.needsUpdate = true;
scene.add(sandMesh);
// Small voxel rocks and beach debris, kept away from the marked route.
for (let i = 0; i < 25; i++) {
const side = i % 2 === 0 ? -1 : 1;
const x = side * (7.3 + seededRandom(i + 70) * 3.2);
const z = -10.5 + seededRandom(i + 120) * 15.5;
const y = sandHeight(z);
const size = 0.18 + seededRandom(i + 170) * 0.38;
addBox(
scene,
size,
size * 0.7,
size * 0.8,
x,
y + size * 0.32,
z,
i % 3 === 0 ? materials.rockLight : materials.rock
);
}
// Sparse blocky beach grass near the upper edges.
for (let i = 0; i < 30; i++) {
const x = (i % 2 === 0 ? -1 : 1) *
(8.8 + seededRandom(i + 300) * 1.8);
const z = -10 + seededRandom(i + 330) * 9;
const y = sandHeight(z);
const h = 0.25 + seededRandom(i + 360) * 0.35;
addBox(
scene,
0.08,
h,
0.08,
x,
y + h * 0.5,
z,
i % 2 ? materials.grass : materials.grassLight
);
}
// -------------------------------------------------------------------------
// Nest cavity, eggs, and voxel rim
// -------------------------------------------------------------------------
const nestHeight = sandHeight(nestCell.z);
const nestFloorTop = nestHeight - 0.54;
addBox(
scene,
0.94,
0.12,
0.94,
nestCell.x,
nestFloorTop - 0.06,
nestCell.z,
materials.darkSand
);
const rimOffsets = [
[-0.66, 0],
[0.66, 0],
[0, -0.66],
[0, 0.66]
];
for (const [dx, dz] of rimOffsets) {
addBox(
scene,
0.42,
0.18,
0.42,
nestCell.x + dx,
nestHeight + 0.07,
nestCell.z + dz,
materials.sand
);
}
const eggOffsets = [
[-0.16, -0.13],
[0.12, -0.17],
[-0.12, 0.14],
[0.18, 0.15]
];
for (const [dx, dz] of eggOffsets) {
addBox(
scene,
0.18,
0.22,
0.18,
nestCell.x + dx,
nestFloorTop + 0.11,
nestCell.z + dz,
materials.egg
);
}
// -------------------------------------------------------------------------
// Geometric downhill route
// -------------------------------------------------------------------------
const routePoints = [
new THREE.Vector3(-2, sandHeight(-7), -7),
new THREE.Vector3(-1.25, sandHeight(-5.3), -5.3),
new THREE.Vector3(0.05, sandHeight(-3.1), -3.1),
new THREE.Vector3(0.85, sandHeight(-0.9), -0.9),
new THREE.Vector3(1.75, sandHeight(1.6), 1.6),
new THREE.Vector3(3.05, sandHeight(3.75), 3.75),
new THREE.Vector3(3.45, sandHeight(5.25), 5.25),
new THREE.Vector3(4.1, WATER_TOP + 0.02, 8.4)
];
const segmentLengths = [];
let routeLength = 0;
for (let i = 0; i < routePoints.length - 1; i++) {
const length = routePoints[i].distanceTo(routePoints[i + 1]);
segmentLengths.push(length);
routeLength += length;
}
function sampleRoute(distance) {
const d = THREE.MathUtils.clamp(distance, 0, routeLength);
let remaining = d;
for (let i = 0; i < segmentLengths.length; i++) {
if (remaining <= segmentLengths[i] || i === segmentLengths.length - 1) {
const t = segmentLengths[i] === 0
? 0
: remaining / segmentLengths[i];
const position = new THREE.Vector3().lerpVectors(
routePoints[i],
routePoints[i + 1],
t
);
const direction = new THREE.Vector3().subVectors(
routePoints[i + 1],
routePoints[i]
).normalize();
return { position, direction };
}
remaining -= segmentLengths[i];
}
}
// Subtle dotted stones make the model's geometric route legible.
const routeMarkers = [];
for (let d = 1.1; d < routeLength - 0.35; d += 0.72) {
const sample = sampleRoute(d);
if (sample.position.z < 6.05) {
routeMarkers.push(sample.position);
}
}
const markerMesh = new THREE.InstancedMesh(
unitBox,
materials.shell,
routeMarkers.length
);
routeMarkers.forEach((position, index) => {
tempObject.position.set(
position.x,
position.y + 0.025,
position.z
);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.set(0.15, 0.04, 0.15);
tempObject.updateMatrix();
markerMesh.setMatrixAt(index, tempObject.matrix);
});
markerMesh.instanceMatrix.needsUpdate = true;
markerMesh.receiveShadow = true;
scene.add(markerMesh);
// A small shoreline sign marks the end of the illustrated route.
const signX = 5.15;
const signZ = 4.75;
const signGround = sandHeight(signZ);
addBox(
scene,
0.11,
1.25,
0.11,
signX,
signGround + 0.625,
signZ,
materials.post
);
addBox(
scene,
0.7,
0.4,
0.1,
signX + 0.26,
signGround + 1.12,
signZ,
materials.sign
);
addBox(
scene,
0.38,
0.13,
0.115,
signX + 0.23,
signGround + 1.13,
signZ,
materials.signBlue
);
// -------------------------------------------------------------------------
// Voxel sea
// -------------------------------------------------------------------------
const waterTiles = [];
for (let z = 7; z <= 19; z++) {
for (let x = -13; x <= 13; x++) {
waterTiles.push({
x,
z,
phase: seededRandom(x * 7 + z * 13) * Math.PI * 2
});
}
}
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.34,
metalness: 0.08,
transparent: true,
opacity: 0.91
});
const waterMesh = new THREE.InstancedMesh(
unitBox,
waterMaterial,
waterTiles.length
);
waterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterMesh.receiveShadow = true;
const waterBase = new THREE.Color(0x2589aa);
waterTiles.forEach((tile, index) => {
tempColor.copy(waterBase).offsetHSL(
(seededRandom(index + 700) - 0.5) * 0.025,
0,
(seededRandom(index + 800) - 0.5) * 0.09
);
waterMesh.setColorAt(index, tempColor);
});
if (waterMesh.instanceColor) waterMesh.instanceColor.needsUpdate = true;
scene.add(waterMesh);
const foamPieces = [];
for (let i = 0; i < 34; i++) {
const x = -12.5 + i * 0.76 + (seededRandom(i + 900) - 0.5) * 0.2;
const z = 6.48 + seededRandom(i + 950) * 0.22;
const width = 0.38 + seededRandom(i + 1000) * 0.55;
const foam = addBox(
scene,
width,
0.035,
0.12,
x,
WATER_TOP + 0.055,
z,
materials.foam,
false,
false
);
foamPieces.push({
mesh: foam,
baseX: x,
width,
phase: seededRandom(i + 1100) * Math.PI * 2
});
}
// A few pale wave streaks farther offshore.
for (let i = 0; i < 26; i++) {
const x = -11 + seededRandom(i + 1200) * 22;
const z = 8.5 + seededRandom(i + 1300) * 9.5;
const width = 0.5 + seededRandom(i + 1400) * 1.4;
addBox(
scene,
width,
0.025,
0.07,
x,
WATER_TOP + 0.045,
z,
materials.foam,
false,
false
);
}
// -------------------------------------------------------------------------
// Turtle construction
// -------------------------------------------------------------------------
function createTurtle(variant = 0, baseScale = 0.48) {
const group = new THREE.Group();
const shellMaterial = shellMaterials[variant % shellMaterials.length];
// Belly/body.
addBox(
group,
0.59,
0.15,
0.9,
0,
0.085,
0,
materials.body
);
// Main shell.
addBox(
group,
0.73,
0.28,
0.88,
0,
0.25,
-0.02,
shellMaterial
);
// Raised voxel scutes.
addBox(
group,
0.36,
0.13,
0.35,
0,
0.43,
-0.18,
shellMaterial
);
addBox(
group,
0.36,
0.13,
0.35,
0,
0.43,
0.16,
shellMaterial
);
addBox(
group,
0.28,
0.11,
0.31,
0,
0.44,
-0.01,
shellMaterial
);
// Neck and head.
addBox(
group,
0.2,
0.18,
0.22,
0,
0.17,
0.54,
materials.head
);
addBox(
group,
0.28,
0.24,
0.3,
0,
0.19,
0.7,
materials.head
);
// Eyes.
addBox(
group,
0.055,
0.055,
0.045,
-0.09,
0.255,
0.845,
materials.eye,
false,
false
);
addBox(
group,
0.055,
0.055,
0.045,
0.09,
0.255,
0.845,
materials.eye,
false,
false
);
const frontFlippers = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.3, 0.14, 0.18);
pivot.rotation.y = side * -0.16;
group.add(pivot);
addBox(
pivot,
0.5,
0.085,
0.24,
side * 0.23,
-0.02,
-0.045,
shellMaterial
);
frontFlippers.push({ pivot, side });
}
const rearFlippers = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.25, 0.1, -0.35);
group.add(pivot);
addBox(
pivot,
0.28,
0.07,
0.22,
side * 0.12,
-0.01,
-0.08,
shellMaterial
);
rearFlippers.push({ pivot, side });
}
group.scale.setScalar(baseScale);
return {
group,
frontFlippers,
rearFlippers,
baseScale
};
}
// Adult turtle beside the nest.
const adult = createTurtle(1, 1.3);
adult.group.position.set(-4, sandHeight(-8) + 0.02, -8);
adult.group.rotation.y = 0.35;
scene.add(adult.group);
// Hatchlings emerge in a staggered sequence.
const hatchlings = [];
const hatchlingCount = 18;
for (let i = 0; i < hatchlingCount; i++) {
const turtle = createTurtle(i, 0.47 + (i % 3) * 0.025);
turtle.group.visible = false;
scene.add(turtle.group);
hatchlings.push({
...turtle,
startDelay: i * 0.38,
speed: 0.92 + (i % 5) * 0.055,
phase: i * 0.91
});
}
// -------------------------------------------------------------------------
// Sky elements
// -------------------------------------------------------------------------
addBox(
scene,
1.7,
1.7,
1.7,
-9.5,
12.2,
-12,
materials.sun,
false,
false
);
const clouds = [];
function createCloud(x, y, z, scale, phase) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
addBox(
group,
2.1,
0.72,
0.8,
0,
0,
0,
materials.cloud,
false,
false
);
addBox(
group,
1.2,
1.0,
0.85,
-0.45,
0.35,
0,
materials.cloud,
false,
false
);
addBox(
group,
1.3,
0.85,
0.8,
0.55,
0.22,
0,
materials.cloud,
false,
false
);
scene.add(group);
clouds.push({ group, baseX: x, phase });
}
createCloud(-7, 10.8, -10, 1.15, 0.2);
createCloud(6, 12.5, -14, 0.9, 1.7);
createCloud(11, 9.5, -6, 1.25, 3.1);
createCloud(-13, 8.8, 0, 0.75, 4.6);
// -------------------------------------------------------------------------
// Direct camera controls — no external control module
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.55, 1.4);
const initialOffset = new THREE.Vector3(16, 14, 20);
const initialRadius = initialOffset.length();
const initialPhi = Math.acos(initialOffset.y / initialRadius);
const initialTheta = Math.atan2(
initialOffset.x,
initialOffset.z
);
let cameraRadius = initialRadius;
let cameraPhi = initialPhi;
let cameraTheta = initialTheta;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
camera.position.set(
cameraTarget.x +
cameraRadius * Math.sin(cameraPhi) * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z +
cameraRadius * Math.sin(cameraPhi) * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.38,
1.28
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
17,
43
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraRadius = initialRadius;
cameraPhi = initialPhi;
cameraTheta = initialTheta;
updateCamera();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const cycleDuration = 32;
const emergenceDuration = 1.8;
let elapsed = 0;
let paused = false;
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
resetButton.addEventListener("click", () => {
elapsed = 0;
});
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function updateWater(time) {
waterTiles.forEach((tile, index) => {
const wave = Math.sin(time * 1.35 + tile.phase) * 0.035;
const height = 0.13;
tempObject.position.set(
tile.x,
WATER_TOP + wave - height * 0.5,
tile.z
);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.set(0.985, height, 0.985);
tempObject.updateMatrix();
waterMesh.setMatrixAt(index, tempObject.matrix);
});
waterMesh.instanceMatrix.needsUpdate = true;
}
function updateHatchlings(time) {
const cycleTime = elapsed % cycleDuration;
for (const turtle of hatchlings) {
const localTime = cycleTime - turtle.startDelay;
if (localTime < 0) {
turtle.group.visible = false;
continue;
}
const travelTime = routeLength / turtle.speed;
const visibleDuration =
emergenceDuration + travelTime + 3.5;
if (localTime > visibleDuration) {
turtle.group.visible = false;
continue;
}
turtle.group.visible = true;
let distance = 0;
let emergence = 1;
if (localTime < emergenceDuration) {
emergence = smoothstep(localTime / emergenceDuration);
} else {
distance = Math.min(
routeLength,
(localTime - emergenceDuration) * turtle.speed
);
}
const routeSample = sampleRoute(distance);
const position = routeSample.position.clone();
const direction = routeSample.direction;
if (localTime < emergenceDuration) {
position.y = THREE.MathUtils.lerp(
nestFloorTop + 0.02,
position.y + 0.02,
emergence
);
position.z += emergence * 0.08;
} else {
const crawlBob = Math.sin(
time * 8 + turtle.phase
) * 0.012;
position.y += 0.02 + crawlBob;
}
turtle.group.position.copy(position);
turtle.group.rotation.y = Math.atan2(
direction.x,
direction.z
);
const emergenceScale = 0.45 + emergence * 0.55;
turtle.group.scale.setScalar(
turtle.baseScale * emergenceScale
);
const flap = Math.sin(time * 8.5 + turtle.phase) * 0.42;
for (const flipper of turtle.frontFlippers) {
flipper.pivot.rotation.z =
-flipper.side * flap;
}
for (const flipper of turtle.rearFlippers) {
flipper.pivot.rotation.z =
-flipper.side * flap * 0.22;
}
}
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
elapsed += dt;
}
const time = elapsed;
updateWater(time);
updateHatchlings(time);
for (const foam of foamPieces) {
const pulse = 0.86 + Math.sin(
time * 1.8 + foam.phase
) * 0.14;
foam.mesh.position.x =
foam.baseX + Math.sin(time * 0.7 + foam.phase) * 0.1;
foam.mesh.scale.x = foam.width * pulse;
}
for (const cloud of clouds) {
cloud.group.position.x =
cloud.baseX + Math.sin(time * 0.06 + cloud.phase) * 1.8;
}
// The adult breathes and gently moves its flippers.
const adultFlap = Math.sin(time * 1.4) * 0.08;
for (const flipper of adult.frontFlippers) {
flipper.pivot.rotation.z =
-flipper.side * adultFlap;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
});
</script>
</body>
</html>
```