Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 25x 25x 25x 25x 9x 9x 9x 9x 9x 9x 250x 250x 250x 250x 250x 250x 250x 9x 1934x 1934x 1934x 1934x 1865x 1865x 1863x 1863x 1863x 1865x 1865x 1865x 1865x 1865x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 2980800x 2906280x 2906280x 2906280x 2906280x 2906280x 1531266x 1531266x 1531266x 1531266x 1531266x 1531266x 1531266x 1531266x 58366x 58366x 58366x 58366x 74520x 74520x 74520x 74520x 69120x 69120x 69120x 69120x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 1863x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 74520x 223560x 223560x 214110x 200034x 223560x | import { clamp } from '@be-music/player-skin-sdk';
import { hash01 } from '@be-music/player-skin-sdk';
/**
* A 3D school of boids (Reynolds' separation / alignment / cohesion) for the Synesthesia backdrop: fish of light that
* swim after a slowly wandering lead point inside a box, tighten on the beat, and scatter on a key press. State lives
* in flat typed arrays and is advanced in place by {@link stepFlock}; everything else is deterministic for the seed.
*/
export interface FlockBounds {
minX: number;
maxX: number;
minY: number;
maxY: number;
minZ: number;
maxZ: number;
}
export interface Flock {
count: number;
/** x, y, z per boid. */
position: Float32Array;
/** vx, vy, vz per boid (units / s). */
velocity: Float32Array;
bounds: FlockBounds;
}
export interface FlockForces {
/** Seconds on the scene clock — drives the wandering lead point. */
seconds: number;
/** 0..1 extra cohesion (the beat pulls the school tight). */
gather?: number;
/** 0..1 burst of separation and speed (a key press scatters the school). */
scatter?: number;
}
const SEPARATION_RADIUS = 58;
const NEIGHBOUR_RADIUS = 150;
const MIN_SPEED = 90;
const MAX_SPEED = 260;
export function createFlock(seed: number, count: number, bounds: FlockBounds): Flock {
const position = new Float32Array(count * 3);
const velocity = new Float32Array(count * 3);
const cx = (bounds.minX + bounds.maxX) / 2;
const cy = (bounds.minY + bounds.maxY) / 2;
const cz = (bounds.minZ + bounds.maxZ) / 2;
for (let index = 0; index < count; index += 1) {
const base = seed * 173 + index * 11;
// Start as one loose school near the centre, heading roughly the same way.
position[index * 3] = cx + (hash01(base + 1) - 0.5) * 240;
position[index * 3 + 1] = cy + (hash01(base + 2) - 0.5) * 120;
position[index * 3 + 2] = cz + (hash01(base + 3) - 0.5) * 240;
velocity[index * 3] = 120 + (hash01(base + 4) - 0.5) * 60;
velocity[index * 3 + 1] = (hash01(base + 5) - 0.5) * 40;
velocity[index * 3 + 2] = (hash01(base + 6) - 0.5) * 60;
}
return { count, position, velocity, bounds };
}
/** The point the school chases at `seconds`: a slow Lissajous loop through the middle of the box. */
export function flockLeader(bounds: FlockBounds, seconds: number): { x: number; y: number; z: number } {
const hx = (bounds.maxX - bounds.minX) / 2;
const hy = (bounds.maxY - bounds.minY) / 2;
const hz = (bounds.maxZ - bounds.minZ) / 2;
return {
x: bounds.minX + hx + Math.sin(seconds * 0.23) * hx * 0.75,
y: bounds.minY + hy + Math.sin(seconds * 0.37 + 1.1) * hy * 0.6,
z: bounds.minZ + hz + Math.cos(seconds * 0.17) * hz * 0.7,
};
}
/** Advances `flock` by `dt` seconds (clamped to 1/15 s). */
export function stepFlock(flock: Flock, dt: number, forces: FlockForces): void {
const step = Math.max(0, Math.min(1 / 15, Number.isFinite(dt) ? dt : 0));
if (step === 0) return;
const { count, position: p, velocity: v, bounds } = flock;
const leader = flockLeader(bounds, forces.seconds);
const gather = forces.gather ?? 0;
const scatter = forces.scatter ?? 0;
const separationRadius = SEPARATION_RADIUS * (1 + 1.5 * scatter);
const maxSpeed = MAX_SPEED * (1 + 0.8 * scatter);
const accel = new Float32Array(count * 3);
for (let i = 0; i < count; i += 1) {
const ix = p[i * 3]!;
const iy = p[i * 3 + 1]!;
const iz = p[i * 3 + 2]!;
let sepX = 0;
let sepY = 0;
let sepZ = 0;
let aliX = 0;
let aliY = 0;
let aliZ = 0;
let cohX = 0;
let cohY = 0;
let cohZ = 0;
let neighbours = 0;
for (let j = 0; j < count; j += 1) {
if (j === i) continue;
const dx = p[j * 3]! - ix;
const dy = p[j * 3 + 1]! - iy;
const dz = p[j * 3 + 2]! - iz;
const d2 = dx * dx + dy * dy + dz * dz;
if (d2 > NEIGHBOUR_RADIUS * NEIGHBOUR_RADIUS) continue;
neighbours += 1;
aliX += v[j * 3]!;
aliY += v[j * 3 + 1]!;
aliZ += v[j * 3 + 2]!;
cohX += dx;
cohY += dy;
cohZ += dz;
if (d2 < separationRadius * separationRadius && d2 > 1e-6) {
const push = 1 / d2;
sepX -= dx * push;
sepY -= dy * push;
sepZ -= dz * push;
}
}
let ax = 0;
let ay = 0;
let az = 0;
if (neighbours > 0) {
// Strong alignment and light cohesion: the school streams in ribbons rather than balling up.
const cohesion = 0.3 * (1 + 2 * gather) * (1 - scatter);
ax += (aliX / neighbours - v[i * 3]!) * 2.2 + (cohX / neighbours) * cohesion;
ay += (aliY / neighbours - v[i * 3 + 1]!) * 2.2 + (cohY / neighbours) * cohesion;
az += (aliZ / neighbours - v[i * 3 + 2]!) * 2.2 + (cohZ / neighbours) * cohesion;
}
const separation = 16000 * (1 + 4 * scatter);
ax += sepX * separation;
ay += sepY * separation;
az += sepZ * separation;
// Chase the leader.
ax += (leader.x - ix) * 0.16;
ay += (leader.y - iy) * 0.16;
az += (leader.z - iz) * 0.16;
// Soft walls.
ax += wall(ix, bounds.minX, bounds.maxX);
ay += wall(iy, bounds.minY, bounds.maxY);
az += wall(iz, bounds.minZ, bounds.maxZ);
accel[i * 3] = ax;
accel[i * 3 + 1] = ay;
accel[i * 3 + 2] = az;
}
for (let i = 0; i < count; i += 1) {
let vx = v[i * 3]! + accel[i * 3]! * step;
let vy = v[i * 3 + 1]! + accel[i * 3 + 1]! * step;
let vz = v[i * 3 + 2]! + accel[i * 3 + 2]! * step;
const speed = Math.hypot(vx, vy, vz);
const clamped = Math.max(MIN_SPEED, Math.min(maxSpeed, speed));
if (speed > 1e-6) {
vx = (vx / speed) * clamped;
vy = (vy / speed) * clamped;
vz = (vz / speed) * clamped;
} else E{
vx = clamped;
}
v[i * 3] = vx;
v[i * 3 + 1] = vy;
v[i * 3 + 2] = vz;
p[i * 3] = clampTo(p[i * 3]! + vx * step, bounds.minX - 200, bounds.maxX + 200);
p[i * 3 + 1] = clampTo(p[i * 3 + 1]! + vy * step, bounds.minY - 200, bounds.maxY + 200);
p[i * 3 + 2] = clampTo(p[i * 3 + 2]! + vz * step, bounds.minZ - 200, bounds.maxZ + 200);
}
}
/** Steering back into `[min, max]`, growing with how far outside a margin the boid has strayed. */
function wall(value: number, min: number, max: number): number {
const margin = (max - min) * 0.12;
if (value < min + margin) return (min + margin - value) * 6;
if (value > max - margin) return (max - margin - value) * 6;
return 0;
}
function clampTo(value: number, min: number, max: number): number {
return clamp(value, min, max);
}
|