Prompt specification
Build or customize the Three.js 3D scene "Cave exit navigation vignette" 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: Cave exit navigation vignette
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cave-exit-navigation-vignette-x0647
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts bat echolocation. Build a small cave mouth with bats following curved exit routes around rock projections; occasional pulse overlays explain obstacle detection without overwhelming the animal silhouettes. Stylized solid forms.
Working Three.js Starter (cave-exit-navigation-vignette-x0647):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Cave Echolocation</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf8ff;
--muted: #9eb4c3;
--cyan: #72e5e7;
--panel: rgba(10, 16, 28, 0.74);
--border: rgba(154, 222, 235, 0.2);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #080b14;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas {
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.title-panel {
position: absolute;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 18px 20px 17px;
border: 1px solid var(--border);
border-radius: 16px;
background:
linear-gradient(135deg, rgba(22, 34, 51, 0.84), rgba(8, 13, 24, 0.7));
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 1px;
content: "";
background: currentColor;
box-shadow: 0 0 9px currentColor;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 38px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 1;
}
.subtitle {
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
position: absolute;
right: 22px;
top: 22px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--border);
border-radius: 10px;
padding: 9px 12px;
background: rgba(12, 20, 32, 0.76);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.035em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
color 160ms ease,
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
color: #fff;
border-color: rgba(114, 229, 231, 0.55);
background: rgba(20, 39, 51, 0.9);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
button.active {
border-color: rgba(114, 229, 231, 0.65);
color: var(--cyan);
background: rgba(24, 58, 65, 0.72);
}
.legend {
position: absolute;
left: 24px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 16px;
padding: 10px 13px;
border: 1px solid rgba(154, 222, 235, 0.14);
border-radius: 11px;
background: rgba(7, 12, 21, 0.62);
color: var(--muted);
font-size: 10px;
letter-spacing: 0.035em;
backdrop-filter: blur(12px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 13px;
height: 8px;
border-radius: 50%;
background: #090c15;
border: 1px solid #738092;
}
.swatch.pulse {
width: 12px;
height: 12px;
border: 1px solid var(--cyan);
border-radius: 50%;
background: transparent;
box-shadow: 0 0 8px rgba(114, 229, 231, 0.5);
}
.swatch.rock {
border-radius: 2px;
background: #45455a;
border-color: #76738a;
}
.hint {
position: absolute;
right: 24px;
bottom: 23px;
color: rgba(199, 221, 231, 0.55);
font-size: 10px;
letter-spacing: 0.04em;
}
#echo-label {
position: absolute;
left: 50%;
top: 50%;
min-width: 165px;
padding: 9px 11px;
transform: translate(-50%, -112%) scale(0.96);
transform-origin: bottom center;
border: 1px solid rgba(114, 229, 231, 0.46);
border-radius: 8px;
background: rgba(8, 24, 31, 0.86);
box-shadow:
0 8px 28px rgba(0, 0, 0, 0.32),
0 0 22px rgba(114, 229, 231, 0.08);
color: #dcffff;
opacity: 0;
transition:
opacity 220ms ease,
transform 220ms ease;
backdrop-filter: blur(10px);
}
#echo-label.visible {
opacity: 1;
transform: translate(-50%, -112%) scale(1);
}
#echo-label::after {
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 20px;
content: "";
background: linear-gradient(var(--cyan), transparent);
}
.echo-title {
color: var(--cyan);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.echo-copy {
margin-top: 3px;
color: #c1d8df;
font-size: 10px;
line-height: 1.4;
}
@media (max-width: 680px) {
.title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
.subtitle {
max-width: 78%;
font-size: 11px;
}
.controls {
top: auto;
right: 14px;
bottom: 54px;
flex-direction: column;
}
button {
padding: 8px 10px;
}
.legend {
left: 14px;
bottom: 14px;
gap: 10px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="hud">
<section class="title-panel" aria-label="Scene description">
<div class="eyebrow">Nocturnal field study</div>
<h1>Cave Echolocation</h1>
<p class="subtitle">
Bats leave the cavern in sweeping arcs, using brief acoustic pulses
to map rock projections before adjusting their flight paths.
</p>
</section>
<nav class="controls" aria-label="Scene controls">
<button id="pause" type="button" aria-pressed="false">Pause</button>
<button id="paths" type="button" aria-pressed="false">Show paths</button>
<button id="camera-reset" type="button">Reset view</button>
<button id="restart" type="button">Restart</button>
</nav>
<div class="legend" aria-label="Legend">
<span class="legend-item">
<span class="swatch"></span>
bat silhouette
</span>
<span class="legend-item">
<span class="swatch pulse"></span>
echo pulse
</span>
<span class="legend-item">
<span class="swatch rock"></span>
rock projection
</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<div id="echo-label" role="status" aria-live="polite">
<div class="echo-title">Echo return</div>
<div class="echo-copy">Rock ahead — the route bends around it.</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const pauseButton = document.querySelector("#pause");
const pathsButton = document.querySelector("#paths");
const cameraResetButton = document.querySelector("#camera-reset");
const restartButton = document.querySelector("#restart");
const echoLabel = document.querySelector("#echo-label");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080b14);
scene.fog = new THREE.Fog(0x080b14, 12, 32);
const camera = new THREE.PerspectiveCamera(
46,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Animated cave scene with bats navigating around rocks using echolocation"
);
// Lighting: cool moonlight, a violet fill, and a subtle cave glow.
scene.add(new THREE.HemisphereLight(0x8fb9da, 0x15131f, 1.45));
const moon = new THREE.DirectionalLight(0xb8d9ff, 3.2);
moon.position.set(6, 10, 8);
moon.castShadow = true;
moon.shadow.mapSize.set(2048, 2048);
moon.shadow.camera.left = -13;
moon.shadow.camera.right = 13;
moon.shadow.camera.top = 12;
moon.shadow.camera.bottom = -9;
moon.shadow.camera.near = 1;
moon.shadow.camera.far = 35;
moon.shadow.bias = -0.0008;
scene.add(moon);
const violetFill = new THREE.PointLight(0x7959d9, 22, 20, 2);
violetFill.position.set(-6, 4, 4);
scene.add(violetFill);
const caveGlow = new THREE.PointLight(0x36bfc4, 14, 11, 2);
caveGlow.position.set(-0.8, 2.2, -5.5);
scene.add(caveGlow);
const rimLight = new THREE.DirectionalLight(0x71e6e2, 1.35);
rimLight.position.set(-5, 4, -3);
scene.add(rimLight);
// Deterministic random values keep the faceted composition stable.
let randomSeed = 47291;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function between(min, max) {
return min + (max - min) * random();
}
// Slightly irregular ground.
const groundGeometry = new THREE.PlaneGeometry(38, 38, 22, 22);
const groundPositions = groundGeometry.attributes.position;
for (let i = 0; i < groundPositions.count; i++) {
groundPositions.setZ(i, between(-0.13, 0.13));
}
groundGeometry.rotateX(-Math.PI / 2);
groundGeometry.computeVertexNormals();
const ground = new THREE.Mesh(
groundGeometry,
new THREE.MeshStandardMaterial({
color: 0x171b29,
roughness: 1,
metalness: 0,
flatShading: true
})
);
ground.position.y = -0.09;
ground.receiveShadow = true;
scene.add(ground);
// Low, flat stones provide scale without distracting from the bats.
const groundStoneMaterials = [
new THREE.MeshStandardMaterial({
color: 0x25293a,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x2c3041,
roughness: 0.95,
flatShading: true
})
];
for (let i = 0; i < 34; i++) {
const angle = between(0, Math.PI * 2);
const distance = between(3.4, 12);
const stone = new THREE.Mesh(
new THREE.DodecahedronGeometry(1, 0),
groundStoneMaterials[i % 2]
);
stone.position.set(
Math.cos(angle) * distance,
between(-0.02, 0.04),
Math.sin(angle) * distance - 1.5
);
stone.scale.set(
between(0.08, 0.32),
between(0.025, 0.09),
between(0.1, 0.36)
);
stone.rotation.set(between(0, 3), between(0, 3), between(0, 3));
stone.receiveShadow = true;
scene.add(stone);
}
// The dark opening of the cave.
const openingShape = new THREE.Shape();
openingShape.moveTo(-2.45, 0);
openingShape.lineTo(-2.45, 2.35);
openingShape.bezierCurveTo(-2.42, 3.28, -1.55, 4.02, 0, 4.12);
openingShape.bezierCurveTo(1.55, 4.02, 2.42, 3.28, 2.45, 2.35);
openingShape.lineTo(2.45, 0);
openingShape.closePath();
const opening = new THREE.Mesh(
new THREE.ShapeGeometry(openingShape, 12),
new THREE.MeshBasicMaterial({ color: 0x02040a })
);
opening.position.set(0, 0.02, -6.46);
scene.add(opening);
const caveRockMaterials = [
new THREE.MeshStandardMaterial({
color: 0x303145,
roughness: 0.97,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x3a394d,
roughness: 0.96,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x292b3d,
roughness: 1,
flatShading: true
})
];
function addRock(
x,
y,
z,
sx,
sy,
sz,
material = caveRockMaterials[0],
detail = 1
) {
const rock = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, detail),
material
);
rock.position.set(x, y, z);
rock.scale.set(sx, sy, sz);
rock.rotation.set(
between(-0.24, 0.24),
between(0, Math.PI * 2),
between(-0.18, 0.18)
);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
return rock;
}
// Faceted boulders form the mouth of the cave.
const rimRocks = [
[-3.25, 1.3, -6.0, 1.65, 2.25, 1.55],
[-2.72, 3.05, -6.03, 0.92, 1.25, 0.98],
[-1.65, 4.28, -6.02, 1.35, 0.92, 1.0],
[0.0, 4.72, -6.12, 1.55, 0.88, 1.08],
[1.62, 4.28, -6.02, 1.35, 0.92, 1.0],
[2.72, 3.03, -6.03, 0.95, 1.3, 0.98],
[3.28, 1.25, -5.98, 1.7, 2.3, 1.55],
[-4.3, 2.45, -6.22, 1.8, 2.8, 1.3],
[4.35, 2.5, -6.2, 1.85, 2.9, 1.35]
];
rimRocks.forEach((r, index) => {
addRock(
...r,
caveRockMaterials[index % caveRockMaterials.length],
1
);
});
// Hanging chips make the cave lip read as a natural formation.
for (let i = 0; i < 7; i++) {
const chip = new THREE.Mesh(
new THREE.ConeGeometry(between(0.18, 0.36), between(0.55, 1.2), 5),
caveRockMaterials[(i + 1) % caveRockMaterials.length]
);
chip.position.set(
-2.25 + i * 0.72 + between(-0.12, 0.12),
between(4.12, 4.5),
-5.84
);
chip.rotation.z = between(-0.12, 0.12);
chip.castShadow = true;
scene.add(chip);
}
// Three prominent projections determine the bats' curved exit routes.
const projectionData = [
{ x: -1.9, y: 1.05, z: -2.45, sx: 0.94, sy: 2.05, sz: 0.8 },
{ x: 0.75, y: 1.18, z: -1.45, sx: 1.0, sy: 2.35, sz: 0.9 },
{ x: 2.75, y: 0.98, z: -0.65, sx: 0.92, sy: 2.0, sz: 0.82 }
];
const projections = projectionData.map((data, index) => {
const group = new THREE.Group();
group.position.set(data.x, 0, data.z);
const material = new THREE.MeshStandardMaterial({
color: index === 1 ? 0x454557 : 0x3b3c50,
roughness: 0.92,
metalness: 0.02,
flatShading: true,
emissive: 0x164a52,
emissiveIntensity: 0
});
const main = new THREE.Mesh(
new THREE.DodecahedronGeometry(1, 1),
material
);
main.position.y = data.y;
main.scale.set(data.sx, data.sy, data.sz);
main.rotation.y = index * 0.85;
main.castShadow = true;
main.receiveShadow = true;
group.add(main);
const shoulder = new THREE.Mesh(
new THREE.DodecahedronGeometry(1, 0),
caveRockMaterials[(index + 1) % caveRockMaterials.length]
);
shoulder.position.set(
data.sx * 0.72,
data.y * 0.45,
data.sz * 0.15
);
shoulder.scale.set(data.sx * 0.62, data.sy * 0.54, data.sz * 0.65);
shoulder.castShadow = true;
group.add(shoulder);
scene.add(group);
return { group, main, material, pulseAt: -100 };
});
// A faceted bat body and membrane wings.
const batBodyMaterial = new THREE.MeshStandardMaterial({
color: 0x080a12,
roughness: 0.72,
metalness: 0.05,
emissive: 0x11182b,
emissiveIntensity: 0.35,
flatShading: true
});
const batWingMaterial = new THREE.MeshStandardMaterial({
color: 0x111421,
roughness: 0.8,
metalness: 0.02,
emissive: 0x10192a,
emissiveIntensity: 0.24,
side: THREE.DoubleSide,
flatShading: true
});
const eyeMaterial = new THREE.MeshBasicMaterial({
color: 0xa4f7f0
});
function createWingGeometry(side) {
const vertices = new Float32Array([
0, 0, 0,
side * 0.44, 0.07, 0.06,
side * 1.13, 0.24, -0.09,
side * 1.56, -0.01, 0.02,
side * 1.1, -0.25, 0.18,
side * 0.46, -0.16, 0.17
]);
const indices = side < 0
? [0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5]
: [0, 2, 1, 0, 3, 2, 0, 4, 3, 0, 5, 4];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(vertices, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createBat() {
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.SphereGeometry(0.25, 8, 6),
batBodyMaterial
);
body.scale.set(0.78, 0.9, 1.35);
body.castShadow = true;
group.add(body);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 8, 6),
batBodyMaterial
);
head.position.set(0, 0.04, 0.33);
head.scale.set(1, 0.92, 0.9);
head.castShadow = true;
group.add(head);
for (const side of [-1, 1]) {
const ear = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.29, 5),
batBodyMaterial
);
ear.position.set(side * 0.11, 0.25, 0.3);
ear.rotation.z = side * -0.13;
group.add(ear);
const eye = new THREE.Mesh(
new THREE.SphereGeometry(0.027, 6, 4),
eyeMaterial
);
eye.position.set(side * 0.075, 0.075, 0.485);
group.add(eye);
}
const leftWing = new THREE.Mesh(
createWingGeometry(-1),
batWingMaterial
);
const rightWing = new THREE.Mesh(
createWingGeometry(1),
batWingMaterial
);
leftWing.castShadow = true;
rightWing.castShadow = true;
group.add(leftWing, rightWing);
return {
group,
leftWing,
rightWing,
phase: between(0, Math.PI * 2)
};
}
const routePoints = [
[
[-1.25, 2.05, -5.35],
[-2.25, 2.65, -4.25],
[-3.45, 2.8, -2.85],
[-3.25, 2.55, -1.25],
[-2.25, 2.8, 0.45],
[-0.65, 2.65, 2.35],
[1.25, 2.9, 4.55]
],
[
[-0.35, 1.82, -5.25],
[0.55, 2.35, -3.85],
[1.92, 2.65, -2.75],
[2.18, 3.0, -1.35],
[1.65, 3.1, 0.15],
[0.95, 2.85, 2.3],
[2.5, 3.1, 4.65]
],
[
[0.85, 2.45, -5.18],
[1.75, 2.95, -3.7],
[3.3, 3.25, -2.45],
[4.15, 3.15, -1.1],
[3.9, 3.4, 0.65],
[3.15, 3.05, 2.55],
[4.8, 3.45, 4.9]
],
[
[-1.8, 1.28, -4.95],
[-2.15, 1.8, -3.45],
[-1.2, 2.25, -2.5],
[-0.48, 2.35, -1.3],
[-0.72, 2.65, 0.25],
[-0.15, 2.7, 2.45],
[1.55, 3.0, 4.7]
]
];
const routeMaterial = new THREE.LineDashedMaterial({
color: 0x70dfe1,
transparent: true,
opacity: 0.2,
dashSize: 0.12,
gapSize: 0.15,
depthWrite: false
});
const bats = routePoints.map((points, index) => {
const curve = new THREE.CatmullRomCurve3(
points.map((p) => new THREE.Vector3(...p)),
false,
"catmullrom",
0.45
);
const bat = createBat();
scene.add(bat.group);
const routeGeometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(140)
);
const routeLine = new THREE.Line(routeGeometry, routeMaterial);
routeLine.visible = false;
scene.add(routeLine);
return {
...bat,
curve,
routeLine,
offset: 0.04 + index * 0.115,
speed: 0.038 + index * 0.003,
echoRock: index
};
});
// Sparse suspended dust catches the cave and moonlight.
const dustPositions = [];
for (let i = 0; i < 190; i++) {
dustPositions.push(
between(-10, 10),
between(0.3, 8),
between(-7, 8)
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x8ebdcc,
size: 0.025,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
scene.add(dust);
// Echo wavefronts. Their opacity stays deliberately restrained.
const pulseGeometry = new THREE.RingGeometry(0.93, 1, 64);
const pulses = [];
function spawnPulse(bat) {
const position = bat.group.position.clone();
for (let i = 0; i < 3; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0x70e9e5,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const ring = new THREE.Mesh(pulseGeometry, material);
ring.position.copy(position);
ring.scale.setScalar(0.2);
ring.renderOrder = 2;
scene.add(ring);
pulses.push({
ring,
startedAt: simulationTime,
delay: i * 0.22,
duration: 1.15 + i * 0.08
});
}
projections[bat.echoRock].pulseAt = simulationTime;
echoLabelUntil = simulationTime + 2.5;
}
// Camera controls are implemented directly without addons.
const cameraState = {
yaw: 0.06,
pitch: 0.13,
radius: 14.6,
target: new THREE.Vector3(0, 2.15, -0.45)
};
const defaultCamera = {
yaw: cameraState.yaw,
pitch: cameraState.pitch,
radius: cameraState.radius
};
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
cameraState.radius * Math.sin(cameraState.yaw) * cp,
cameraState.target.y +
cameraState.radius * Math.sin(cameraState.pitch),
cameraState.target.z +
cameraState.radius * Math.cos(cameraState.yaw) * cp
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
cameraState.radius = defaultCamera.radius;
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
-0.08,
0.62
);
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.01,
8,
22
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
let paused = false;
let simulationTime = 0;
let nextPulseAt = 2.2;
let pulseIndex = 0;
let echoLabelUntil = -1;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.classList.toggle("active", paused);
pauseButton.setAttribute("aria-pressed", String(paused));
});
pathsButton.addEventListener("click", () => {
const visible = !pathsButton.classList.contains("active");
pathsButton.classList.toggle("active", visible);
pathsButton.setAttribute("aria-pressed", String(visible));
bats.forEach((bat) => {
bat.routeLine.visible = visible;
});
});
cameraResetButton.addEventListener("click", resetCamera);
restartButton.addEventListener("click", () => {
simulationTime = 0;
nextPulseAt = 1.2;
pulseIndex = 0;
echoLabelUntil = -1;
pulses.forEach(({ ring, material }) => {
scene.remove(ring);
material.dispose();
});
pulses.length = 0;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
} else if (event.key.toLowerCase() === "r") {
resetCamera();
} else if (event.key.toLowerCase() === "p") {
pathsButton.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
});
const forward = new THREE.Vector3(0, 0, 1);
const orientation = new THREE.Quaternion();
const bankRotation = new THREE.Quaternion();
const projectedPosition = new THREE.Vector3();
function updateEchoLabel() {
const visible = simulationTime < echoLabelUntil;
echoLabel.classList.toggle("visible", visible);
if (!visible) return;
const activeProjection = projections[pulseIndex % projections.length];
activeProjection.group.getWorldPosition(projectedPosition);
projectedPosition.y += 2.35;
projectedPosition.project(camera);
const onScreen =
projectedPosition.z > -1 &&
projectedPosition.z < 1 &&
Math.abs(projectedPosition.x) < 1.2 &&
Math.abs(projectedPosition.y) < 1.2;
echoLabel.style.visibility = onScreen ? "visible" : "hidden";
if (onScreen) {
const x = (projectedPosition.x * 0.5 + 0.5) * innerWidth;
const y = (-projectedPosition.y * 0.5 + 0.5) * innerHeight;
echoLabel.style.left = `${x}px`;
echoLabel.style.top = `${y}px`;
}
}
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += delta;
}
updateCamera();
bats.forEach((bat) => {
const progress =
(simulationTime * bat.speed + bat.offset) % 1;
const position = bat.curve.getPointAt(progress);
const tangent = bat.curve.getTangentAt(progress).normalize();
bat.group.position.copy(position);
orientation.setFromUnitVectors(forward, tangent);
bankRotation.setFromAxisAngle(
forward,
Math.sin(simulationTime * 1.7 + bat.phase) * 0.16
);
orientation.multiply(bankRotation);
bat.group.quaternion.copy(orientation);
const edgeFade = Math.min(
1,
progress * 10,
(1 - progress) * 10
);
bat.group.scale.setScalar(0.72 * Math.max(0.01, edgeFade));
const flap = Math.sin(simulationTime * 12 + bat.phase) * 0.55;
bat.leftWing.rotation.z = -flap;
bat.rightWing.rotation.z = flap;
});
// Only one bat pulses at a time, with several seconds between events.
if (!paused && simulationTime >= nextPulseAt) {
const pulsingBat = bats[pulseIndex % bats.length];
spawnPulse(pulsingBat);
pulseIndex++;
nextPulseAt = simulationTime + 4.8;
}
for (let i = pulses.length - 1; i >= 0; i--) {
const pulse = pulses[i];
const age = simulationTime - pulse.startedAt - pulse.delay;
if (age < 0) {
pulse.ring.visible = false;
continue;
}
pulse.ring.visible = true;
const t = age / pulse.duration;
if (t >= 1) {
scene.remove(pulse.ring);
pulse.ring.material.dispose();
pulses.splice(i, 1);
continue;
}
const eased = 1 - Math.pow(1 - t, 2);
pulse.ring.scale.setScalar(0.22 + eased * 4.25);
pulse.ring.material.opacity = (1 - t) * 0.23;
pulse.ring.quaternion.copy(camera.quaternion);
}
projections.forEach((projection) => {
const age = simulationTime - projection.pulseAt;
const strength = Math.max(0, 1 - age / 1.9);
projection.material.emissiveIntensity = strength * 0.72;
});
dust.rotation.y = simulationTime * 0.012;
dust.position.y = Math.sin(simulationTime * 0.18) * 0.06;
updateEchoLabel();
renderer.render(scene, camera);
}
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```