#version 330 core
out vec4 FragColor;
in vec2 vUV;

uniform vec3 iResolution;
uniform float iTime;
uniform float uAlpha;
uniform sampler2D iChannel0;

vec3 boostStars(vec3 c)
{
    float b = max(max(c.r, c.g), c.b);
    float m = smoothstep(0.18, 0.85, b);
    return c + c * m * 0.4;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
    vec2 p = (fragCoord - iResolution.xy * 0.5) / (min(iResolution.x, iResolution.y) * 0.5);
    float r = length(p);
    float a = atan(p.y, p.x) + iTime * 0.30;
    float tunnel = 0.18 / max(r, 0.025);

    vec2 starUV = vec2(fract(a / 6.2831853 + 0.01 * sin(iTime * 8.0 + tunnel * 20.0)), tunnel + iTime / 3.5) * 2.0;
    float beam = smoothstep(1.0, 0.05, r);
    float fade = smoothstep(2.0, 0.12, r);
    vec2 uv0 = vec2(fract(starUV.x), starUV.y);
    vec2 uv1 = vec2(fract(starUV.x + 0.5), starUV.y + 0.18);

    vec3 trail = boostStars(textureLod(iChannel0, uv0, 0.0).rgb) * 0.45 + boostStars(textureLod(iChannel0, uv1, 0.0).rgb) * 0.25;
    vec3 color = trail * vec3(0.75, 0.9, 1.35) + vec3(0.08, 0.16, 0.35) * beam + vec3(0.6, 0.75, 1.0) * pow(beam, 5.0);
    fragColor = vec4(color * fade, uAlpha);
}

void main()
{
    mainImage(FragColor, gl_FragCoord.xy);
}
