#version 450

// Name: Remember
// Author: diatribes
// URL: https://www.shadertoy.com/view/tXSBDK

layout(location = 0) in  vec2 vUV;
layout(location = 0) out vec4 FragColor;

layout(set = 3, binding = 0) uniform ShadertoyUBO {
    float iTime;
    vec2  iResolution;
};

// fuzzy brain — Hash function to replace iChannel0 texture noise
float hash12(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * .1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

void mainImage(out vec4 o, vec2 u) {

    vec3 q, p = vec3(iResolution.xy, iResolution.x / iResolution.y);

    float i = 0.0, s,
          d = .125 * hash12(u),
          t = iTime * .1;

    u = (u + u - p.xy) / p.y;
    if (abs(u.y) > .8) { o = vec4(0); return; }

    o = vec4(0.0);

    for (; i < 64.; i++) {

        q = p = vec3(u * d, d + t * 5.);
        p.xy *= mat2(cos(.1 * p.z + .1 * t + vec4(0, 33, 11, 0)));

        q.xz = cos(q.xz);
        p.z = cos(p.z);
        for (s = 1.; s++ < 6.;
            q += sin(.6 * t + p.zxy * .6),
            p += sin(t + t + p.yzx * s) * .6);

        d += s = .02 + abs(min(length(p + 3. * sin(p.z * .5)) - 4., length(q - 2. * sin(p.z * .4)) - 6.)) * .2;

        vec4 brightTerm = min(.01 * vec4(6, 2, 1, 0) / max(length(u * sin(t + t + t)), 0.001), vec4(50.0));
        o += brightTerm + 1. / s * length(u);

    }

    o = tanh(max(o / 6e2 + dot(u, u) * .35, 0.));
}

void main() {
    vec2 fragCoordPixels = vUV * iResolution;
    vec4 outColor;
    mainImage(outColor, fragCoordPixels);
    FragColor = outColor;
}
