#version 330 core
out vec4 FragColor;
in vec2 vUV;
uniform vec3 iResolution;
uniform float iTime;

// EtherFlow adapted shader

void mainImage(out vec4 o, vec2 u) {
    float d = 0.0;
    float a = 0.0;
    float e = 0.0;
    float i = 0.0;
    float s = 0.0;
    float t = iTime;
    vec3 ep;
    vec3 p = iResolution;

    // scale coords
    u = (u + u - p.xy) / p.y;

    u += vec2(cos(t * .4) * .3, cos(t * .8) * .1);

    for (o = vec4(0.0); i++ < 100.0;

        // accumulate distance
        d += s = min(.02 + .4 * abs(s), e = max(.8 * e, .01)),

        // grayscale color
        o += 16.0 * vec4(3.0, 2.0, 7.0, 0.0) / (s + e * .5) + .1 * vec4(6.0, 2.0, 1.0, 0.0) / length(u.x))

        // noise loop start, march
        for (p = vec3(u * d, d + t), // p = ro + rd *d, p.z + t;

            // entity (orb) position
            ep = p - vec3(
                sin(sin(t * 1.4) + t * .3) * 16.0,
                sin(sin(t) + t * 1.5) * 16.0,
                42.0 + t + cos(t * .5) * 32.0),

            // orb sphere
            e = length(ep) - .1,

            // plane, mix with entity/orb
            s = mix(e * .02, 16.0 - abs(p.x), smoothstep(0.0, 32.0, e)),

            // noise params
            a = .4; a < 8.0; a *= 1.4)

            // apply turbulence
            p += cos(p.yzx * .5) * .2,

            // apply noise
            s -= abs(dot(cos(2.0 * t + .2 * p.z + p * a), .2 + p - p)) / a;

    // tanh tonemap, blue tint, brightness, moon
    o = tanh(o / 1e3);
}

void main() { mainImage(FragColor, vUV * iResolution.xy); FragColor.a *= 1.0; }