#version 330 core
out vec4 FragColor;
uniform vec3 iResolution;
uniform float iTime;
uniform float uAlpha;

// Converted Gyroid shader

float tanh_f(float x) { return (exp(x)-exp(-x))/(exp(x)+exp(-x)); }

void mainImage(out vec4 o, vec2 u) {
    float s = 0.0;
    float i = 0.0;
    float d = 0.0;
    float T = iTime;

    vec3 p = iResolution;

    u = ( u - p.xy/2.0 ) / p.y
       + vec2(sin(T*0.3)*0.3, sin(T*0.15)*0.2);

    // initialize o to zero vector
    o = vec4(0.0);

    for(i = 0.0; i < 64.0; i += 1.0)
    {
        p = vec3(u * d, d + T * 1e2);
        p.z = p.z + cos(p.z * 0.7);
        p.x += 1e2 - cos(T) * 1e1;
        p += cos(p.yzx / 16.0) * 16.0;
        s = 0.005 + 0.8 * abs(32.0 * dot(sin(p / 132.0), cos(p.yzx / 64.0)));
        d += s;
        o += (1.0 + cos(0.03 * p.y + vec4(3.0,1.0,0.0,0.0))) / s;
    }

    o = tanh(o / 3000.0);
}

void main() { mainImage(FragColor, gl_FragCoord.xy); FragColor.a *= uAlpha; }
