Prompt specification
Build or customize the Three.js 3D scene "Evening birds above a field" 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: Evening birds above a field
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/evening-birds-above-a-field-x0687
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Build a farm-edge scene with trees, rooftops, and a modest flock; agents turn around a bounded airspace while maintaining readable spacing and coherent overall movement.
Working Three.js Starter (evening-birds-above-a-field-x0687):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Windward Acres — Bounded Flock</title>
<style>
:root {
color-scheme: light;
--ink: #24352d;
--muted: #65746c;
--paper: rgba(248, 246, 235, 0.88);
--line: rgba(36, 53, 45, 0.14);
--accent: #bd6446;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8dd;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 14px 40px rgba(36, 53, 45, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 20px 22px 18px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(1.65rem, 3vw, 2.35rem);
font-weight: 500;
line-height: 1;
letter-spacing: -0.025em;
}
#intro p:last-of-type {
margin: 11px 0 15px;
color: var(--muted);
font-size: 0.84rem;
line-height: 1.55;
}
.tags {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.tag {
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.38);
color: #526159;
font-size: 0.66rem;
font-weight: 700;
letter-spacing: 0.04em;
}
#controls {
right: 24px;
bottom: 24px;
display: flex;
gap: 7px;
padding: 8px;
border-radius: 15px;
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid transparent;
border-radius: 10px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 0.73rem;
font-weight: 750;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(36, 53, 45, 0.08);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(189, 100, 70, 0.28);
background: rgba(189, 100, 70, 0.12);
color: #8c432f;
}
#hint {
position: fixed;
z-index: 4;
left: 26px;
bottom: 25px;
color: rgba(36, 53, 45, 0.7);
font-size: 0.69rem;
font-weight: 650;
letter-spacing: 0.03em;
pointer-events: none;
text-shadow: 0 1px 8px rgba(255, 255, 255, 0.65);
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #dce8dd;
color: var(--ink);
font-size: 0.76rem;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 500ms ease, visibility 500ms ease;
}
#loading.hidden {
visibility: hidden;
opacity: 0;
}
@media (max-width: 650px) {
#intro {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 16px 17px;
}
#intro p:last-of-type {
display: none;
}
.tags {
margin-top: 12px;
}
#controls {
right: 14px;
bottom: 14px;
}
button {
padding: 0 10px;
font-size: 0.68rem;
}
#hint {
left: 16px;
bottom: 20px;
max-width: 42vw;
line-height: 1.45;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive stylized farm scene with a circling flock"></main>
<section id="intro" class="panel">
<p class="eyebrow">A small pastoral systems study</p>
<h1>Windward Acres</h1>
<p>
Eighteen birds circle the farm in a soft oval, using alignment, cohesion,
and separation to preserve both formation and breathing room.
</p>
<div class="tags">
<span class="tag">18 agents</span>
<span class="tag">Bounded airspace</span>
<span class="tag">Low-poly farm</span>
</div>
</section>
<nav id="controls" class="panel" aria-label="Scene controls">
<button id="pause" type="button">Pause</button>
<button id="boundary" class="active" type="button" aria-pressed="true">Boundary</button>
<button id="reset" type="button">Reset view</button>
</nav>
<div id="hint">Drag to orbit · Wheel to zoom · Space to pause</div>
<div id="loading">Preparing the farm…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const mount = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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",
"A stylized farm with a flock of birds circling above it"
);
mount.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce8dd);
scene.fog = new THREE.Fog(0xdce8dd, 58, 118);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
180
);
const cameraTarget = new THREE.Vector3(0, 3.2, 0);
let cameraTheta = 0.73;
let cameraPhi = 0.96;
let cameraRadius = 52;
const hemisphere = new THREE.HemisphereLight(0xf7f4df, 0x6e7655, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-28, 42, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -45;
sun.shadow.camera.right = 45;
sun.shadow.camera.top = 40;
sun.shadow.camera.bottom = -40;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d4d1, 0.55);
fill.position.set(30, 18, -24);
scene.add(fill);
// -------------------------------------------------------------------------
// Reusable materials and construction helpers
// -------------------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
flatShading: true,
...options
});
}
const materials = {
grass: material(0x91a96d),
outerGrass: material(0x9eb17b),
earth: material(0x96704e),
path: material(0xc9b78f),
barn: material(0xa94f3d),
barnDark: material(0x743b35),
roof: material(0x4f5a53, { side: THREE.DoubleSide }),
roofRed: material(0x713e37, { side: THREE.DoubleSide }),
cream: material(0xe9dfc4),
darkWood: material(0x674b38),
wood: material(0xa27b50),
trunk: material(0x76543a),
leaf: material(0x66875b),
leafLight: material(0x79955f),
leafDark: material(0x4f7355),
hay: material(0xd5a64c),
hayBand: material(0x8e6632),
wheat: material(0xd6b756),
vegetable: material(0x668d4f),
soil: material(0x98724d),
window: material(0x7faaa8, { roughness: 0.3, metalness: 0.08 }),
metal: material(0xa9aaa0, { roughness: 0.48, metalness: 0.38 }),
eye: material(0x242521),
cloud: material(0xf1f1df, { roughness: 1 })
};
function addBox(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
mat,
segments = 12
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeGableRoof(width, depth, height, mat) {
const w = width / 2;
const d = depth / 2;
const vertices = new Float32Array([
-w, 0, -d,
w, 0, -d,
0, height, -d,
-w, 0, d,
w, 0, d,
0, height, d
]);
const indices = [
0, 1, 2,
3, 5, 4,
0, 3, 5, 0, 5, 2,
1, 2, 5, 1, 5, 4,
0, 2, 1,
3, 4, 5
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// -------------------------------------------------------------------------
// Ground and landscape
// -------------------------------------------------------------------------
const outerGround = new THREE.Mesh(
new THREE.PlaneGeometry(180, 180),
materials.outerGrass
);
outerGround.rotation.x = -Math.PI / 2;
outerGround.position.y = -1.08;
outerGround.receiveShadow = true;
scene.add(outerGround);
const groundBase = new THREE.Mesh(
new THREE.BoxGeometry(72, 1, 48),
[
materials.earth,
materials.earth,
materials.grass,
materials.earth,
materials.earth,
materials.earth
]
);
groundBase.position.y = -0.5;
groundBase.receiveShadow = true;
scene.add(groundBase);
const path = addBox(
scene,
[3.3, 0.055, 28],
[-1.8, 0.035, 1.5],
materials.path,
false
);
path.rotation.y = -0.045;
const sidePath = addBox(
scene,
[19, 0.055, 2.6],
[8.7, 0.04, -2.3],
materials.path,
false
);
sidePath.rotation.y = -0.18;
// -------------------------------------------------------------------------
// Barn
// -------------------------------------------------------------------------
const barn = new THREE.Group();
barn.position.set(-12, 0, -5.5);
scene.add(barn);
addBox(barn, [8.2, 4.6, 6.2], [0, 2.3, 0], materials.barn);
addBox(barn, [8.45, 0.18, 6.45], [0, 0.12, 0], materials.barnDark);
const barnRoof = makeGableRoof(8.9, 6.8, 2.65, materials.roof);
barnRoof.position.y = 4.6;
barn.add(barnRoof);
addBox(barn, [3.25, 3.15, 0.16], [0, 1.58, 3.16], materials.barnDark);
addBox(barn, [3.55, 0.16, 0.16], [0, 3.18, 3.25], materials.cream);
addBox(barn, [0.16, 3.22, 0.16], [0, 1.58, 3.27], materials.cream);
const braceA = addBox(
barn,
[0.13, 3.45, 0.13],
[0, 1.58, 3.29],
materials.cream
);
braceA.rotation.z = 0.64;
const braceB = addBox(
barn,
[0.13, 3.45, 0.13],
[0, 1.58, 3.29],
materials.cream
);
braceB.rotation.z = -0.64;
addBox(barn, [0.9, 0.9, 0.14], [0, 3.8, 3.22], materials.window);
addBox(barn, [1.05, 0.12, 0.16], [0, 3.8, 3.3], materials.cream);
addBox(barn, [0.12, 1.05, 0.16], [0, 3.8, 3.3], materials.cream);
// -------------------------------------------------------------------------
// Farmhouse
// -------------------------------------------------------------------------
const house = new THREE.Group();
house.position.set(9.2, 0, -9.2);
scene.add(house);
addBox(house, [7, 3.7, 5.4], [0, 1.85, 0], materials.cream);
addBox(house, [7.2, 0.18, 5.6], [0, 0.12, 0], materials.darkWood);
const houseRoof = makeGableRoof(7.7, 6, 2.45, materials.roofRed);
houseRoof.position.y = 3.7;
house.add(houseRoof);
addBox(house, [1.05, 2.15, 0.16], [0.65, 1.08, 2.78], materials.barnDark);
addBox(house, [1.35, 0.14, 0.18], [0.65, 2.2, 2.87], materials.cream);
addBox(house, [1.35, 1.25, 0.16], [-2.05, 2.05, 2.78], materials.window);
addBox(house, [1.55, 0.13, 0.19], [-2.05, 2.05, 2.87], materials.cream);
addBox(house, [0.13, 1.4, 0.19], [-2.05, 2.05, 2.87], materials.cream);
addBox(house, [0.13, 1.35, 1.4], [3.57, 2.05, 0.45], materials.window);
addBox(house, [0.18, 1.52, 0.13], [3.64, 2.05, 0.45], materials.cream);
addBox(house, [0.18, 0.13, 1.52], [3.64, 2.05, 0.45], materials.cream);
addBox(house, [0.78, 2.2, 0.82], [-1.85, 5.35, -0.8], materials.barnDark);
addBox(house, [0.98, 0.18, 1.02], [-1.85, 6.5, -0.8], materials.roof);
// -------------------------------------------------------------------------
// Silo
// -------------------------------------------------------------------------
const silo = new THREE.Group();
silo.position.set(-2.8, 0, -13.3);
scene.add(silo);
addCylinder(silo, 2.15, 2.25, 6.7, [0, 3.35, 0], materials.metal, 16);
addCylinder(silo, 0, 2.35, 2.1, [0, 7.75, 0], materials.roof, 16);
for (let i = 0; i < 3; i++) {
const band = new THREE.Mesh(
new THREE.TorusGeometry(2.2, 0.055, 5, 20),
materials.roof
);
band.rotation.x = Math.PI / 2;
band.position.y = 1.45 + i * 2.1;
silo.add(band);
}
// -------------------------------------------------------------------------
// Fields, crops, hay, and fencing
// -------------------------------------------------------------------------
const field = new THREE.Group();
field.position.set(14, 0, 7.1);
scene.add(field);
addBox(field, [14.8, 0.07, 9.5], [0, 0.045, 0], materials.soil, false);
const rowCount = 12;
const columnCount = 18;
const wheatGeometry = new THREE.ConeGeometry(0.075, 0.58, 5);
const wheat = new THREE.InstancedMesh(
wheatGeometry,
materials.wheat,
rowCount * columnCount
);
const dummy = new THREE.Object3D();
let wheatIndex = 0;
for (let row = 0; row < rowCount; row++) {
for (let column = 0; column < columnCount; column++) {
dummy.position.set(
-6.45 + row * 1.18,
0.34,
-4.15 + column * 0.48
);
dummy.rotation.y = (row + column) * 0.37;
const scale = 0.85 + ((row * 7 + column * 11) % 5) * 0.055;
dummy.scale.set(scale, scale, scale);
dummy.updateMatrix();
wheat.setMatrixAt(wheatIndex++, dummy.matrix);
}
}
wheat.castShadow = true;
wheat.receiveShadow = true;
field.add(wheat);
const vegetableField = new THREE.Group();
vegetableField.position.set(-23.5, 0, 3.4);
scene.add(vegetableField);
addBox(
vegetableField,
[10.3, 0.06, 11.8],
[0, 0.04, 0],
materials.soil,
false
);
const vegetableGeometry = new THREE.IcosahedronGeometry(0.24, 0);
const vegetables = new THREE.InstancedMesh(
vegetableGeometry,
materials.vegetable,
90
);
let vegetableIndex = 0;
for (let row = 0; row < 9; row++) {
for (let column = 0; column < 10; column++) {
dummy.position.set(
-4.1 + row * 1.02,
0.24,
-4.8 + column * 1.04
);
dummy.rotation.set(
(row % 3) * 0.2,
(row + column) * 0.71,
(column % 4) * 0.13
);
dummy.scale.setScalar(0.9 + ((row + column) % 4) * 0.08);
dummy.updateMatrix();
vegetables.setMatrixAt(vegetableIndex++, dummy.matrix);
}
}
vegetables.castShadow = true;
vegetables.receiveShadow = true;
vegetableField.add(vegetables);
function makeHayBale(x, z, rotation = 0) {
const bale = new THREE.Group();
bale.position.set(x, 0.72, z);
bale.rotation.y = rotation;
const body = addCylinder(
bale,
0.78,
0.78,
1.3,
[0, 0, 0],
materials.hay,
12
);
body.rotation.z = Math.PI / 2;
for (const offset of [-0.42, 0.42]) {
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.79, 0.045, 5, 16),
materials.hayBand
);
band.rotation.y = Math.PI / 2;
band.position.x = offset;
bale.add(band);
}
scene.add(bale);
}
makeHayBale(-18, 9.7, -0.15);
makeHayBale(-15.9, 10.8, 0.08);
makeHayBale(-18.8, 12.1, -0.03);
makeHayBale(-25.5, -11.8, 0.28);
function addFenceSegment(x1, z1, x2, z2) {
const group = new THREE.Group();
const length = Math.hypot(x2 - x1, z2 - z1);
const posts = Math.max(2, Math.ceil(length / 3));
const angle = -Math.atan2(z2 - z1, x2 - x1);
for (let i = 0; i <= posts; i++) {
const t = i / posts;
addBox(
group,
[0.22, 1.5, 0.22],
[
THREE.MathUtils.lerp(x1, x2, t),
0.75,
THREE.MathUtils.lerp(z1, z2, t)
],
materials.wood
);
}
for (const height of [0.58, 1.13]) {
const rail = addBox(
group,
[length, 0.13, 0.13],
[(x1 + x2) / 2, height, (z1 + z2) / 2],
materials.wood
);
rail.rotation.y = angle;
}
scene.add(group);
}
addFenceSegment(-29, 15.5, -5.2, 15.5);
addFenceSegment(-5.2, 15.5, -5.2, 21.3);
addFenceSegment(20, 15.5, 30, 15.5);
addFenceSegment(-29, -15.7, -17.5, -15.7);
const trough = new THREE.Group();
trough.position.set(3.2, 0, 14.2);
scene.add(trough);
addBox(trough, [4.2, 0.7, 1.35], [0, 0.36, 0], materials.darkWood);
addBox(trough, [3.65, 0.12, 0.92], [0, 0.73, 0], materials.window, false);
addBox(trough, [0.38, 0.75, 0.38], [-1.55, 0.38, 0], materials.wood);
addBox(trough, [0.38, 0.75, 0.38], [1.55, 0.38, 0], materials.wood);
// -------------------------------------------------------------------------
// Stylized trees around the farm edge
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1, variant = 0) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
addCylinder(
tree,
0.28,
0.43,
3.1,
[0, 1.55, 0],
materials.trunk,
7
);
const crownMaterial = variant % 3 === 0
? materials.leafLight
: variant % 3 === 1
? materials.leaf
: materials.leafDark;
const crownA = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.7, 0),
crownMaterial
);
crownA.position.set(0, 3.65, 0);
crownA.scale.set(1.15, 1.05, 1);
crownA.rotation.y = variant * 0.61;
crownA.castShadow = true;
crownA.receiveShadow = true;
tree.add(crownA);
const crownB = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.25, 0),
crownMaterial
);
crownB.position.set(0.95, 3.2, 0.18);
crownB.castShadow = true;
tree.add(crownB);
const crownC = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.18, 0),
materials.leafLight
);
crownC.position.set(-0.82, 3.45, -0.12);
crownC.castShadow = true;
tree.add(crownC);
scene.add(tree);
}
[
[-31, -13, 1.15, 0],
[-28, 6.5, 0.95, 1],
[-30, 17.5, 1.18, 2],
[-21, 19.2, 0.92, 0],
[-11, 19.5, 1.02, 1],
[28, -14, 1.08, 2],
[31, 2, 1.12, 0],
[29, 13.7, 0.94, 1],
[23.5, 19, 1.16, 2],
[12, 19.1, 0.92, 0],
[1.5, 19.5, 1.08, 1],
[-29.5, -18.2, 0.9, 2]
].forEach((tree) => addTree(...tree));
// -------------------------------------------------------------------------
// Soft clouds
// -------------------------------------------------------------------------
const clouds = [];
function addCloud(x, y, z, scale) {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
const pieces = [
[-1.25, 0, 0, 1.0],
[-0.3, 0.3, 0.05, 1.2],
[0.75, 0.1, 0, 0.95],
[1.55, -0.05, 0.08, 0.65]
];
for (const [px, py, pz, s] of pieces) {
const puff = new THREE.Mesh(
new THREE.DodecahedronGeometry(s, 0),
materials.cloud
);
puff.position.set(px, py, pz);
cloud.add(puff);
}
scene.add(cloud);
clouds.push(cloud);
}
addCloud(-25, 24, -30, 1.1);
addCloud(18, 27, -37, 1.45);
addCloud(36, 22, -18, 0.95);
// -------------------------------------------------------------------------
// Bounded flock
// -------------------------------------------------------------------------
const bounds = {
radiusX: 26,
radiusZ: 15,
minimumY: 5.2,
maximumY: 13.6
};
const boundaryGroup = new THREE.Group();
const boundaryPoints = [];
for (let i = 0; i < 160; i++) {
const angle = (i / 160) * Math.PI * 2;
boundaryPoints.push(
new THREE.Vector3(
Math.cos(angle) * bounds.radiusX,
8.7,
Math.sin(angle) * bounds.radiusZ
)
);
}
const boundaryGeometry = new THREE.BufferGeometry().setFromPoints(
boundaryPoints
);
const boundaryMaterial = new THREE.LineDashedMaterial({
color: 0xf7f1d5,
dashSize: 0.8,
gapSize: 0.65,
transparent: true,
opacity: 0.35,
depthWrite: false
});
const boundaryLine = new THREE.LineLoop(
boundaryGeometry,
boundaryMaterial
);
boundaryLine.computeLineDistances();
boundaryGroup.add(boundaryLine);
scene.add(boundaryGroup);
const bodyGeometry = new THREE.SphereGeometry(1, 9, 6);
const headGeometry = new THREE.SphereGeometry(1, 8, 6);
const beakGeometry = new THREE.ConeGeometry(0.08, 0.28, 5);
const eyeGeometry = new THREE.SphereGeometry(0.035, 6, 4);
function makeWingGeometry(sign) {
const vertices = new Float32Array([
0, 0, 0.08,
sign * 1.25, 0, -0.28,
sign * 0.52, 0, -0.64,
0, 0, 0.08,
sign * 0.52, 0, -0.64,
sign * 0.08, 0, -0.46
]);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.computeVertexNormals();
return geometry;
}
const rightWingGeometry = makeWingGeometry(1);
const leftWingGeometry = makeWingGeometry(-1);
const tailGeometry = new THREE.BufferGeometry();
tailGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
0, 0, -0.42,
-0.28, 0, -0.84,
0.28, 0, -0.84
], 3)
);
tailGeometry.computeVertexNormals();
const birdPalette = [
0xf1ead6,
0xddd6c2,
0xc99b78,
0x71847b,
0xe0b36f
];
function createBird(index) {
const group = new THREE.Group();
const visual = new THREE.Group();
group.add(visual);
const bodyColor = birdPalette[index % birdPalette.length];
const bodyMaterial = material(bodyColor);
const wingMaterial = material(
new THREE.Color(bodyColor).multiplyScalar(0.72)
);
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.scale.set(0.23, 0.17, 0.54);
body.castShadow = true;
visual.add(body);
const head = new THREE.Mesh(headGeometry, bodyMaterial);
head.position.set(0, 0.07, 0.49);
head.scale.setScalar(0.19);
head.castShadow = true;
visual.add(head);
const beak = new THREE.Mesh(beakGeometry, materials.hayBand);
beak.position.set(0, 0.025, 0.76);
beak.rotation.x = Math.PI / 2;
visual.add(beak);
for (const sign of [-1, 1]) {
const eye = new THREE.Mesh(eyeGeometry, materials.eye);
eye.position.set(sign * 0.145, 0.12, 0.56);
visual.add(eye);
}
const rightPivot = new THREE.Group();
rightPivot.position.set(0.13, 0.01, 0);
const rightWing = new THREE.Mesh(
rightWingGeometry,
new THREE.MeshStandardMaterial({
color: wingMaterial.color,
roughness: 0.9,
flatShading: true,
side: THREE.DoubleSide
})
);
rightWing.castShadow = true;
rightPivot.add(rightWing);
visual.add(rightPivot);
const leftPivot = new THREE.Group();
leftPivot.position.set(-0.13, 0.01, 0);
const leftWing = new THREE.Mesh(
leftWingGeometry,
rightWing.material
);
leftWing.castShadow = true;
leftPivot.add(leftWing);
visual.add(leftPivot);
const tail = new THREE.Mesh(
tailGeometry,
new THREE.MeshStandardMaterial({
color: wingMaterial.color,
roughness: 0.9,
flatShading: true,
side: THREE.DoubleSide
})
);
visual.add(tail);
const scale = 0.77 + ((index * 13) % 7) * 0.035;
visual.scale.setScalar(scale);
const angle = (index / 18) * Math.PI * 2;
const position = new THREE.Vector3(
Math.cos(angle) * bounds.radiusX * 0.62,
8.7 + Math.sin(angle * 2.3) * 1.15,
Math.sin(angle) * bounds.radiusZ * 0.62
);
const velocity = new THREE.Vector3(
-Math.sin(angle),
Math.sin(index * 1.7) * 0.08,
Math.cos(angle)
)
.normalize()
.multiplyScalar(2.05 + (index % 4) * 0.11);
group.position.copy(position);
scene.add(group);
return {
group,
visual,
rightPivot,
leftPivot,
position,
velocity,
phase: index * 0.71,
size: scale
};
}
const birds = Array.from({ length: 18 }, (_, index) =>
createBird(index)
);
const forward = new THREE.Vector3(0, 0, 1);
let simulationTime = 0;
let paused = false;
function updateFlock(dt) {
simulationTime += dt;
for (const bird of birds) {
const separation = new THREE.Vector3();
const alignment = new THREE.Vector3();
const cohesion = new THREE.Vector3();
let neighborCount = 0;
for (const other of birds) {
if (other === bird) continue;
const distance = bird.position.distanceTo(other.position);
if (distance < 8.2) {
alignment.add(other.velocity);
cohesion.add(other.position);
neighborCount++;
if (distance < 2.55) {
const away = bird.position.clone().sub(other.position);
if (away.lengthSq() > 0.0001) {
away.normalize().multiplyScalar((2.55 - distance) / 2.55);
separation.add(away);
}
}
}
}
const acceleration = new THREE.Vector3();
if (separation.lengthSq() > 0) {
acceleration.addScaledVector(separation.normalize(), 2.85);
}
if (neighborCount > 0) {
alignment.divideScalar(neighborCount).normalize();
cohesion
.divideScalar(neighborCount)
.sub(bird.position)
.normalize();
acceleration.addScaledVector(alignment, 0.82);
acceleration.addScaledVector(cohesion, 0.20);
}
// A soft elliptical radius keeps the flock inside readable airspace.
const ellipseX = bird.position.x / bounds.radiusX;
const ellipseZ = bird.position.z / bounds.radiusZ;
const radialDistance = Math.hypot(ellipseX, ellipseZ);
const radial = new THREE.Vector3(ellipseX, 0, ellipseZ);
if (radial.lengthSq() > 0.0001) {
radial.normalize();
acceleration.addScaledVector(
radial,
(radialDistance - 0.61) * 3.15
);
}
// A shared tangential direction creates coherent farm-wide circulation.
const angle = Math.atan2(ellipseZ, ellipseX);
const tangent = new THREE.Vector3(
-bounds.radiusX * Math.sin(angle),
0,
bounds.radiusZ * Math.cos(angle)
).normalize();
acceleration.addScaledVector(tangent, 0.72);
// Gentle vertical correction and individual variation.
acceleration.y += (8.7 - bird.position.y) * 0.38;
acceleration.x +=
Math.sin(simulationTime * 0.63 + bird.phase) * 0.09;
acceleration.z +=
Math.cos(simulationTime * 0.57 + bird.phase * 1.2) * 0.09;
if (acceleration.length() > 4.1) {
acceleration.setLength(4.1);
}
bird.velocity.addScaledVector(acceleration, dt);
const speed = bird.velocity.length();
if (speed > 2.8) bird.velocity.setLength(2.8);
if (speed < 1.55) bird.velocity.setLength(1.55);
bird.position.addScaledVector(bird.velocity, dt);
// Hard limits remain as a final safeguard.
const hardX = bounds.radiusX - 0.8;
const hardZ = bounds.radiusZ - 0.8;
if (Math.abs(bird.position.x) > hardX) {
bird.position.x = THREE.MathUtils.clamp(
bird.position.x,
-hardX,
hardX
);
bird.velocity.x *= -0.42;
}
if (Math.abs(bird.position.z) > hardZ) {
bird.position.z = THREE.MathUtils.clamp(
bird.position.z,
-hardZ,
hardZ
);
bird.velocity.z *= -0.42;
}
bird.position.y = THREE.MathUtils.clamp(
bird.position.y,
bounds.minimumY,
bounds.maximumY
);
}
// A final minimum-distance pass prevents visual overlap.
for (let i = 0; i < birds.length; i++) {
for (let j = i + 1; j < birds.length; j++) {
const first = birds[i];
const second = birds[j];
const difference = first.position.clone().sub(second.position);
const distance = difference.length();
if (distance < 1.35 && distance > 0.0001) {
difference.divideScalar(distance);
const correction = (1.35 - distance) * 0.5;
first.position.addScaledVector(difference, correction);
second.position.addScaledVector(difference, -correction);
}
}
}
for (const bird of birds) {
bird.group.position.copy(bird.position);
const targetQuaternion = new THREE.Quaternion().setFromUnitVectors(
forward,
bird.velocity.clone().normalize()
);
bird.group.quaternion.slerp(
targetQuaternion,
1 - Math.exp(-dt * 8)
);
const flap = Math.sin(simulationTime * 9.2 + bird.phase);
bird.rightPivot.rotation.z = flap * 0.48;
bird.leftPivot.rotation.z = -flap * 0.48;
const bank = THREE.MathUtils.clamp(
-bird.velocity.x * 0.045,
-0.25,
0.25
);
bird.visual.rotation.z = THREE.MathUtils.lerp(
bird.visual.rotation.z,
bank,
1 - Math.exp(-dt * 5)
);
}
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, keyboard shortcuts, and reset
// -------------------------------------------------------------------------
const pauseButton = document.querySelector("#pause");
const boundaryButton = document.querySelector("#boundary");
const resetButton = document.querySelector("#reset");
function setPaused(value) {
paused = value;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.classList.toggle("active", paused);
pauseButton.setAttribute("aria-pressed", String(paused));
}
function toggleBoundary() {
boundaryGroup.visible = !boundaryGroup.visible;
boundaryButton.classList.toggle("active", boundaryGroup.visible);
boundaryButton.setAttribute(
"aria-pressed",
String(boundaryGroup.visible)
);
}
function resetView() {
cameraTheta = 0.73;
cameraPhi = 0.96;
cameraRadius = 52;
}
pauseButton.addEventListener("click", () => setPaused(!paused));
boundaryButton.addEventListener("click", toggleBoundary);
resetButton.addEventListener("click", resetView);
let pointerActive = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerActive = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.42,
1.38
);
});
function releasePointer(event) {
pointerActive = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.035,
27,
76
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setPaused(!paused);
} else if (event.key.toLowerCase() === "b") {
toggleBoundary();
} else if (event.key.toLowerCase() === "r") {
resetView();
}
});
function updateCamera(dt) {
const desiredPosition = new THREE.Vector3(
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.position.lerp(
desiredPosition,
1 - Math.exp(-dt * 8)
);
camera.lookAt(cameraTarget);
}
// -------------------------------------------------------------------------
// Resize and animation loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.04);
if (!paused) {
updateFlock(dt);
}
for (let i = 0; i < clouds.length; i++) {
const cloud = clouds[i];
cloud.position.x += dt * (0.11 + i * 0.018);
if (cloud.position.x > 52) cloud.position.x = -52;
}
updateCamera(dt);
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
}
}
animate();
</script>
</body>
</html>
```