const gauss = () => { let u = 0, v = 0; while (!u) u = Math.random(); while (!v) v = Math.random(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(6.2832 * v); }; // A detailed spiral galaxy in its own plane (x/z): barred bulge, four main logarithmic arms with dark dust // lanes on their inner edges, dozens of short flocculent arm segments between them, bright star-forming // knots strung along the arms, and a faint disc. Returns the points and a per-point brightness. export function galaxyPts(m: number) { const out = new Float32Array(m * 3), sh = new Float32Array(m); const PITCH = 1 / Math.tan(0.24), armAt = (r: number, base: number) => base + PITCH * Math.log(r / 0.08); const knots: number[][] = []; for (let k = 0; k < 110; k++) { const base = ((k % 4) * Math.PI) / 2, r = 0.18 + Math.random() * 0.78, a = armAt(r, base) + gauss() * 0.04; knots.push([Math.cos(a) * r, Math.sin(a) * r, 0.006 + Math.random() * 0.014]); } const segs: number[][] = []; for (let k = 0; k < 64; k++) segs.push([Math.random() * 6.2832, 0.14 + Math.random() * 0.8, 0.08 + Math.random() * 0.22]); let i = 0; while (i < m) { const u = Math.random(); let x: number, y: number, z: number, s: number; if (u < 0.11) { const bar = Math.random() < 0.35; x = gauss() * (bar ? 0.15 : 0.065); z = gauss() * (bar ? 0.028 : 0.055); y = gauss() * 0.035; s = 0.85 + Math.random() * 0.15; if (bar) { const c = Math.cos(0.5), sn = Math.sin(0.5), xx = x * c - z * sn; z = x * sn + z * c; x = xx; } } else if (u < 0.52) { const base = (((Math.random() * 4) | 0) * Math.PI) / 2, r = 0.1 + Math.pow(Math.random(), 0.75) * 0.92; const off = gauss() * (0.016 + 0.045 * r); if (off < -0.005 && off > -0.016 - 0.02 * r && Math.random() < 0.88) continue; const a = armAt(r, base) + off / r; x = Math.cos(a) * r; z = Math.sin(a) * r; y = gauss() * 0.01; s = 0.32 + 0.58 * Math.exp(-Math.abs(off) / 0.022) * (1.05 - r * 0.5); } else if (u < 0.76) { const g = segs[(Math.random() * segs.length) | 0], r = g[1] + Math.random() * g[2], a = armAt(r, g[0]) + (gauss() * 0.018) / r; x = Math.cos(a) * r + gauss() * 0.006; z = Math.sin(a) * r + gauss() * 0.006; y = gauss() * 0.01; s = 0.28 + 0.32 * Math.random(); } else if (u < 0.87) { const kn = knots[(Math.random() * knots.length) | 0]; x = kn[0] + gauss() * kn[2]; z = kn[1] + gauss() * kn[2]; y = gauss() * 0.008; s = 0.92; } else { const r = Math.pow(Math.random(), 0.6) * 1.12, a = Math.random() * 6.2832; x = Math.cos(a) * r; z = Math.sin(a) * r; y = gauss() * 0.035; s = 0.1; } out[i * 3] = x; out[i * 3 + 1] = y; out[i * 3 + 2] = z; sh[i] = s; i++; } return { points: out, shade: sh }; }