Bri-Tunes/public/js/bg-grid.js

706 lines
25 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Synthwave horizon grid background. Three.js module.
import * as THREE from 'three';
const canvas = document.getElementById('bg-canvas');
if (canvas) {
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x05060d, 18, 80);
const camera = new THREE.PerspectiveCamera(70, 1, 0.1, 200);
camera.position.set(0, 2.2, 10);
camera.lookAt(0, 1.8, 0);
const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, alpha: true });
renderer.setClearColor(0x000000, 0);
function resize() {
const w = window.innerWidth;
const h = window.innerHeight;
const dpr = Math.min(window.devicePixelRatio || 1, 1.5);
renderer.setPixelRatio(dpr);
renderer.setSize(w, h, false);
camera.aspect = w / h;
camera.updateProjectionMatrix();
}
resize();
window.addEventListener('resize', resize);
// ---------- Sunset backdrop (a large plane far behind the grid) ----------
const sunsetGeo = new THREE.PlaneGeometry(200, 120);
const sunsetMat = new THREE.ShaderMaterial({
uniforms: { uTime: { value: 0 } },
transparent: true,
depthWrite: false,
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
varying vec2 vUv;
uniform float uTime;
vec3 c1 = vec3(0.02, 0.01, 0.05); // deep space
vec3 c2 = vec3(0.22, 0.08, 0.36); // violet (dimmed)
vec3 c3 = vec3(0.40, 0.06, 0.34); // magenta (dimmed)
vec3 c4 = vec3(0.42, 0.26, 0.12); // amber (dimmed)
void main() {
float y = vUv.y;
// horizon around y = 0.45
vec3 col;
if (y < 0.45) {
col = mix(c1, c1 * 0.8, y / 0.45);
} else if (y < 0.58) {
col = mix(c4, c3, (y - 0.45) / 0.13);
} else if (y < 0.78) {
col = mix(c3, c2, (y - 0.58) / 0.20);
} else {
col = mix(c2, c1, (y - 0.78) / 0.22);
}
// subtle horizontal scan-bands over the sun
float band = sin((vUv.y - 0.45) * 90.0) * 0.5 + 0.5;
if (vUv.y > 0.46 && vUv.y < 0.58) {
float mask = smoothstep(0.58, 0.46, vUv.y);
col *= mix(1.0, 0.55, band * mask);
}
// Global darken so foreground text retains contrast
col *= 0.75;
gl_FragColor = vec4(col, 1.0);
}
`,
});
const sunset = new THREE.Mesh(sunsetGeo, sunsetMat);
sunset.position.set(0, 10, -60);
scene.add(sunset);
// ---------- Horizon line (thin cyan) ----------
const horizonGeo = new THREE.BufferGeometry();
horizonGeo.setAttribute(
'position',
new THREE.Float32BufferAttribute([-100, 5.5, -55, 100, 5.5, -55], 3),
);
const horizon = new THREE.Line(
horizonGeo,
new THREE.LineBasicMaterial({ color: 0x00e5ff, transparent: true, opacity: 0.9 }),
);
scene.add(horizon);
// ---------- Infinite recessing grid ----------
const GRID_SIZE = 200;
const GRID_DIVS = 40;
const gridGeo = buildGridGeometry(GRID_SIZE, GRID_DIVS);
const gridMat = new THREE.LineBasicMaterial({
color: 0x8f5bff,
transparent: true,
opacity: 0.85,
});
const grid = new THREE.LineSegments(gridGeo, gridMat);
grid.rotation.x = -Math.PI / 2;
grid.position.y = 0;
scene.add(grid);
function buildGridGeometry(size, divs) {
const verts = [];
const step = size / divs;
const half = size / 2;
for (let i = 0; i <= divs; i++) {
const p = -half + i * step;
// lines along X
verts.push(-half, p, 0, half, p, 0);
// lines along Y (which becomes Z after rotation)
verts.push(p, -half, 0, p, half, 0);
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.Float32BufferAttribute(verts, 3));
return geo;
}
// ---------- Palm trees ----------
// Trunk: muted brown; fronds: teal-green — both dimmed to sit in the synthwave palette
const palmTrunkMat = new THREE.LineBasicMaterial({ color: 0x9a6a42, transparent: true, opacity: 0.8 });
const palmFrondMat = new THREE.LineBasicMaterial({ color: 0x3fd68f, transparent: true, opacity: 0.7 });
const UP = new THREE.Vector3(0, 1, 0);
// A single feathered frond in local space: starts at the origin, arcs out
// toward +X and droops down. Built as a curved midrib with pinnate leaflets
// on both sides — widest near the middle, tapering to the tip.
function makeFrond(len, droop) {
const curve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(len * 0.5, len * 0.32, 0),
new THREE.Vector3(len * 1.05, -len * droop, 0),
);
const samples = 14;
const pts = curve.getPoints(samples);
const verts = [];
const push = (a, b) => verts.push(a.x, a.y, a.z, b.x, b.y, b.z);
for (let i = 1; i < pts.length; i++) push(pts[i - 1], pts[i]); // midrib
for (let i = 1; i <= samples; i++) {
const t = i / samples;
if (t < 0.1) continue;
const p = pts[i];
const tangent = pts[i].clone().sub(pts[i - 1]).normalize();
// Leaflet length follows a pinnate profile: 0 at base & tip, max mid-frond
const sideLen = Math.sin(t * Math.PI) * len * 0.30 * (0.9 + 0.1 * Math.sin(i * 3.7));
for (const s of [1, -1]) {
// Out of the midrib plane, angled slightly upward like real leaflets
const dir = new THREE.Vector3(-tangent.y * s, tangent.x * s, 0).lerp(UP, 0.35).normalize();
push(p, p.clone().addScaledVector(dir, sideLen));
}
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.Float32BufferAttribute(verts, 3));
return new THREE.LineSegments(geo, palmFrondMat);
}
function makePalmTree() {
const group = new THREE.Group();
// Gently curved trunk with ringed "joints" for a segmented look
const trunkHeight = 4.6;
const trunkCurve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0.24, trunkHeight * 0.55, 0),
new THREE.Vector3(0.6, trunkHeight, 0),
);
const joints = trunkCurve.getPoints(6);
const trunk = [];
for (let i = 1; i < joints.length; i++) {
trunk.push(joints[i - 1].x, joints[i - 1].y, joints[i - 1].z);
trunk.push(joints[i].x, joints[i].y, joints[i].z);
}
// Two offset rails so the trunk reads with a bit of thickness
for (const p of joints) {
trunk.push(p.x, p.y, p.z + 0.13, p.x, p.y, p.z - 0.13);
}
const trunkGeo = new THREE.BufferGeometry();
trunkGeo.setAttribute('position', new THREE.Float32BufferAttribute(trunk, 3));
group.add(new THREE.LineSegments(trunkGeo, palmTrunkMat));
// Ring joints along the trunk
for (let i = 1; i < joints.length - 1; i++) {
const ring = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.CircleGeometry(0.17, 7)), palmTrunkMat);
ring.rotation.x = Math.PI / 2;
ring.position.copy(joints[i]);
group.add(ring);
}
// Crown: small faceted knot where the fronds sprout
const crown = trunkCurve.getPoint(1);
const knot = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.IcosahedronGeometry(0.22, 0)), palmTrunkMat);
knot.position.copy(crown);
group.add(knot);
// 8 fronds in two tiers — lower tier longer and droopier, upper tier shorter
const FRONDS = 8;
for (let i = 0; i < FRONDS; i++) {
const lower = i % 2 === 0;
const frond = makeFrond(
(lower ? 2.6 : 2.1) + Math.random() * 0.9,
(lower ? 0.72 : 0.45) + Math.random() * 0.15,
);
const pivot = new THREE.Object3D();
pivot.position.copy(crown);
pivot.rotation.y = (Math.PI * 2 / FRONDS) * i + Math.random() * 0.5;
if (lower) pivot.position.y -= 0.08;
pivot.add(frond);
group.add(pivot);
}
// Randomize lean direction, and vary size slightly per tree
group.rotation.y = Math.random() * Math.PI * 2;
group.scale.setScalar(1.0 + Math.random() * 0.35);
return group;
}
// 4 columns × 4 rows = 16 trees; adjacent columns staggered by half-step
const PALM_COLS = [
{ x: +10, zOffset: 0 },
{ x: +14, zOffset: -7 },
{ x: -10, zOffset: 0 },
{ x: -14, zOffset: -7 },
];
const PALM_Z_ROWS = [-5, -19, -33, -47];
const palmTrees = [];
for (const col of PALM_COLS) {
for (const zRow of PALM_Z_ROWS) {
const tree = makePalmTree();
tree.position.set(col.x, 0, zRow + col.zOffset);
tree.userData.baseX = col.x; // restore X to this lane on recycle
scene.add(tree);
palmTrees.push(tree);
}
}
// ---------- Flyby wireframe vehicles ----------
function rnd(a, b) { return a + Math.random() * (b - a); }
function makeVehicleMat() {
// Each vehicle gets its own material so color can be changed at spawn time.
return new THREE.LineBasicMaterial({ color: 0x00e5ff, transparent: true, opacity: 0.9 });
}
// Shared builders. Vehicles are built nose-first along +X so they can be
// spawned heading either way and flown with natural, level attitudes.
function addPart(group, mat, geo, x = 0, y = 0, z = 0, rx = 0, ry = 0, rz = 0) {
const part = new THREE.LineSegments(new THREE.EdgesGeometry(geo), mat);
part.position.set(x, y, z);
part.rotation.set(rx, ry, rz);
group.add(part);
return part;
}
function addLoop(group, mat, pts) {
const loop = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(pts), mat);
group.add(loop);
return loop;
}
const V3 = (x, y, z = 0) => new THREE.Vector3(x, y, z);
function circlePtsX(r, n, x) { // ring in the YZ plane, normal along X
const pts = [];
for (let i = 0; i < n; i++) {
const a = (i / n) * Math.PI * 2;
pts.push(new THREE.Vector3(x, Math.cos(a) * r, Math.sin(a) * r));
}
return pts;
}
function circlePtsY(r, n, y) { // ring in the XZ plane (horizontal)
const pts = [];
for (let i = 0; i < n; i++) {
const a = (i / n) * Math.PI * 2;
pts.push(new THREE.Vector3(Math.cos(a) * r, y, Math.sin(a) * r));
}
return pts;
}
function makeAirplane() {
const mat = makeVehicleMat();
const g = new THREE.Group();
// Fuselage: round tube, pointed nose, tapered tail
addPart(g, mat, new THREE.CylinderGeometry(0.26, 0.26, 5.2, 8, 1), 0, 0, 0, 0, 0, Math.PI / 2);
addPart(g, mat, new THREE.ConeGeometry(0.26, 1.1, 8, 1), 3.1, 0, 0, 0, 0, -Math.PI / 2);
addPart(g, mat, new THREE.CylinderGeometry(0.06, 0.26, 1.3, 8, 1), -3.25, 0, 0, 0, 0, Math.PI / 2);
// Under-wing engines
addPart(g, mat, new THREE.CylinderGeometry(0.16, 0.2, 1.0, 8, 1), 0.55, -0.3, 0.85, 0, 0, Math.PI / 2);
addPart(g, mat, new THREE.CylinderGeometry(0.16, 0.2, 1.0, 8, 1), 0.55, -0.3, -0.85, 0, 0, Math.PI / 2);
// Swept main wings with slight dihedral
addLoop(g, mat, [V3(0.55, 0, 0.2), V3(-0.85, 0.22, 2.2), V3(-1.65, 0.22, 1.75), V3(-1.15, 0, 0.2)]);
addLoop(g, mat, [V3(0.55, 0, -0.2), V3(-0.85, 0.22, -2.2), V3(-1.65, 0.22, -1.75), V3(-1.15, 0, -0.2)]);
// Swept tailplane
addLoop(g, mat, [V3(-3.0, 0, 0.15), V3(-3.7, 0.12, 0.95), V3(-3.95, 0.12, 0.8), V3(-3.5, 0, 0.15)]);
addLoop(g, mat, [V3(-3.0, 0, -0.15), V3(-3.7, 0.12, -0.95), V3(-3.95, 0.12, -0.8), V3(-3.5, 0, -0.15)]);
// Vertical stabiliser
addLoop(g, mat, [V3(-2.6, 0.15, 0), V3(-3.5, 1.05, 0), V3(-3.85, 1.05, 0), V3(-3.85, 0.15, 0)]);
const s = { phase: Math.random() * 20 };
return {
group: g,
mat,
speedBase: 6,
spawn(dir, z, speed) {
s.dir = dir; s.speed = speed;
s.yBase = rnd(7, 12);
s.yawBase = dir > 0 ? 0 : Math.PI;
g.position.set(dir > 0 ? -75 : 75, s.yBase, z);
g.rotation.set(0, 0, 0);
},
// Level cruise with a slow, gentle heading change and matching bank
update(dt, t) {
const ph = t + s.phase;
g.position.x += s.dir * s.speed * dt;
g.position.y = s.yBase + Math.sin(ph * 0.18) * 0.35;
g.rotation.y = s.yawBase + Math.sin(ph * 0.21) * 0.16 * s.dir;
g.rotation.z = Math.cos(ph * 0.21) * 0.06 * s.dir;
},
};
}
function makeUFO() {
const mat = makeVehicleMat();
const g = new THREE.Group();
// Saucer body
addPart(g, mat, new THREE.CylinderGeometry(1.75, 1.45, 0.5, 14, 1));
const dome = addPart(g, mat, new THREE.SphereGeometry(0.8, 10, 6), 0, 0.35, 0);
dome.scale.set(1, 0.75, 1);
const under = addPart(g, mat, new THREE.SphereGeometry(0.5, 8, 5), 0, -0.35, 0);
under.scale.set(1.4, 0.6, 1.4);
// Light rings
addLoop(g, mat, circlePtsY(1.9, 20, -0.08));
addLoop(g, mat, circlePtsY(1.25, 14, 0.3));
const s = { phase: Math.random() * 20 };
return {
group: g,
mat,
speedBase: 3,
spawn(dir, z, speed) {
s.dir = dir; s.speed = speed;
s.yBase = rnd(5, 11);
g.position.set(dir > 0 ? -75 : 75, s.yBase, z);
g.rotation.set(0, 0, 0);
},
// Hovering drift: slow forward slide with a vertical bob and steady yaw
update(dt, t) {
const ph = t + s.phase;
g.position.x += s.dir * s.speed * dt;
g.position.y = s.yBase + Math.sin(ph * 0.8) * 0.35;
g.rotation.y += dt * 0.22;
g.rotation.z = Math.sin(ph * 0.6) * 0.04;
},
};
}
function makeCar() {
const mat = makeVehicleMat();
const g = new THREE.Group();
// Lower body + cabin
addPart(g, mat, new THREE.BoxGeometry(4.4, 0.55, 1.85), 0, 0.62, 0);
addPart(g, mat, new THREE.BoxGeometry(2.4, 0.55, 1.65), -0.1, 1.18, 0);
// Headlights and taillights
for (const z of [0.55, -0.55]) {
addPart(g, mat, new THREE.BoxGeometry(0.05, 0.12, 0.3), 2.21, 0.6, z);
addPart(g, mat, new THREE.BoxGeometry(0.05, 0.1, 0.25), -2.21, 0.65, z);
}
// Spoked wheels that spin about their axle
const wheels = [];
for (const [wx, wz] of [[1.45, 0.85], [1.45, -0.85], [-1.45, 0.85], [-1.45, -0.85]]) {
const wheel = new THREE.Group();
wheel.position.set(wx, 0.42, wz);
wheel.add(new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.CircleGeometry(0.42, 10)), mat));
wheel.add(new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.CircleGeometry(0.12, 6)), mat));
g.add(wheel);
wheels.push(wheel);
}
const s = { phase: Math.random() * 20 };
return {
group: g,
mat,
speedBase: 5,
spawn(dir, z, speed) {
s.dir = dir; s.speed = speed;
s.yawBase = dir > 0 ? 0 : Math.PI;
g.position.set(dir > 0 ? -75 : 75, 0, z);
g.rotation.set(0, 0, 0);
},
// Drives along the ground: level, steady, slight suspension bounce
update(dt, t) {
const ph = t + s.phase;
g.position.x += s.dir * s.speed * dt;
g.position.y = Math.abs(Math.sin(ph * 2.2)) * -0.04;
g.rotation.y = s.yawBase;
for (const w of wheels) w.rotation.z += (s.dir * s.speed * dt) / 0.42;
},
};
}
function makeSpaceship() {
const mat = makeVehicleMat();
const g = new THREE.Group();
// Hull: slim body, pointed nose, tapered aft section
addPart(g, mat, new THREE.CylinderGeometry(0.3, 0.3, 2.6, 8, 1), 0, 0, 0, 0, 0, Math.PI / 2);
addPart(g, mat, new THREE.ConeGeometry(0.3, 1.5, 8, 1), 2.0, 0, 0, 0, 0, -Math.PI / 2);
addPart(g, mat, new THREE.CylinderGeometry(0.18, 0.3, 1.1, 8, 1), -1.8, 0, 0, 0, 0, Math.PI / 2);
// Canopy
const canopy = addPart(g, mat, new THREE.SphereGeometry(0.26, 8, 6), 0.7, 0.3, 0);
canopy.scale.set(1.3, 1, 1);
// Swept delta wings
addLoop(g, mat, [V3(0.6, 0, 0.18), V3(-1.3, 0.05, 1.8), V3(-2.0, 0.15, 1.15), V3(-1.15, 0, 0.18)]);
addLoop(g, mat, [V3(0.6, 0, -0.18), V3(-1.3, 0.05, -1.8), V3(-2.0, 0.15, -1.15), V3(-1.15, 0, -0.18)]);
// Twin dorsal fins
addLoop(g, mat, [V3(-1.5, 0.1, 0.3), V3(-2.2, 0.1, 0.3), V3(-2.2, 0.85, 0.3)]);
addLoop(g, mat, [V3(-1.5, 0.1, -0.3), V3(-2.2, 0.1, -0.3), V3(-2.2, 0.85, -0.3)]);
// Twin engine nozzles
addPart(g, mat, new THREE.CylinderGeometry(0.17, 0.21, 0.3, 8, 1), -2.5, 0, 0.22, 0, 0, Math.PI / 2);
addPart(g, mat, new THREE.CylinderGeometry(0.17, 0.21, 0.3, 8, 1), -2.5, 0, -0.22, 0, 0, Math.PI / 2);
const s = { phase: Math.random() * 20 };
return {
group: g,
mat,
speedBase: 5.5,
spawn(dir, z, speed) {
s.dir = dir; s.speed = speed;
s.yBase = rnd(6, 14);
s.yawBase = dir > 0 ? 0 : Math.PI;
g.position.set(dir > 0 ? -75 : 75, s.yBase, z);
g.rotation.set(0, 0, 0);
},
// Gliding flight: slow yaw drift with a hint of roll and pitch
update(dt, t) {
const ph = t + s.phase;
g.position.x += s.dir * s.speed * dt;
g.position.y = s.yBase + Math.sin(ph * 0.25) * 0.5;
g.rotation.y = s.yawBase + Math.sin(ph * 0.19) * 0.12 * s.dir;
g.rotation.z = Math.sin(ph * 0.16) * 0.08 * s.dir;
g.rotation.x = Math.sin(ph * 0.13) * 0.05;
},
};
}
function makeHelicopter() {
const mat = makeVehicleMat();
const g = new THREE.Group();
// Cabin (front) + main body, nose at +X
const cabin = addPart(g, mat, new THREE.SphereGeometry(0.5, 8, 6), 1.05, 0.05, 0);
cabin.scale.set(1.25, 0.9, 1.0);
addPart(g, mat, new THREE.BoxGeometry(1.7, 0.7, 0.72), 0.1, 0, 0);
// Tail boom + fin
addPart(g, mat, new THREE.BoxGeometry(1.7, 0.2, 0.2), -1.45, 0.1, 0);
addLoop(g, mat, [V3(-2.0, 0.15, 0), V3(-2.35, 0.7, 0), V3(-2.35, 0.15, 0)]);
// Tail rotor (spins about Z)
const tailRotor = new THREE.Group();
tailRotor.position.set(-2.35, 0.2, 0.15);
tailRotor.add(new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.CircleGeometry(0.32, 10)), mat));
g.add(tailRotor);
// Main rotor: hub + spinning disc with two long blades
addPart(g, mat, new THREE.CylinderGeometry(0.09, 0.13, 0.2, 6, 1), 0.15, 0.52, 0);
const mainRotor = new THREE.Group();
mainRotor.position.set(0.15, 0.65, 0);
const disc = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsY(1.55, 12, 0)), mat);
mainRotor.add(disc);
mainRotor.add(new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.BoxGeometry(3.1, 0.03, 0.14)), mat));
const blade2 = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.BoxGeometry(3.1, 0.03, 0.14)), mat);
blade2.rotation.y = Math.PI / 2;
mainRotor.add(blade2);
g.add(mainRotor);
// Landing skids
for (const z of [0.5, -0.5]) {
addPart(g, mat, new THREE.BoxGeometry(2.4, 0.06, 0.06), 0, -0.6, z);
addPart(g, mat, new THREE.BoxGeometry(0.08, 0.35, 0.08), 0.65, -0.35, z);
addPart(g, mat, new THREE.BoxGeometry(0.08, 0.35, 0.08), -0.65, -0.35, z);
}
const s = { phase: Math.random() * 20 };
return {
group: g,
mat,
speedBase: 4,
spawn(dir, z, speed) {
s.dir = dir; s.speed = speed;
s.yBase = rnd(4, 8);
s.yawBase = dir > 0 ? 0 : Math.PI;
g.position.set(dir > 0 ? -75 : 75, s.yBase, z);
g.rotation.set(0, 0, 0);
},
// Slow forward flight with rotor spin, hover bob and gentle sway
update(dt, t) {
const ph = t + s.phase;
g.position.x += s.dir * s.speed * dt;
g.position.y = s.yBase + Math.sin(ph * 1.1) * 0.15;
g.rotation.y = s.yawBase + Math.sin(ph * 0.3) * 0.05 * s.dir;
g.rotation.z = Math.sin(ph * 0.7) * 0.05 * s.dir;
mainRotor.rotation.y += dt * 26;
tailRotor.rotation.z += dt * 42;
},
};
}
function makeRocket() {
const mat = makeVehicleMat();
const g = new THREE.Group();
// Cylindrical body, conical nose
addPart(g, mat, new THREE.CylinderGeometry(0.3, 0.3, 3.4, 10, 1), 0, 0, 0, 0, 0, Math.PI / 2);
addPart(g, mat, new THREE.ConeGeometry(0.3, 1.4, 10, 1), 2.4, 0, 0, 0, 0, -Math.PI / 2);
// Decorative bands
addLoop(g, mat, circlePtsX(0.31, 12, 0.5));
addLoop(g, mat, circlePtsX(0.31, 12, -0.8));
// Four tail fins
for (let i = 0; i < 4; i++) {
const fin = new THREE.Group();
fin.rotation.y = (Math.PI / 2) * i;
addLoop(fin, mat, [V3(-1.0, 0.28, 0), V3(-1.7, 0.28, 0), V3(-1.7, 0.85, 0)]);
g.add(fin);
}
// Nozzle ring + flickering exhaust plume
addLoop(g, mat, circlePtsX(0.24, 10, -1.75));
const flame = new THREE.Group();
flame.position.set(-1.8, 0, 0);
const flamePts = new THREE.BufferGeometry().setFromPoints([V3(0, 0, 0), V3(-0.8, 0, 0)]);
flame.add(new THREE.Line(flamePts, mat));
g.add(flame);
const s = { phase: Math.random() * 20 };
return {
group: g,
mat,
speedBase: 8,
spawn(dir, z, speed) {
s.dir = dir; s.speed = speed;
s.yBase = rnd(10, 16);
s.yawBase = dir > 0 ? 0 : Math.PI;
g.position.set(dir > 0 ? -75 : 75, s.yBase, z);
g.rotation.set(0, 0, 0);
},
// Long, steady high-altitude flight with a whisper of pitch wobble
update(dt, t) {
const ph = t + s.phase;
g.position.x += s.dir * s.speed * dt;
g.position.y = s.yBase + Math.sin(ph * 0.12) * 0.3;
g.rotation.y = s.yawBase;
g.rotation.z = Math.sin(ph * 0.5) * 0.04 * s.dir;
flame.scale.x = 0.7 + 0.35 * Math.sin(ph * 45);
},
};
}
const FLYBY_COLORS = [0x00e5ff, 0xff2bd6, 0x8f5bff];
const flybyPool = [makeAirplane, makeUFO, makeCar, makeSpaceship, makeHelicopter, makeRocket]
.map(fn => {
const r = fn();
r.group.visible = false;
scene.add(r.group);
return r;
});
const flyby = {
active: null,
nextAt: performance.now() + rnd(5000, 12000),
colorIdx: 0,
};
function spawnFlyby() {
const entry = flybyPool[Math.floor(Math.random() * flybyPool.length)];
const dir = Math.random() < 0.5 ? -1 : 1;
entry.mat.color.setHex(FLYBY_COLORS[flyby.colorIdx++ % FLYBY_COLORS.length]);
const z = rnd(-8, -45);
// Closer passes move faster so they feel properly scaled in speed.
const dist = Math.abs(camera.position.z - z);
const speed = entry.speedBase * (45 / dist);
entry.spawn(dir, z, speed);
entry.group.visible = true;
flyby.active = entry;
}
// ---------- Beat / bass reactivity surface ----------
const state = {
beat: 0, // smoothed bass energy 0..1
scrollX: 0, // accumulated grid scroll on X
scrollZ: 0, // accumulated grid scroll on Z
dirAngle: 0, // current heading (radians, 0 = +Z toward camera)
targetAngle: 0, // target heading we're easing toward
nextTurnAt: performance.now() + 10000,
lastT: performance.now(),
visible: true,
};
function pickNewDirection() {
const spread = Math.PI * 0.9;
state.targetAngle = (Math.random() - 0.5) * spread;
state.nextTurnAt = performance.now() + 10000;
}
window.briTunesBg = {
setBeat(v) {
state.beat = Math.max(state.beat * 0.6, Math.min(1, v));
},
};
document.addEventListener('visibilitychange', () => {
state.visible = !document.hidden;
});
function frame() {
if (!state.visible) {
requestAnimationFrame(frame);
return;
}
const now = performance.now();
const dt = Math.min(0.05, (now - state.lastT) / 1000);
state.lastT = now;
// Decay beat
state.beat *= 0.92;
// Time to pick a new heading?
if (now >= state.nextTurnAt) pickNewDirection();
// Ease current angle toward target
const angleDelta = state.targetAngle - state.dirAngle;
state.dirAngle += angleDelta * Math.min(1, dt * 1.2);
// Scroll the grid in the current heading
const baseSpeed = 6;
const speed = baseSpeed * (1 + state.beat * 1.2);
state.scrollX += dt * speed * Math.sin(state.dirAngle);
state.scrollZ += dt * speed * Math.cos(state.dirAngle);
const step = (GRID_SIZE / GRID_DIVS);
grid.position.x = ((state.scrollX % step) + step) % step;
grid.position.z = ((state.scrollZ % step) + step) % step;
// Scroll palm trees with the grid (same heading); recycle when behind camera
for (const t of palmTrees) {
t.position.x += dt * speed * Math.sin(state.dirAngle);
t.position.z += dt * speed * Math.cos(state.dirAngle);
if (t.position.z > 15) {
t.position.z = -54;
t.position.x = t.userData.baseX; // snap back to column lane on recycle
}
}
// Flyby objects
if (!flyby.active && now >= flyby.nextAt) spawnFlyby();
if (flyby.active) {
flyby.active.update(dt, now * 0.001);
if (Math.abs(flyby.active.group.position.x) > 80) {
flyby.active.group.visible = false;
flyby.active = null;
flyby.nextAt = now + rnd(4000, 10000);
}
}
// Pulse color on beat (violet -> magenta)
const mix = Math.min(1, state.beat);
gridMat.color.setRGB(0.56 + mix * 0.44, 0.35 - mix * 0.2, 0.94);
gridMat.opacity = 0.75 + mix * 0.25;
horizon.material.opacity = 0.7 + mix * 0.3;
sunsetMat.uniforms.uTime.value = now * 0.001;
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
if (reduced) {
renderer.render(scene, camera);
} else {
requestAnimationFrame(frame);
}
}