Prompt specification
Build or customize the Three.js 3D scene "Huddle position exchange" 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: Huddle position exchange
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/huddle-position-exchange-x0702
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts penguin huddles. Animate a small huddle gradually rearranging so individuals change boundary positions; preserve spacing and use a symbolic exposure indicator tied only to local neighbors. Voxel style.
Working Three.js Starter (huddle-position-exchange-x0702):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Penguin Huddles</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(7, 20, 31, 0.82);
--border: rgba(151, 207, 220, 0.22);
--text: #e5f0f2;
--muted: #9db5bd;
--amber: #ffc65c;
--white: #f4f6ed;
--cyan: #58d8cf;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #081725;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
color: var(--text);
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
#title {
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 17px 18px;
}
#title .eyebrow {
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.22em;
text-transform: uppercase;
}
#title h1 {
margin: 7px 0 5px;
font-size: clamp(18px, 2.5vw, 27px);
font-weight: 650;
letter-spacing: -0.04em;
}
#title p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
#status {
top: 22px;
right: 22px;
min-width: 235px;
padding: 14px 16px;
font-size: 11px;
line-height: 1.75;
}
#status strong {
display: block;
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#status .row {
display: flex;
justify-content: space-between;
gap: 22px;
}
#status .value {
color: var(--text);
}
#legend {
left: 22px;
bottom: 22px;
max-width: min(395px, calc(100vw - 44px));
padding: 15px 17px;
font-size: 10px;
line-height: 1.65;
}
#legend .legend-title {
margin-bottom: 8px;
color: var(--text);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#legend .explanation {
margin: 0 0 10px;
color: var(--muted);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 4px;
}
.swatch {
display: inline-block;
width: 10px;
height: 10px;
box-shadow: 0 0 10px currentColor;
}
.amber { color: var(--amber); background: var(--amber); }
.white { color: var(--white); background: var(--white); }
.cyan { color: var(--cyan); background: var(--cyan); }
#controls {
right: 22px;
bottom: 22px;
padding: 10px 13px;
color: var(--muted);
font-size: 10px;
line-height: 1.6;
text-align: right;
}
@media (max-width: 720px) {
#status {
top: auto;
right: 16px;
bottom: 78px;
min-width: 205px;
}
#title {
top: 16px;
left: 16px;
}
#legend {
left: 16px;
bottom: 16px;
max-width: calc(100vw - 32px);
}
#controls {
display: none;
}
}
</style>
</head>
<body>
<section id="title" class="hud panel">
<div class="eyebrow">Arctic behavior study / 07</div>
<h1>Voxel Huddle Field</h1>
<p>
A small huddle slowly exchanges boundary positions while maintaining
clearance. The highlighted birds carry local-neighbor exposure badges.
</p>
</section>
<section id="status" class="hud panel">
<strong>Observed small huddle</strong>
<div class="row">
<span>Penguins</span>
<span class="value" id="birdCount">7</span>
</div>
<div class="row">
<span>Local radius</span>
<span class="value" id="radiusReadout">1.22 units</span>
</div>
<div class="row">
<span>Exposed / enclosed</span>
<span class="value" id="exposureReadout">—</span>
</div>
</section>
<section id="legend" class="hud panel">
<div class="legend-title">Local exposure indicator</div>
<p class="explanation">
Each square pip represents one neighboring penguin inside the local
radius. Pip count and color are derived only from that local count.
</p>
<div class="legend-row">
<i class="swatch amber"></i>
<span>0–2 neighbors: exposed</span>
</div>
<div class="legend-row">
<i class="swatch white"></i>
<span>3 neighbors: linked</span>
</div>
<div class="legend-row">
<i class="swatch cyan"></i>
<span>4–6 neighbors: enclosed</span>
</div>
</section>
<section id="controls" class="hud panel">
drag: orbit<br>
shift / right-drag: pan<br>
wheel: zoom · double-click: reset
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel scene showing groups of penguins huddling in snow"
);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091a2a);
scene.fog = new THREE.Fog(0x091a2a, 20, 48);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xc8eaff, 0x243541, 2.15);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff3d4, 3.2);
keyLight.position.set(8, 15, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -15;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x79cfff, 1.25);
rimLight.position.set(-9, 7, -8);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Shared materials and voxel geometry helpers
// -------------------------------------------------------------------------
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
...options
});
const materials = {
ocean: standard(0x071925, { roughness: 0.48 }),
ice: standard(0x78aebe),
iceLight: standard(0xa6d4df),
iceDark: standard(0x5b91a4),
snow: standard(0xdce9ec),
snowBright: standard(0xf1f6f4),
snowBlue: standard(0xb9d1da),
penguin: standard(0x111b25),
penguinAlt: standard(0x1c2a36),
belly: standard(0xf0f2e7),
orange: standard(0xf1a43b),
eye: standard(0x020508),
badgePanel: new THREE.MeshBasicMaterial({
color: 0x081a25,
transparent: true,
opacity: 0.9
}),
badgeOff: new THREE.MeshBasicMaterial({ color: 0x29404a }),
snowflake: new THREE.MeshBasicMaterial({
color: 0xe9f5f7,
transparent: true,
opacity: 0.72
})
};
const geometryCache = new Map();
function boxGeometry(w, h, d) {
const key = `${w}:${h}:${d}`;
if (!geometryCache.has(key)) {
geometryCache.set(key, new THREE.BoxGeometry(w, h, d));
}
return geometryCache.get(key);
}
function addBox(parent, w, h, d, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(boxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let seed = 81732;
function random() {
seed = (1664525 * seed + 1013904223) >>> 0;
return seed / 4294967296;
}
// -------------------------------------------------------------------------
// Custom camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 0.8, -0.35),
radius: 19.2,
theta: 0.67,
phi: 0.92
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.target.set(0, 0.8, -0.35);
orbit.radius = 19.2;
orbit.theta = 0.67;
orbit.phi = 0.92;
updateCamera();
}
let dragging = false;
let dragButton = 0;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
dragging = true;
dragButton = event.button;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
event.preventDefault();
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (event.shiftKey || dragButton === 2) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const amount = orbit.radius * 0.00155;
orbit.target.addScaledVector(right, -dx * amount);
orbit.target.addScaledVector(forward, dy * amount);
orbit.target.x = THREE.MathUtils.clamp(
orbit.target.x, -8, 8
);
orbit.target.z = THREE.MathUtils.clamp(
orbit.target.z, -7, 7
);
} else {
orbit.theta -= dx * 0.005;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005, 0.25, 1.43
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
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("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8,
31
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
updateCamera();
// -------------------------------------------------------------------------
// Stepped ice floe and voxel scenery
// -------------------------------------------------------------------------
const GROUND_Y = 0.21;
addBox(scene, 80, 0.3, 80, materials.ocean, 0, -1.42, 0);
addBox(scene, 16, 1.1, 13, materials.ice, 0, -0.55, -0.4);
addBox(scene, 15.5, 0.22, 12.5, materials.snow, 0, 0.1, -0.4);
// Thin rectangular "cracks" reinforce the stepped, graphic ice treatment.
const crackMaterial = standard(0x7ba5b0);
const cracks = [
[-5.4, 0.214, -3.8, 2.4, 0.018, 0.07, 0.32],
[0.1, 0.214, -4.8, 1.8, 0.018, 0.06, -0.25],
[5.7, 0.214, -0.8, 1.2, 0.018, 0.07, 0.8],
[-1.9, 0.214, 4.2, 2.1, 0.018, 0.07, 0.12]
];
for (const [x, y, z, w, h, d, rotation] of cracks) {
const crack = addBox(scene, w, h, d, crackMaterial, x, y, z);
crack.rotation.y = rotation;
}
// Low voxel snow banks.
for (let i = 0; i < 72; i++) {
const x = (random() - 0.5) * 14.7;
const z = (random() - 0.5) * 11.5;
const nearLarge = Math.hypot(x + 3.6, z + 2.2) < 4.0;
const nearSmall = Math.hypot(x - 3.0, z - 1.6) < 2.15;
if (nearLarge || nearSmall) continue;
const w = 0.18 + random() * 0.62;
const d = 0.18 + random() * 0.62;
const h = 0.04 + random() * 0.13;
const material = random() > 0.45
? materials.snowBright
: materials.snowBlue;
addBox(scene, w, h, d, material, x, GROUND_Y + h / 2, z);
}
function addIceberg(x, z, scale, tint) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
addBox(group, 3.6, 1.2, 2.8, materials.iceDark, 0, -0.3, 0);
addBox(group, 2.8, 1.25, 2.2, materials.ice, -0.15, 0.72, 0);
addBox(group, 1.8, 1.35, 1.7, materials.iceLight, 0.1, 1.78, 0.05);
addBox(group, 0.9, 0.8, 1.0, tint, 0.2, 2.72, -0.02);
addBox(group, 2.9, 0.12, 2.25, materials.snowBright, -0.12, 1.38, 0);
}
addIceberg(-10.8, -9.0, 1.2, materials.snowBright);
addIceberg(10.5, -10.5, 1.45, materials.snow);
addIceberg(-12.5, 4.5, 0.78, materials.snowBright);
addIceberg(12.0, 5.5, 0.9, materials.snow);
// -------------------------------------------------------------------------
// Voxel penguins
// -------------------------------------------------------------------------
function createPenguin(scale = 1, alternate = false) {
const root = new THREE.Group();
const model = new THREE.Group();
model.scale.setScalar(scale);
root.add(model);
const dark = alternate ? materials.penguinAlt : materials.penguin;
// Feet, lower torso, upper torso, and head.
addBox(model, 0.28, 0.12, 0.36, materials.orange, -0.20, 0.06, 0.08);
addBox(model, 0.28, 0.12, 0.36, materials.orange, 0.20, 0.06, 0.08);
addBox(model, 0.70, 0.76, 0.54, dark, 0, 0.44, 0);
addBox(model, 0.74, 0.58, 0.58, dark, 0, 0.94, 0);
addBox(model, 0.62, 0.58, 0.58, dark, 0, 1.43, 0);
// White belly and face.
addBox(model, 0.52, 0.68, 0.08, materials.belly, 0, 0.65, 0.30);
addBox(model, 0.43, 0.34, 0.08, materials.belly, 0, 1.45, 0.31);
// Pixel eyes and beak.
addBox(model, 0.055, 0.065, 0.035, materials.eye, -0.115, 1.50, 0.37);
addBox(model, 0.055, 0.065, 0.035, materials.eye, 0.115, 1.50, 0.37);
addBox(model, 0.22, 0.13, 0.22, materials.orange, 0, 1.34, 0.43);
// Blocky flippers.
const leftFlipper = addBox(
model, 0.18, 0.52, 0.28, dark, -0.43, 0.78, 0.01
);
const rightFlipper = addBox(
model, 0.18, 0.52, 0.28, dark, 0.43, 0.78, 0.01
);
leftFlipper.rotation.z = -0.14;
rightFlipper.rotation.z = 0.14;
root.userData.model = model;
root.userData.phase = random() * Math.PI * 2;
root.userData.yaw = 0;
return root;
}
const allPenguins = [];
const staticPenguins = [];
// Larger background huddle arranged on a pointy hex lattice.
const largeCenter = new THREE.Vector3(-3.6, GROUND_Y, -2.2);
const hexSize = 1.02;
for (let q = -2; q <= 2; q++) {
for (let r = -2; r <= 2; r++) {
const s = -q - r;
if (Math.max(Math.abs(q), Math.abs(r), Math.abs(s)) > 2) continue;
const x = Math.sqrt(3) * hexSize * (q + r / 2);
const z = 1.5 * hexSize * r;
const penguin = createPenguin(0.90, (q + r) % 2 === 0);
penguin.position.set(
largeCenter.x + x,
GROUND_Y,
largeCenter.z + z
);
penguin.userData.home = penguin.position.clone();
penguin.userData.jitter = (random() - 0.5) * 0.22;
scene.add(penguin);
allPenguins.push(penguin);
staticPenguins.push(penguin);
}
}
// -------------------------------------------------------------------------
// Small active huddle
//
// Six rim penguins advance around the center in a shared angular phase.
// Their radial offsets remain between 1.11 and 1.29 units. With 60-degree
// angular separation, the minimum center-to-rim or rim-to-rim distance is
// at least 1.11 units, so birds never intersect or exchange through each
// other while boundary positions change.
// -------------------------------------------------------------------------
const activeCenter = new THREE.Vector3(3.0, GROUND_Y, 1.6);
const activePenguins = [];
const LOCAL_RADIUS = 1.22;
const centerPenguin = createPenguin(0.94, false);
centerPenguin.position.copy(activeCenter);
centerPenguin.userData.isActive = true;
scene.add(centerPenguin);
allPenguins.push(centerPenguin);
activePenguins.push(centerPenguin);
for (let i = 0; i < 6; i++) {
const penguin = createPenguin(0.94, i % 2 === 0);
penguin.userData.isActive = true;
penguin.userData.rimIndex = i;
scene.add(penguin);
allPenguins.push(penguin);
activePenguins.push(penguin);
}
// A segmented square-pixel ring identifies the observed huddle.
const focusMaterial = new THREE.MeshBasicMaterial({
color: 0x57d8cf,
transparent: true,
opacity: 0.62
});
const focusRing = new THREE.Group();
focusRing.position.set(activeCenter.x, GROUND_Y + 0.025, activeCenter.z);
scene.add(focusRing);
for (let i = 0; i < 24; i++) {
const angle = (i / 24) * Math.PI * 2;
const segment = addBox(
focusRing,
0.10,
0.04,
0.31,
focusMaterial,
Math.sin(angle) * 1.96,
0,
Math.cos(angle) * 1.96
);
segment.rotation.y = angle;
segment.castShadow = false;
segment.receiveShadow = false;
}
// -------------------------------------------------------------------------
// Local exposure badges
//
// The number of illuminated square pips is exactly the number of nearby
// penguins. Color is also selected exclusively from that local count.
// No global position, weather, or environmental state contributes.
// -------------------------------------------------------------------------
function createExposureBadge() {
const group = new THREE.Group();
const panel = addBox(
group,
0.79,
0.19,
0.04,
materials.badgePanel,
0, 0, 0
);
panel.castShadow = false;
panel.receiveShadow = false;
const litMaterial = new THREE.MeshBasicMaterial({ color: 0xffc65c });
const pips = [];
for (let i = 0; i < 6; i++) {
const pip = addBox(
group,
0.066,
0.066,
0.035,
materials.badgeOff,
-0.25 + i * 0.10,
0,
0.037
);
pip.castShadow = false;
pip.receiveShadow = false;
pips.push({ mesh: pip, litMaterial });
}
return { group, pips, litMaterial };
}
for (const penguin of activePenguins) {
penguin.userData.badge = createExposureBadge();
penguin.userData.neighborCount = 0;
scene.add(penguin.userData.badge.group);
}
function updateExposureBadges() {
let exposed = 0;
let enclosed = 0;
for (const penguin of activePenguins) {
let count = 0;
for (const other of allPenguins) {
if (other === penguin) continue;
const dx = other.position.x - penguin.position.x;
const dz = other.position.z - penguin.position.z;
if (dx * dx + dz * dz <= LOCAL_RADIUS * LOCAL_RADIUS) {
count++;
}
}
penguin.userData.neighborCount = count;
if (count <= 2) exposed++;
if (count >= 4) enclosed++;
const color =
count <= 2 ? 0xffc65c :
count === 3 ? 0xf4f6ed :
0x58d8cf;
const badge = penguin.userData.badge;
badge.litMaterial.color.setHex(color);
for (let i = 0; i < badge.pips.length; i++) {
badge.pips[i].mesh.material =
i < count ? badge.litMaterial : materials.badgeOff;
}
badge.group.position.set(
penguin.position.x,
penguin.position.y + 2.04,
penguin.position.z
);
// Billboard the indicator so its symbolic count stays readable.
badge.group.lookAt(camera.position);
}
document.getElementById("exposureReadout").textContent =
`${exposed} / ${enclosed}`;
}
// -------------------------------------------------------------------------
// Falling voxel snow
// -------------------------------------------------------------------------
const flakeCount = 170;
const snowMesh = new THREE.InstancedMesh(
boxGeometry(0.045, 0.045, 0.045),
materials.snowflake,
flakeCount
);
snowMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
snowMesh.frustumCulled = false;
scene.add(snowMesh);
const flakes = [];
for (let i = 0; i < flakeCount; i++) {
flakes.push({
x: (random() - 0.5) * 34,
y: random() * 17,
z: (random() - 0.5) * 29,
speed: 0.65 + random() * 1.25,
drift: 0.08 + random() * 0.16,
size: 0.55 + random() * 1.2
});
}
const dummy = new THREE.Object3D();
function updateSnow(delta, elapsed) {
for (let i = 0; i < flakes.length; i++) {
const flake = flakes[i];
flake.y -= flake.speed * delta;
flake.x += Math.sin(elapsed * 0.5 + i) * flake.drift * delta;
if (flake.y < -1.1) {
flake.y = 14 + random() * 5;
flake.x = (random() - 0.5) * 34;
flake.z = (random() - 0.5) * 29;
}
dummy.position.set(flake.x, flake.y, flake.z);
dummy.rotation.set(0, elapsed * 0.25 + i, 0);
dummy.scale.setScalar(flake.size);
dummy.updateMatrix();
snowMesh.setMatrixAt(i, dummy.matrix);
}
snowMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Animation helpers
// -------------------------------------------------------------------------
function dampAngle(current, target, smoothing, delta) {
const difference = Math.atan2(
Math.sin(target - current),
Math.cos(target - current)
);
return current + difference * (1 - Math.exp(-smoothing * delta));
}
function updateStaticHuddle(elapsed, delta) {
for (const penguin of staticPenguins) {
const home = penguin.userData.home;
const targetYaw =
Math.atan2(
largeCenter.x - home.x,
largeCenter.z - home.z
) + penguin.userData.jitter;
penguin.userData.yaw = dampAngle(
penguin.userData.yaw,
targetYaw,
3.0,
delta
);
penguin.rotation.y = penguin.userData.yaw;
const phase = penguin.userData.phase;
penguin.userData.model.position.y =
Math.sin(elapsed * 1.8 + phase) * 0.018;
}
}
function updateActiveHuddle(elapsed, delta) {
// Every 16 seconds, the whole rim advances one boundary slot. Smooth
// interpolation makes the rearrangement gradual and non-crossing.
const cycleLength = 16;
const cycleIndex = Math.floor(elapsed / cycleLength);
const cycleProgress = (elapsed % cycleLength) / cycleLength;
const eased = cycleProgress * cycleProgress * (3 - 2 * cycleProgress);
const phase =
cycleIndex * (Math.PI / 3) +
eased * (Math.PI / 3) +
Math.sin(elapsed * 0.23) * 0.025;
centerPenguin.position.copy(activeCenter);
centerPenguin.userData.yaw = dampAngle(
centerPenguin.userData.yaw,
0.24 + Math.sin(elapsed * 0.18) * 0.15,
3.0,
delta
);
centerPenguin.rotation.y = centerPenguin.userData.yaw;
centerPenguin.userData.model.position.y =
Math.sin(elapsed * 1.9) * 0.022;
for (let i = 0; i < activePenguins.length - 1; i++) {
const penguin = activePenguins[i + 1];
const angle = phase + i * (Math.PI / 3);
// Independent radial breathing changes local contact while keeping
// every pair above the safe 1.11-unit minimum spacing.
const radius =
1.20 + 0.09 * Math.sin(elapsed * 0.48 + i * 1.73);
penguin.position.set(
activeCenter.x + Math.sin(angle) * radius,
GROUND_Y,
activeCenter.z + Math.cos(angle) * radius
);
const targetYaw = Math.atan2(
activeCenter.x - penguin.position.x,
activeCenter.z - penguin.position.z
);
penguin.userData.yaw = dampAngle(
penguin.userData.yaw,
targetYaw,
3.2,
delta
);
penguin.rotation.y = penguin.userData.yaw;
penguin.userData.model.position.y =
Math.sin(elapsed * 2.1 + penguin.userData.phase) * 0.028;
penguin.userData.model.rotation.z =
Math.sin(elapsed * 0.9 + i) * 0.018;
}
focusMaterial.opacity = 0.48 + Math.sin(elapsed * 1.7) * 0.13;
focusRing.rotation.y = Math.sin(elapsed * 0.18) * 0.025;
}
// -------------------------------------------------------------------------
// Resize and main loop
// -------------------------------------------------------------------------
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
document.getElementById("birdCount").textContent =
String(activePenguins.length);
document.getElementById("radiusReadout").textContent =
`${LOCAL_RADIUS.toFixed(2)} units`;
let elapsed = 0;
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
elapsed += delta;
updateStaticHuddle(elapsed, delta);
updateActiveHuddle(elapsed, delta);
updateExposureBadges();
updateSnow(delta, elapsed);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```