Prompt specification
Build or customize the Three.js 3D scene "Bat pulse and target distance" 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: Bat pulse and target distance
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/bat-pulse-and-target-distance-x0645
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring bat echolocation. Place a stylized bat and two suspended targets in a dark display chamber; show expanding pulse rings as a symbolic visualization of sound propagation. Stylized solid forms.
Working Three.js Starter (bat-pulse-and-target-distance-x0645):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Echolocation Chamber</title>
<style>
:root {
color-scheme: dark;
--cyan: #61e8ff;
--cyan-dim: #1b7282;
--amber: #f2b75d;
--coral: #ff6477;
--text: #d9edf4;
--muted: #78919e;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #03060b;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 44%, transparent 40%, rgba(0, 4, 9, 0.34) 76%, rgba(0, 2, 6, 0.82) 100%),
linear-gradient(to bottom, rgba(20, 58, 70, 0.03), transparent 24%);
z-index: 2;
}
.hud {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
color: var(--text);
text-transform: uppercase;
letter-spacing: 0.12em;
}
.title-block {
position: absolute;
top: 30px;
left: 34px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.24em;
}
.eyebrow::before {
content: "";
width: 22px;
height: 1px;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
h1 {
margin: 10px 0 7px;
font-size: clamp(22px, 3vw, 38px);
line-height: 1;
font-weight: 500;
letter-spacing: 0.055em;
}
.subtitle {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.18em;
}
.readout {
position: absolute;
top: 30px;
right: 34px;
width: 190px;
padding: 16px 17px;
border: 1px solid rgba(97, 232, 255, 0.18);
background: linear-gradient(135deg, rgba(8, 20, 29, 0.78), rgba(5, 10, 17, 0.52));
backdrop-filter: blur(8px);
box-shadow: inset 0 0 24px rgba(24, 119, 143, 0.05);
}
.readout-label {
margin-bottom: 12px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.22em;
}
.metric {
display: flex;
align-items: baseline;
justify-content: space-between;
margin: 7px 0;
font-size: 9px;
}
.metric strong {
color: var(--cyan);
font-size: 14px;
font-weight: 500;
letter-spacing: 0.08em;
text-shadow: 0 0 12px rgba(97, 232, 255, 0.55);
}
.targets {
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid rgba(97, 232, 255, 0.12);
}
.target-row {
display: flex;
align-items: center;
gap: 9px;
margin: 8px 0;
color: #91a7b1;
font-size: 8px;
transition: color 0.15s;
}
.target-dot {
width: 7px;
height: 7px;
border: 1px solid currentColor;
border-radius: 50%;
}
.target-row.active {
color: var(--amber);
}
.target-row.active .target-dot {
background: currentColor;
box-shadow: 0 0 11px currentColor;
}
.instructions {
position: absolute;
bottom: 28px;
left: 34px;
color: #627b88;
font-size: 8px;
line-height: 1.8;
letter-spacing: 0.17em;
}
.instructions span {
color: var(--cyan);
}
.status {
position: absolute;
right: 34px;
bottom: 28px;
color: #627b88;
font-size: 8px;
letter-spacing: 0.17em;
text-align: right;
}
.status-line {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 9px;
}
.status-line::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
animation: blink 1.4s ease-in-out infinite;
}
@keyframes blink {
50% {
opacity: 0.35;
}
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: var(--cyan);
background: #03060b;
font-size: 10px;
letter-spacing: 0.25em;
transition: opacity 0.5s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.title-block {
top: 20px;
left: 20px;
}
.readout {
top: auto;
right: 20px;
bottom: 66px;
width: 160px;
padding: 12px;
}
.instructions {
left: 20px;
bottom: 20px;
}
.status {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div class="hud">
<div class="title-block">
<div class="eyebrow">Bio-acoustic display / chamber 02</div>
<h1>Echolocation</h1>
<div class="subtitle">Stylized pulse-propagation visualization</div>
</div>
<div class="readout">
<div class="readout-label">Live acoustic array</div>
<div class="metric">
<span>Active pulses</span>
<strong id="pulse-count">03</strong>
</div>
<div class="metric">
<span>Envelope radius</span>
<strong id="pulse-range">0.0 m</strong>
</div>
<div class="targets">
<div class="target-row" id="target-a">
<span class="target-dot"></span>
Target A / suspended
</div>
<div class="target-row" id="target-b">
<span class="target-dot"></span>
Target B / suspended
</div>
</div>
</div>
<div class="instructions">
<span>Drag</span> to orbit /
<span>Scroll</span> to zoom /
<span>R</span> to reset
</div>
<div class="status">
<div class="status-line">Sonar array operational</div>
<div id="clock" style="margin-top:7px;">00:00.0</div>
</div>
</div>
<div id="loading">Initializing acoustic array</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.getElementById("scene");
const loading = document.getElementById("loading");
// Renderer
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.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
// Scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x03060b);
scene.fog = new THREE.FogExp2(0x05080d, 0.034);
const camera = new THREE.PerspectiveCamera(
46,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Custom camera controls
const orbit = {
target: new THREE.Vector3(0, 1.0, 0),
radius: 10.6,
theta: 0.29,
phi: 1.25
};
function updateCamera() {
const spherical = new THREE.Spherical(orbit.radius, orbit.phi, orbit.theta);
camera.position.setFromSpherical(spherical).add(orbit.target);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.radius = 10.6;
orbit.theta = 0.29;
orbit.phi = 1.25;
updateCamera();
}
updateCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
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;
orbit.theta -= dx * 0.005;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.66, 1.48);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
6.2,
15.5
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "ArrowLeft") orbit.theta += 0.08;
if (event.code === "ArrowRight") orbit.theta -= 0.08;
if (event.code === "ArrowUp") orbit.phi = Math.max(0.66, orbit.phi - 0.06);
if (event.code === "ArrowDown") orbit.phi = Math.min(1.48, orbit.phi + 0.06);
if (event.code === "KeyR") resetCamera();
updateCamera();
});
// Lighting
scene.add(new THREE.HemisphereLight(0x789ab8, 0x080b10, 0.62));
const keyLight = new THREE.DirectionalLight(0xb4d2ec, 2.1);
keyLight.position.set(2.5, 7.5, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
scene.add(keyLight);
const cyanSpot = new THREE.SpotLight(0x54dcff, 85, 18, 0.52, 0.72, 1.4);
cyanSpot.position.set(-4.5, 4.5, 4.2);
cyanSpot.target.position.set(-2.8, 1.1, 0);
scene.add(cyanSpot, cyanSpot.target);
const amberSpot = new THREE.SpotLight(0xffbc6b, 72, 17, 0.5, 0.7, 1.5);
amberSpot.position.set(4.5, 5.0, 3.5);
amberSpot.target.position.set(1.4, 1.6, -0.5);
scene.add(amberSpot, amberSpot.target);
const rimLight = new THREE.PointLight(0xa23b72, 16, 8, 2);
rimLight.position.set(-4.5, 0.5, -2.6);
scene.add(rimLight);
// Shared materials
const chamberMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1119,
roughness: 0.9,
metalness: 0.12
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x101923,
roughness: 0.82,
metalness: 0.16
});
const trimMaterial = new THREE.MeshStandardMaterial({
color: 0x1b2a36,
roughness: 0.62,
metalness: 0.48
});
const darkTrimMaterial = new THREE.MeshStandardMaterial({
color: 0x081017,
roughness: 0.7,
metalness: 0.4
});
const cyanTrimMaterial = new THREE.MeshStandardMaterial({
color: 0x123d48,
emissive: 0x0b6377,
emissiveIntensity: 0.8,
roughness: 0.5,
metalness: 0.25
});
function addBox(size, position, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Display chamber
addBox([12, 0.2, 10], [0, -1.55, 0], floorMaterial);
addBox([12, 6.4, 0.22], [0, 1.65, -4.8], chamberMaterial);
addBox([0.22, 6.4, 10], [-5.9, 1.65, 0], chamberMaterial);
addBox([0.22, 6.4, 10], [5.9, 1.65, 0], chamberMaterial);
addBox([12, 0.2, 10], [0, 4.75, 0], darkTrimMaterial);
// Back-wall ribs and acoustic panels
for (let i = 0; i < 8; i++) {
addBox(
[0.09, 5.65, 0.16],
[-5.2 + i * 1.48, 1.5, -4.61],
trimMaterial
);
}
for (const x of [-4.05, -1.85, 2.05, 4.25]) {
addBox([1.22, 4.7, 0.08], [x, 1.45, -4.63], darkTrimMaterial);
addBox([0.035, 3.8, 0.04], [x + 0.42, 1.45, -4.56], cyanTrimMaterial);
}
// Floor seams
for (const z of [-3.4, -1.8, 1.4, 3.5]) {
addBox([10.9, 0.012, 0.018], [0, -1.44, z], darkTrimMaterial);
}
// Dim sonar graphic mounted on the rear wall
const rearGraphicMaterial = new THREE.MeshBasicMaterial({
color: 0x14505d,
transparent: true,
opacity: 0.26,
depthWrite: false
});
for (const radius of [1.25, 1.85, 2.55]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.018, 6, 100),
rearGraphicMaterial
);
ring.position.set(0.1, 1.35, -4.48);
scene.add(ring);
}
// Low launch pad beneath the bat
const launchPad = new THREE.Mesh(
new THREE.CylinderGeometry(1.25, 1.38, 0.13, 48),
trimMaterial
);
launchPad.position.set(-3.15, -1.42, 0.25);
launchPad.receiveShadow = true;
scene.add(launchPad);
const launchRing = new THREE.Mesh(
new THREE.TorusGeometry(1.04, 0.026, 8, 72),
cyanTrimMaterial
);
launchRing.rotation.x = Math.PI / 2;
launchRing.position.set(-3.15, -1.345, 0.25);
scene.add(launchRing);
// Helpers
const up = new THREE.Vector3(0, 1, 0);
function cylinderBetween(start, end, radius, material, parent = scene, segments = 10) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function setCylinderBetween(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.set(1, length, 1);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
}
// Stylized bat
function createBat() {
const root = new THREE.Group();
root.position.set(-3.15, 1.25, 0.25);
root.rotation.y = -0.05;
scene.add(root);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x24162e,
roughness: 0.72,
metalness: 0.06
});
const bellyMaterial = new THREE.MeshStandardMaterial({
color: 0x4c2a50,
roughness: 0.85
});
const wingMaterial = new THREE.MeshStandardMaterial({
color: 0x3b193d,
roughness: 0.8,
metalness: 0.02,
side: THREE.DoubleSide,
flatShading: true
});
const wingBoneMaterial = new THREE.MeshStandardMaterial({
color: 0x8b4865,
roughness: 0.58,
metalness: 0.12
});
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0xffb4c5,
emissive: 0xff315f,
emissiveIntensity: 3.2,
roughness: 0.25
});
const noseMaterial = new THREE.MeshStandardMaterial({
color: 0xff738c,
emissive: 0x7b1538,
emissiveIntensity: 1.1,
roughness: 0.45
});
function sphere(scale, position, material, segments = 28) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(1, segments, Math.max(16, segments / 2)),
material
);
mesh.scale.set(...scale);
mesh.position.set(...position);
mesh.castShadow = true;
root.add(mesh);
return mesh;
}
sphere([0.69, 0.48, 0.36], [0, 0, 0], bodyMaterial, 32);
sphere([0.38, 0.34, 0.13], [-0.02, -0.03, 0.30], bellyMaterial);
sphere([0.38, 0.36, 0.34], [0.64, 0.07, 0], bodyMaterial, 28);
sphere([0.25, 0.18, 0.23], [0.96, -0.02, 0], bellyMaterial);
// Ears
for (const z of [-0.19, 0.19]) {
const ear = new THREE.Mesh(
new THREE.ConeGeometry(0.15, 0.52, 5),
bodyMaterial
);
ear.position.set(0.56, 0.48, z);
ear.rotation.z = z < 0 ? -0.10 : 0.10;
ear.castShadow = true;
root.add(ear);
}
// Eyes
for (const z of [-0.205, 0.205]) {
const eye = sphere([0.075, 0.075, 0.075], [0.96, 0.16, z], eyeMaterial, 18);
eye.castShadow = false;
}
const nose = sphere([0.085, 0.07, 0.085], [1.075, -0.03, 0], noseMaterial, 18);
nose.castShadow = false;
// Small fangs
for (const z of [-0.10, 0.10]) {
const fang = new THREE.Mesh(
new THREE.ConeGeometry(0.035, 0.14, 5),
new THREE.MeshStandardMaterial({ color: 0xd9d3c9, roughness: 0.45 })
);
fang.position.set(1.015, -0.23, z);
fang.rotation.z = Math.PI;
fang.castShadow = true;
root.add(fang);
}
// Faceted, solid membrane wing
const wingShape = new THREE.Shape();
wingShape.moveTo(0.32, 0.18);
wingShape.lineTo(0.02, 0.48);
wingShape.lineTo(-0.52, 0.40);
wingShape.lineTo(-1.28, 0.28);
wingShape.lineTo(-1.68, -0.08);
wingShape.lineTo(-1.30, -0.20);
wingShape.lineTo(-0.94, -0.36);
wingShape.lineTo(-0.52, -0.30);
wingShape.lineTo(-0.20, -0.18);
wingShape.closePath();
const wingGeometry = new THREE.ExtrudeGeometry(wingShape, {
depth: 0.08,
bevelEnabled: true,
bevelSize: 0.025,
bevelThickness: 0.022,
bevelSegments: 1,
steps: 1,
curveSegments: 1
});
wingGeometry.translate(0, 0, -0.04);
wingGeometry.rotateX(Math.PI / 2);
function createWing(sign) {
const wing = new THREE.Group();
root.add(wing);
const membrane = new THREE.Mesh(wingGeometry, wingMaterial);
if (sign < 0) membrane.scale.z = -1;
membrane.castShadow = true;
membrane.receiveShadow = true;
wing.add(membrane);
const p = (x, z) => new THREE.Vector3(x, 0.075, z * sign);
const shoulder = p(0.22, 0.18);
const wrist = p(-0.36, 1.15);
const tip = p(-1.15, 2.64);
const edgeA = p(-1.64, -0.08);
const edgeB = p(-0.95, -0.35);
const edgeC = p(-0.52, -0.29);
cylinderBetween(shoulder, wrist, 0.042, wingBoneMaterial, wing);
cylinderBetween(wrist, tip, 0.034, wingBoneMaterial, wing);
cylinderBetween(wrist, edgeA, 0.029, wingBoneMaterial, wing);
cylinderBetween(wrist, edgeB, 0.026, wingBoneMaterial, wing);
cylinderBetween(wrist, edgeC, 0.025, wingBoneMaterial, wing);
cylinderBetween(p(-0.35, 0.18), p(-0.36, 1.15), 0.034, wingBoneMaterial, wing);
return wing;
}
const nearWing = createWing(1);
const farWing = createWing(-1);
// Sonar emitter at the bat's mouth
const emitter = new THREE.Object3D();
emitter.position.set(1.15, 0.01, 0);
root.add(emitter);
const emitterMaterial = new THREE.MeshBasicMaterial({
color: 0x9af5ff,
transparent: true,
opacity: 0.9
});
const emitterGlow = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 16, 12),
emitterMaterial
);
emitter.add(emitterGlow);
const emitterLight = new THREE.PointLight(0x55e8ff, 9, 4.5, 2);
emitter.add(emitterLight);
return {
root,
nearWing,
farWing,
emitter,
emitterGlow,
emitterLight,
basePosition: root.position.clone()
};
}
const bat = createBat();
// Suspended targets
const targetWireMaterial = new THREE.MeshStandardMaterial({
color: 0x657782,
roughness: 0.4,
metalness: 0.8
});
function createTarget(position, accentColor, rotationY, phase) {
const group = new THREE.Group();
group.position.copy(position);
group.rotation.y = rotationY;
scene.add(group);
const outerMaterial = new THREE.MeshStandardMaterial({
color: 0xd3a454,
emissive: accentColor,
emissiveIntensity: 0.28,
roughness: 0.38,
metalness: 0.72
});
const innerMaterial = new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.32,
roughness: 0.5,
metalness: 0.1
});
const backingMaterial = new THREE.MeshStandardMaterial({
color: 0x111820,
roughness: 0.55,
metalness: 0.6
});
const faceMaterial = new THREE.MeshStandardMaterial({
color: 0xd9d4c8,
roughness: 0.62,
metalness: 0.06
});
const backing = new THREE.Mesh(
new THREE.CylinderGeometry(0.53, 0.53, 0.10, 48),
backingMaterial
);
backing.rotation.x = Math.PI / 2;
backing.castShadow = true;
group.add(backing);
const outer = new THREE.Mesh(
new THREE.TorusGeometry(0.63, 0.095, 12, 72),
outerMaterial
);
outer.castShadow = true;
group.add(outer);
const face = new THREE.Mesh(
new THREE.CylinderGeometry(0.39, 0.39, 0.09, 48),
faceMaterial
);
face.rotation.x = Math.PI / 2;
face.position.z = 0.075;
face.castShadow = true;
group.add(face);
const inner = new THREE.Mesh(
new THREE.CylinderGeometry(0.215, 0.215, 0.12, 40),
innerMaterial
);
inner.rotation.x = Math.PI / 2;
inner.position.z = 0.11;
inner.castShadow = true;
group.add(inner);
const bullseye = new THREE.Mesh(
new THREE.TorusGeometry(0.285, 0.027, 8, 48),
outerMaterial
);
bullseye.position.z = 0.135;
group.add(bullseye);
const center = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.07, 0.135, 24),
backingMaterial
);
center.rotation.x = Math.PI / 2;
center.position.z = 0.145;
group.add(center);
// Four calibration ticks
for (let i = 0; i < 4; i++) {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.035, 0.07),
outerMaterial
);
const angle = i * Math.PI / 2;
tick.position.set(Math.sin(angle) * 0.74, Math.cos(angle) * 0.74, 0.015);
tick.rotation.z = -angle;
group.add(tick);
}
const bracket = addBox([0.30, 0.17, 0.22], [0, 0.77, 0], trimMaterial, group);
const anchor = new THREE.Vector3(position.x, 4.57, position.z);
const cable = new THREE.Mesh(
new THREE.CylinderGeometry(0.012, 0.012, 1, 8),
targetWireMaterial
);
scene.add(cable);
const mount = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.12, 0.12, 16),
trimMaterial
);
mount.position.copy(anchor);
scene.add(mount);
return {
group,
outerMaterial,
innerMaterial,
cable,
anchor,
basePosition: position.clone(),
baseRotation: rotationY,
phase,
hit: 0,
color: accentColor
};
}
const targets = [
createTarget(new THREE.Vector3(0.85, 2.55, -1.15), 0xff596f, 0.20, 0.4),
createTarget(new THREE.Vector3(3.05, 0.78, 0.35), 0x5be5ff, 0.02, 2.2)
];
// Expanding sonar wave packets
const pulses = [];
const impacts = [];
const pulseRingGeometry = new THREE.TorusGeometry(1, 0.013, 8, 100);
const pulseCoreGeometry = new THREE.SphereGeometry(1, 12, 8);
function spawnPulse(initialAge = 0) {
const origin = new THREE.Vector3();
bat.emitter.getWorldPosition(origin);
const group = new THREE.Group();
group.position.copy(origin);
scene.add(group);
const material = new THREE.MeshBasicMaterial({
color: 0x67ebff,
transparent: true,
opacity: 0.58,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const ringXY = new THREE.Mesh(pulseRingGeometry, material);
const ringYZ = new THREE.Mesh(pulseRingGeometry, material);
const ringXZ = new THREE.Mesh(pulseRingGeometry, material);
ringYZ.rotation.y = Math.PI / 2;
ringXZ.rotation.x = Math.PI / 2;
group.add(ringXY, ringYZ, ringXZ);
const coreMaterial = new THREE.MeshBasicMaterial({
color: 0xd5fbff,
transparent: true,
opacity: 0.9,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const core = new THREE.Mesh(pulseCoreGeometry, coreMaterial);
core.scale.setScalar(0.027);
group.add(core);
pulses.push({
group,
material,
coreMaterial,
core,
age: initialAge,
life: 2.9,
origin: origin.clone(),
hitTargets: new Set()
});
}
function spawnImpact(target) {
const material = new THREE.MeshBasicMaterial({
color: target.color,
transparent: true,
opacity: 0.9,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(0.68, 0.027, 8, 64),
material
);
mesh.position.copy(target.group.position);
mesh.quaternion.copy(target.group.quaternion);
scene.add(mesh);
impacts.push({ mesh, material, age: 0 });
}
// Pre-populate the chamber so the visualization is active immediately.
spawnPulse(0.22);
spawnPulse(0.94);
spawnPulse(1.68);
let nextPulseTime = 0.85;
let emitterFlash = 0;
let elapsed = 0;
let hudTimer = 0;
const clock = new THREE.Clock();
const pulseCountElement = document.getElementById("pulse-count");
const pulseRangeElement = document.getElementById("pulse-range");
const clockElement = document.getElementById("clock");
const targetAELEMENT = document.getElementById("target-a");
const targetBELEMENT = document.getElementById("target-b");
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
hudTimer += dt;
// Bat motion and wing flap
bat.root.position.x = bat.basePosition.x + Math.sin(elapsed * 0.82) * 0.075;
bat.root.position.y = bat.basePosition.y + Math.sin(elapsed * 2.1) * 0.16;
bat.root.rotation.x = Math.sin(elapsed * 1.45) * 0.025;
bat.root.rotation.z = Math.sin(elapsed * 1.1) * 0.035;
const flap = Math.sin(elapsed * 6.1) * 0.19;
bat.nearWing.rotation.x = flap;
bat.farWing.rotation.x = -flap;
emitterFlash = Math.max(0, emitterFlash - dt * 4.5);
bat.emitterGlow.scale.setScalar(1 + emitterFlash * 1.3);
bat.emitterLight.intensity = 9 + emitterFlash * 34;
// Target suspension and sway
for (const target of targets) {
target.group.position.x =
target.basePosition.x + Math.sin(elapsed * 0.7 + target.phase) * 0.055;
target.group.position.y =
target.basePosition.y + Math.sin(elapsed * 0.92 + target.phase) * 0.045;
target.group.position.z =
target.basePosition.z + Math.sin(elapsed * 0.62 + target.phase) * 0.035;
target.group.rotation.y =
target.baseRotation + Math.sin(elapsed * 0.55 + target.phase) * 0.035;
target.hit *= Math.exp(-dt * 3.5);
target.group.scale.setScalar(1 + target.hit * 0.065);
target.outerMaterial.emissiveIntensity = 0.28 + target.hit * 2.4;
target.innerMaterial.emissiveIntensity = 0.32 + target.hit * 1.8;
const cableBottom = target.group.position.clone();
cableBottom.y += 0.86;
setCylinderBetween(target.cable, target.anchor, cableBottom);
}
// Emit new wave packets
if (elapsed >= nextPulseTime) {
spawnPulse();
emitterFlash = 1;
nextPulseTime += 1.48;
}
// Update expanding orthogonal rings
for (let i = pulses.length - 1; i >= 0; i--) {
const pulse = pulses[i];
pulse.age += dt;
if (pulse.age >= pulse.life) {
scene.remove(pulse.group);
pulse.material.dispose();
pulse.coreMaterial.dispose();
pulses.splice(i, 1);
continue;
}
const progress = pulse.age / pulse.life;
const radius = 0.12 + pulse.age * 2.45;
pulse.group.scale.setScalar(radius);
pulse.material.opacity = 0.58 * Math.pow(1 - progress, 1.35);
pulse.coreMaterial.opacity = Math.max(0, 0.88 * (1 - progress * 2.8));
// Detect target intersections
targets.forEach((target, targetIndex) => {
if (pulse.hitTargets.has(targetIndex)) return;
const targetPosition = new THREE.Vector3();
target.group.getWorldPosition(targetPosition);
const distance = pulse.origin.distanceTo(targetPosition);
if (radius >= distance) {
pulse.hitTargets.add(targetIndex);
target.hit = 1;
spawnImpact(target);
}
});
}
// Target-local confirmation rings
for (let i = impacts.length - 1; i >= 0; i--) {
const impact = impacts[i];
impact.age += dt;
if (impact.age >= 0.68) {
scene.remove(impact.mesh);
impact.mesh.geometry.dispose();
impact.material.dispose();
impacts.splice(i, 1);
continue;
}
const progress = impact.age / 0.68;
impact.mesh.scale.setScalar(0.92 + progress * 1.15);
impact.material.opacity = Math.pow(1 - progress, 1.4) * 0.9;
}
// HUD
if (hudTimer > 0.1) {
hudTimer = 0;
const maximumRadius = pulses.reduce(
(maximum, pulse) => Math.max(maximum, 0.12 + pulse.age * 2.45),
0
);
pulseCountElement.textContent = String(pulses.length).padStart(2, "0");
pulseRangeElement.textContent = `${maximumRadius.toFixed(1)} m`;
targetAELEMENT.classList.toggle("active", targets[0].hit > 0.08);
targetBELEMENT.classList.toggle("active", targets[1].hit > 0.08);
const minutes = Math.floor(elapsed / 60).toString().padStart(2, "0");
const seconds = (elapsed % 60).toFixed(1).padStart(4, "0");
clockElement.textContent = `${minutes}:${seconds}`;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// Reveal the scene after the first rendered frame.
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 600);
});
</script>
</body>
</html>
```