| 1 | // Galaxia de partículas WebGL para el splash — sin librerías, autocontenida. |
| 2 | // Técnica: GL_POINTS + blending aditivo + rotación diferencial calculada por |
| 3 | // completo en el vertex shader desde una semilla (la CPU no toca nada por |
| 4 | // frame). ~24k partículas en espiral rosa→violeta con motas doradas, parallax |
| 5 | // de ratón y explosión al pulsar Entrar. |
| 6 | export function startGalaxy(container) { |
| 7 | const canvas = document.createElement('canvas'); |
| 8 | canvas.className = 'gl'; |
| 9 | container.prepend(canvas); |
| 10 | const gl = canvas.getContext('webgl', { alpha: true, antialias: false, depth: false }); |
| 11 | if (!gl) { canvas.remove(); return null; } // fallback: se queda el gradiente CSS |
| 12 | |
| 13 | const N = 24000; |
| 14 | const seeds = new Float32Array(N * 2); |
| 15 | for (let i = 0; i < seeds.length; i++) seeds[i] = Math.random(); |
| 16 | |
| 17 | const VS = ` |
| 18 | attribute vec2 seed; |
| 19 | uniform float t, aspect, burst, dpr; |
| 20 | uniform vec2 par; |
| 21 | varying vec3 vColor; |
| 22 | varying float vGlow; |
| 23 | float hash(float n){ return fract(sin(n) * 43758.5453); } |
| 24 | void main(){ |
| 25 | float r = 0.08 + 1.35 * pow(seed.y, 0.65); |
| 26 | float arm = floor(hash(seed.x * 7.0 + seed.y) * 3.0); |
| 27 | float ang = seed.x * 0.9 + arm * 2.0944 + r * 2.6 + t * (0.12 + 0.25 / (r + 0.2)); |
| 28 | float wob = (hash(seed.x * 13.1) - 0.5) * 0.22 * r; |
| 29 | vec2 p = vec2(cos(ang), sin(ang)) * (r + wob); |
| 30 | p.y *= 0.62; |
| 31 | p.y += (hash(seed.y * 17.7) - 0.5) * 0.14; |
| 32 | p += par * (0.03 + r * 0.06); |
| 33 | p *= 1.0 + burst * (0.4 + seed.y * 2.4); |
| 34 | gl_Position = vec4(p.x / aspect, p.y, 0.0, 1.0); |
| 35 | float tw = 0.55 + 0.45 * sin(t * (1.5 + seed.x * 3.0) + seed.y * 40.0); |
| 36 | vGlow = tw * (1.0 - burst * 0.55); |
| 37 | float gold = step(0.93, hash(seed.x * 31.3 + seed.y * 7.7)); |
| 38 | vec3 pink = vec3(0.33, 0.86, 0.77); |
| 39 | vec3 violet = vec3(0.51, 0.45, 1.0); |
| 40 | vec3 goldc = vec3(0.82, 0.63, 0.42); |
| 41 | vColor = mix(mix(pink, violet, smoothstep(0.1, 1.3, r)), goldc, gold); |
| 42 | gl_PointSize = (1.3 + 2.8 * tw * max(1.2 - r * 0.5, 0.2)) * dpr; |
| 43 | }`; |
| 44 | const FS = ` |
| 45 | precision mediump float; |
| 46 | varying vec3 vColor; |
| 47 | varying float vGlow; |
| 48 | void main(){ |
| 49 | float m = smoothstep(0.5, 0.0, length(gl_PointCoord - 0.5)); |
| 50 | gl_FragColor = vec4(vColor * vGlow, m * vGlow); |
| 51 | }`; |
| 52 | |
| 53 | const sh = (type, src) => { |
| 54 | const s = gl.createShader(type); |
| 55 | gl.shaderSource(s, src); |
| 56 | gl.compileShader(s); |
| 57 | if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s)); |
| 58 | return s; |
| 59 | }; |
| 60 | const prog = gl.createProgram(); |
| 61 | gl.attachShader(prog, sh(gl.VERTEX_SHADER, VS)); |
| 62 | gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FS)); |
| 63 | gl.linkProgram(prog); |
| 64 | gl.useProgram(prog); |
| 65 | |
| 66 | const buf = gl.createBuffer(); |
| 67 | gl.bindBuffer(gl.ARRAY_BUFFER, buf); |
| 68 | gl.bufferData(gl.ARRAY_BUFFER, seeds, gl.STATIC_DRAW); // una vez; la GPU manda |
| 69 | const loc = gl.getAttribLocation(prog, 'seed'); |
| 70 | gl.enableVertexAttribArray(loc); |
| 71 | gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); |
| 72 | |
| 73 | const U = n => gl.getUniformLocation(prog, n); |
| 74 | const uT = U('t'), uAspect = U('aspect'), uBurst = U('burst'), uPar = U('par'), uDpr = U('dpr'); |
| 75 | |
| 76 | gl.enable(gl.BLEND); |
| 77 | gl.blendFunc(gl.SRC_ALPHA, gl.ONE); // aditivo = brillo |
| 78 | gl.clearColor(0, 0, 0, 0); |
| 79 | |
| 80 | let burstAt = 0, alive = true; |
| 81 | const mouse = { x: 0, y: 0, tx: 0, ty: 0 }; |
| 82 | const onMove = e => { |
| 83 | mouse.tx = (e.clientX / innerWidth - 0.5) * 2; |
| 84 | mouse.ty = -(e.clientY / innerHeight - 0.5) * 2; |
| 85 | }; |
| 86 | addEventListener('pointermove', onMove); |
| 87 | |
| 88 | const dpr = Math.min(devicePixelRatio || 1, 2); |
| 89 | function frame(now) { |
| 90 | if (!alive) return; |
| 91 | const w = container.clientWidth, h = container.clientHeight; |
| 92 | if (canvas.width !== w * dpr || canvas.height !== h * dpr) { |
| 93 | canvas.width = w * dpr; |
| 94 | canvas.height = h * dpr; |
| 95 | gl.viewport(0, 0, canvas.width, canvas.height); |
| 96 | } |
| 97 | mouse.x += (mouse.tx - mouse.x) * 0.05; |
| 98 | mouse.y += (mouse.ty - mouse.y) * 0.05; |
| 99 | const t = now / 1000; |
| 100 | const burst = burstAt ? Math.min((now - burstAt) / 900, 1) : 0; |
| 101 | gl.clear(gl.COLOR_BUFFER_BIT); |
| 102 | gl.uniform1f(uT, t); |
| 103 | gl.uniform1f(uAspect, w / h); |
| 104 | gl.uniform1f(uBurst, burst); |
| 105 | gl.uniform1f(uDpr, dpr); |
| 106 | gl.uniform2f(uPar, mouse.x, mouse.y); |
| 107 | gl.drawArrays(gl.POINTS, 0, N); |
| 108 | requestAnimationFrame(frame); |
| 109 | } |
| 110 | requestAnimationFrame(frame); |
| 111 | |
| 112 | return { |
| 113 | burst() { burstAt = performance.now(); }, |
| 114 | stop() { alive = false; removeEventListener('pointermove', onMove); canvas.remove(); }, |
| 115 | }; |
| 116 | } |