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

987 lines
39 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;
}
// ---------- Sky: stars and moon (static, in front of the sunset backdrop) ----------
const starCount = 160;
const starPos = new Float32Array(starCount * 3);
const starCol = new Float32Array(starCount * 3);
const starBase = [], starSpeed = [], starPhase = [], starGlint = [];
for (let i = 0; i < starCount; i++) {
starPos[i * 3] = rnd(-85, 85);
starPos[i * 3 + 1] = rnd(14, 62);
starPos[i * 3 + 2] = rnd(-58, -55);
starCol[i * 3] = 1; starCol[i * 3 + 1] = 1; starCol[i * 3 + 2] = 1;
starBase.push(0.4 + Math.random() * 0.6); // rest brightness
starSpeed.push(0.6 + Math.random() * 2.2); // twinkle tempo
starPhase.push(Math.random() * Math.PI * 2); // twinkle offset
starGlint.push(Math.random() < 0.35 ? 0.6 + Math.random() * 0.6 : 0); // occasional sharp sparkle
}
const starGeo = new THREE.BufferGeometry();
starGeo.setAttribute('position', new THREE.BufferAttribute(starPos, 3));
starGeo.setAttribute('color', new THREE.BufferAttribute(starCol, 3));
const starsMat = new THREE.PointsMaterial({ color: 0xffffff, vertexColors: true, size: 0.85, transparent: true, opacity: 0.9, fog: false });
scene.add(new THREE.Points(starGeo, starsMat));
// Moon: a wireframe disc with a few craters
function circlePtsZ(r, n) {
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, Math.sin(a) * r, 0));
}
return pts;
}
const moon = new THREE.Group();
moon.position.set(38, 42, -58);
const moonMat = new THREE.LineBasicMaterial({ color: 0xffe8c0, transparent: true, opacity: 0.9, fog: false });
moon.add(new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(4.5, 20)), moonMat));
const moonCraterMat = new THREE.LineBasicMaterial({ color: 0xffe8c0, transparent: true, opacity: 0.4, fog: false });
for (const [cx, cy, cr] of [[1.2, 0.8, 1.1], [-1.5, -1.2, 0.8], [0.4, -2.0, 0.6], [-0.6, 1.6, 0.5]]) {
const crater = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(cr, 10)), moonCraterMat);
crater.position.set(cx, cy, 0);
moon.add(crater);
}
scene.add(moon);
// ---------- Equalizer skyline (distant bars pulsing with the bass) ----------
const EQ_BARS = 28;
const eqBase = [], eqPhase = [], eqSpeed = [];
const eqVerts = new Float32Array(EQ_BARS * 2 * 3);
const eqCol = new Float32Array(EQ_BARS * 2 * 3);
const eqPalette = [new THREE.Color(0x00e5ff), new THREE.Color(0x8f5bff), new THREE.Color(0xff2bd6)];
for (let i = 0; i < EQ_BARS; i++) {
eqBase.push(1.2 + Math.random() * 3.2);
eqPhase.push(Math.random() * Math.PI * 2);
eqSpeed.push(0.5 + Math.random() * 1.2);
const x = -52.5 + i * (105 / (EQ_BARS - 1));
const c = eqPalette[i % 3];
eqVerts.set([x, 0, 0, x, 1, 0], i * 6); // bottom, top (animated)
eqCol.set([c.r * 0.25, c.g * 0.25, c.b * 0.25, c.r, c.g, c.b], i * 6); // dark base -> bright tip
}
const eqGeo = new THREE.BufferGeometry();
eqGeo.setAttribute('position', new THREE.BufferAttribute(eqVerts, 3));
eqGeo.setAttribute('color', new THREE.BufferAttribute(eqCol, 3));
const eqMat = new THREE.LineBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.55 });
const eq = new THREE.LineSegments(eqGeo, eqMat);
eq.position.z = -45; // far down the grid, like a distant city skyline
scene.add(eq);
// ---------- Street lamps (scroll with the grid, recycle behind the camera) ----------
const LAMP_H = 5.4; // up with the palm trunks
const lampMat = new THREE.LineBasicMaterial({ color: 0xcfd8e3, transparent: true, opacity: 0.8 }); // silver
const lampGeo = new THREE.BufferGeometry();
lampGeo.setAttribute('position', new THREE.Float32BufferAttribute([
0, 0, 0, 0, LAMP_H, 0, // pole
0, LAMP_H, 0, 0.6, LAMP_H, 0, // arm
], 3));
const lampHeadGeo = new THREE.EdgesGeometry(new THREE.BoxGeometry(0.4, 0.24, 0.26));
// Light beam: an open cone of struts from the lamp head down to the grid
const beamGeo = new THREE.EdgesGeometry(new THREE.CylinderGeometry(0.18, 2.0, LAMP_H - 0.2, 8, 1, true));
const beamMat = new THREE.LineBasicMaterial({ color: 0xe6eef7, transparent: true, opacity: 0.15, depthWrite: false });
// Pool of light on the surface
const poolGeo = new THREE.BufferGeometry().setFromPoints(circlePtsY(2.0, 14, 0));
const poolMat = new THREE.LineBasicMaterial({ color: 0xeef4fb, transparent: true, opacity: 0.4, depthWrite: false });
for (const lane of [+6.2, -6.2]) {
for (let i = 0; i < 9; i++) {
const lamp = new THREE.Group();
lamp.position.set(lane, 0, -6 - i * 12);
lamp.add(new THREE.LineSegments(lampGeo, lampMat));
const head = new THREE.LineSegments(lampHeadGeo, lampMat);
head.position.set(0.78, LAMP_H - 0.1, 0);
lamp.add(head);
const beam = new THREE.LineSegments(beamGeo, beamMat);
beam.position.set(0.78, (LAMP_H - 0.2) / 2, 0);
lamp.add(beam);
const pool = new THREE.LineLoop(poolGeo, poolMat);
pool.position.set(0.78, 0.03, 0);
lamp.add(pool);
lamp.userData.baseX = lane;
// Slight size variance so they don't read as a rigid fence
lamp.scale.setScalar(0.9 + Math.random() * 0.3);
scene.add(lamp);
palmTrees.push(lamp); // reuse the same scroll & recycle pass
}
}
// ---------- Occasional shooting star ----------
const shootMat = new THREE.LineBasicMaterial({ color: 0xd9ecff, transparent: true, opacity: 0, fog: false });
const shootGroup = new THREE.Group();
shootGroup.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(0, 0, 0), new THREE.Vector3(1, 0, 0)]), shootMat));
shootGroup.visible = false;
scene.add(shootGroup);
const shooting = {
active: false,
group: shootGroup,
nextAt: performance.now() + rnd(12000, 25000),
life: 0,
dur: 1.3,
vel: new THREE.Vector3(),
};
const _shootDir = new THREE.Vector3();
function spawnShootingStar() {
const side = Math.random() < 0.5 ? -1 : 1;
shooting.vel.set(side * rnd(0.7, 1), -rnd(0.3, 0.5), 0).normalize().multiplyScalar(rnd(55, 85));
shooting.group.position.set(-side * rnd(15, 45), rnd(36, 55), -52);
_shootDir.copy(shooting.vel).normalize();
shooting.group.quaternion.setFromUnitVectors(new THREE.Vector3(1, 0, 0), _shootDir);
shooting.life = 0;
shooting.group.visible = true;
shooting.active = true;
}
// ---------- Retro diner kiosk (white body, neon-red signage) ----------
const kioskWhite = new THREE.LineBasicMaterial({ color: 0xe9edf3, transparent: true, opacity: 0.8 });
const neonRed = new THREE.LineBasicMaterial({ color: 0xff2b4a, transparent: true, opacity: 0.9 });
const warmGlow = new THREE.LineBasicMaterial({ color: 0xffc46b, transparent: true, opacity: 0.75 });
const shrubGreen = new THREE.LineBasicMaterial({ color: 0x3fd68f, transparent: true, opacity: 0.55 });
function starPts(rOut, rIn, n = 5) {
const pts = [];
for (let i = 0; i < n * 2; i++) {
const r = i % 2 === 0 ? rOut : rIn;
const a = (i / (n * 2)) * Math.PI * 2 - Math.PI / 2;
pts.push(new THREE.Vector3(Math.cos(a) * r, Math.sin(a) * r, 0));
}
return pts;
}
function rectRing(group, mat, w, d, y, cx = 0, cz = 0) {
const h = w / 2, k = d / 2;
return addLoop(group, mat, [V3(cx + h, y, cz + k), V3(cx - h, y, cz + k), V3(cx - h, y, cz - k), V3(cx + h, y, cz - k)]);
}
function makeDinerKiosk() {
const g = new THREE.Group(); // ground at y=0, front facing +Z (toward the camera)
// ---- Kiosk building: main box + stepped crown ----
addPart(g, kioskWhite, new THREE.BoxGeometry(6, 3.2, 4), 0, 1.6, 0);
addPart(g, kioskWhite, new THREE.BoxGeometry(6.6, 0.25, 4.6), 0, 3.32, 0);
addPart(g, kioskWhite, new THREE.BoxGeometry(4.4, 0.3, 3.0), 0, 3.62, -0.3);
rectRing(g, neonRed, 6.6, 4.6, 3.46); // neon roofline
// ---- Front facade ----
// Round diner window with warm interior glow
const roundWin = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.8, 12)), kioskWhite);
roundWin.position.set(-1.5, 2.0, 2.01); g.add(roundWin);
const roundGlass = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.62, 12)), warmGlow);
roundGlass.position.set(-1.5, 2.0, 2.02); g.add(roundGlass);
// Serving hatch: frame, glowing glass, counter ledge, neon awning
addPart(g, kioskWhite, new THREE.BoxGeometry(1.9, 1.3, 0.06), 1.5, 1.7, 2.01);
addPart(g, warmGlow, new THREE.BoxGeometry(1.55, 1.0, 0.03), 1.5, 1.68, 2.03);
addPart(g, kioskWhite, new THREE.BoxGeometry(2.2, 0.12, 0.7), 1.5, 0.9, 2.3);
addPart(g, neonRed, new THREE.BoxGeometry(2.4, 0.1, 1.1), 1.5, 2.75, 2.55, -0.35, 0, 0);
// Door with window, knob and step
addPart(g, kioskWhite, new THREE.BoxGeometry(0.95, 2.1, 0.06), -2.7, 1.05, 2.01);
addPart(g, warmGlow, new THREE.BoxGeometry(0.55, 0.75, 0.03), -2.7, 1.6, 2.03);
const knob = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.06, 8)), kioskWhite);
knob.position.set(-2.35, 0.95, 2.05); g.add(knob);
addPart(g, kioskWhite, new THREE.BoxGeometry(1.3, 0.12, 0.6), -2.7, 0.06, 2.35);
// Menu board with "text" lines
addPart(g, kioskWhite, new THREE.BoxGeometry(1.2, 0.85, 0.05), 2.45, 2.3, 2.01);
for (const my of [2.55, 2.35, 2.15]) {
addPart(g, warmGlow, new THREE.BoxGeometry(0.85, 0.05, 0.02), 2.45, my, 2.03);
}
// ---- Rooftop star sign ----
addPart(g, kioskWhite, new THREE.CylinderGeometry(0.06, 0.06, 1.3, 5), 0, 4.4, -0.3);
const roofStar = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(starPts(0.75, 0.3)), neonRed);
roofStar.position.set(0, 5.55, -0.3); g.add(roofStar);
// ---- Tower pylon sign: pole, twin disc, neon star ----
addPart(g, kioskWhite, new THREE.CylinderGeometry(0.09, 0.09, 6.4, 6), -4.6, 3.2, 0.6);
const pylonDisc = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(1.25, 14)), kioskWhite);
pylonDisc.position.set(-4.6, 5.3, 0.6); g.add(pylonDisc);
const pylonInner = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.95, 12)), neonRed);
pylonInner.position.set(-4.6, 5.3, 0.62); g.add(pylonInner);
const pylonStar = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(starPts(0.6, 0.24)), neonRed);
pylonStar.position.set(-4.6, 5.3, 0.64); g.add(pylonStar);
const pylonCap = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.18, 8)), neonRed);
pylonCap.position.set(-4.6, 6.5, 0.6); g.add(pylonCap);
// ---- Gas pump station: canopy, posts, pump ----
addPart(g, kioskWhite, new THREE.BoxGeometry(2.8, 0.16, 2.0), 5.0, 2.7, 0.4);
rectRing(g, neonRed, 2.8, 2.0, 2.58, 5.0, 0.4); // canopy neon edge
addPart(g, kioskWhite, new THREE.CylinderGeometry(0.07, 0.07, 2.7, 5), 6.1, 1.35, -0.25);
addPart(g, kioskWhite, new THREE.CylinderGeometry(0.07, 0.07, 2.7, 5), 6.1, 1.35, 1.05);
rectRing(g, kioskWhite, 2.6, 1.8, 0.02, 5.0, 0.4); // pump pad
addPart(g, kioskWhite, new THREE.BoxGeometry(0.75, 0.18, 0.55), 5.0, 0.09, 0.4); // pump base
addPart(g, kioskWhite, new THREE.BoxGeometry(0.55, 1.0, 0.38), 5.0, 0.68, 0.4); // pump body
const meter = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.2, 8)), warmGlow);
meter.position.set(5.0, 0.95, 0.61); g.add(meter);
const pumpTop = new THREE.LineLoop(new THREE.BufferGeometry().setFromPoints(circlePtsZ(0.2, 8)), neonRed);
pumpTop.position.set(5.0, 1.22, 0.4); g.add(pumpTop);
addPart(g, kioskWhite, new THREE.BoxGeometry(0.1, 0.32, 0.1), 5.3, 0.75, 0.4); // nozzle
// ---- Shrubs for a touch of life ----
const shrubA = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.IcosahedronGeometry(0.4, 0)), shrubGreen);
shrubA.position.set(-3.7, 0.3, 2.6); g.add(shrubA);
const shrubB = new THREE.LineSegments(new THREE.EdgesGeometry(new THREE.IcosahedronGeometry(0.45, 0)), shrubGreen);
shrubB.position.set(3.5, 0.35, 2.5); g.add(shrubB);
return g;
}
// Two kiosks on opposite lanes, far apart so one shows up only occasionally
const kioskA = makeDinerKiosk();
kioskA.position.set(20, 0, -40);
const kioskB = makeDinerKiosk();
kioskB.position.set(-20, 0, -130);
kioskB.scale.x = -1; // mirrored so its sign side faces the road
for (const k of [kioskA, kioskB]) {
k.userData.baseX = k.position.x;
k.userData.recycleZ = -180; // long spacing = "occasional"
scene.add(k);
palmTrees.push(k); // reuse the same scroll & recycle pass
}
// ---------- 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 = t.userData.recycleZ || -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);
}
}
// Street lamps: glow, beam and ground pool all breathe with the beat
lampMat.opacity = 0.55 + state.beat * 0.35;
beamMat.opacity = 0.1 + state.beat * 0.14;
poolMat.opacity = 0.3 + state.beat * 0.45;
// Diner kiosk: neon signage flickers brighter on the beat
neonRed.opacity = 0.7 + state.beat * 0.3;
warmGlow.opacity = 0.55 + state.beat * 0.4;
// Equalizer skyline: idle shimmer + bass-driven growth
const eqPos = eqGeo.attributes.position;
for (let i = 0; i < EQ_BARS; i++) {
const h = eqBase[i] * (0.55 + 0.45 * Math.sin(now * 0.001 * eqSpeed[i] + eqPhase[i])) * (1 + state.beat * 1.6);
eqPos.setY(i * 2 + 1, h);
}
eqPos.needsUpdate = true;
// Star twinkle: every star breathes on its own rhythm, some glint sharply
const starColorAttr = starGeo.attributes.color;
for (let i = 0; i < starCount; i++) {
const t1 = now * 0.001 * starSpeed[i] + starPhase[i];
let b = starBase[i] * (0.3 + 0.55 * Math.pow(0.5 + 0.5 * Math.sin(t1), 2));
if (starGlint[i]) {
const s = Math.sin(now * 0.0021 * starSpeed[i] + starPhase[i] * 3);
b += starGlint[i] * Math.pow(Math.max(0, s), 6);
}
starColorAttr.setXYZ(i, b * 0.78, b * 0.88, Math.min(1.2, b)); // pale ice-blue tint
}
starColorAttr.needsUpdate = true;
// Shooting star streaks
if (!shooting.active && now >= shooting.nextAt) spawnShootingStar();
if (shooting.active) {
shooting.life += dt;
const p = Math.min(1, shooting.life / shooting.dur);
shooting.group.position.addScaledVector(shooting.vel, dt);
shootMat.opacity = Math.sin(Math.PI * p) * 0.9;
shooting.group.scale.set(4 + 4 * Math.sin(Math.PI * p), 1, 1);
if (p >= 1) {
shooting.active = false;
shooting.group.visible = false;
shooting.nextAt = now + rnd(18000, 40000);
}
}
// 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);
}
}