Become a Pro / website · becomeapro.ai / src / components / home / galaxy-shape.ts
| 1 | const gauss = () => { |
| 2 | let u = 0, v = 0; |
| 3 | while (!u) u = Math.random(); |
| 4 | while (!v) v = Math.random(); |
| 5 | return Math.sqrt(-2 * Math.log(u)) * Math.cos(6.2832 * v); |
| 6 | }; |
| 7 | |
| 8 | // A detailed spiral galaxy in its own plane (x/z): barred bulge, four main logarithmic arms with dark dust |
| 9 | // lanes on their inner edges, dozens of short flocculent arm segments between them, bright star-forming |
| 10 | // knots strung along the arms, and a faint disc. Returns the points and a per-point brightness. |
| 11 | export function galaxyPts(m: number) { |
| 12 | const out = new Float32Array(m * 3), sh = new Float32Array(m); |
| 13 | const PITCH = 1 / Math.tan(0.24), armAt = (r: number, base: number) => base + PITCH * Math.log(r / 0.08); |
| 14 | const knots: number[][] = []; |
| 15 | for (let k = 0; k < 110; k++) { |
| 16 | const base = ((k % 4) * Math.PI) / 2, r = 0.18 + Math.random() * 0.78, a = armAt(r, base) + gauss() * 0.04; |
| 17 | knots.push([Math.cos(a) * r, Math.sin(a) * r, 0.006 + Math.random() * 0.014]); |
| 18 | } |
| 19 | const segs: number[][] = []; |
| 20 | 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]); |
| 21 | let i = 0; |
| 22 | while (i < m) { |
| 23 | const u = Math.random(); let x: number, y: number, z: number, s: number; |
| 24 | if (u < 0.11) { |
| 25 | const bar = Math.random() < 0.35; |
| 26 | 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; |
| 27 | 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; } |
| 28 | } else if (u < 0.52) { |
| 29 | const base = (((Math.random() * 4) | 0) * Math.PI) / 2, r = 0.1 + Math.pow(Math.random(), 0.75) * 0.92; |
| 30 | const off = gauss() * (0.016 + 0.045 * r); |
| 31 | if (off < -0.005 && off > -0.016 - 0.02 * r && Math.random() < 0.88) continue; |
| 32 | const a = armAt(r, base) + off / r; |
| 33 | x = Math.cos(a) * r; z = Math.sin(a) * r; y = gauss() * 0.01; |
| 34 | s = 0.32 + 0.58 * Math.exp(-Math.abs(off) / 0.022) * (1.05 - r * 0.5); |
| 35 | } else if (u < 0.76) { |
| 36 | const g = segs[(Math.random() * segs.length) | 0], r = g[1] + Math.random() * g[2], a = armAt(r, g[0]) + (gauss() * 0.018) / r; |
| 37 | x = Math.cos(a) * r + gauss() * 0.006; z = Math.sin(a) * r + gauss() * 0.006; y = gauss() * 0.01; |
| 38 | s = 0.28 + 0.32 * Math.random(); |
| 39 | } else if (u < 0.87) { |
| 40 | const kn = knots[(Math.random() * knots.length) | 0]; |
| 41 | x = kn[0] + gauss() * kn[2]; z = kn[1] + gauss() * kn[2]; y = gauss() * 0.008; s = 0.92; |
| 42 | } else { |
| 43 | const r = Math.pow(Math.random(), 0.6) * 1.12, a = Math.random() * 6.2832; |
| 44 | x = Math.cos(a) * r; z = Math.sin(a) * r; y = gauss() * 0.035; s = 0.1; |
| 45 | } |
| 46 | out[i * 3] = x; out[i * 3 + 1] = y; out[i * 3 + 2] = z; sh[i] = s; i++; |
| 47 | } |
| 48 | return { points: out, shade: sh }; |
| 49 | } |