// Name: Creation by Silexars
// Author: Danguafer
// URL: https://www.shadertoy.com/view/XsXXDn
#version 330 core
precision highp float;

out vec4 FragColor;
in vec2 vUV;
uniform vec2 iResolution;
uniform float iTime;

#define t iTime
#define r iResolution.xy

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec3 c = vec3(0.0);
    float l;
    float z = t;
    for (int i = 0; i < 3; i++) {
        vec2 uv, p = fragCoord.xy / r;
        uv = p;
        p -= 0.5;
        p.x *= r.x / r.y;
        z += 0.07;
        l = length(p);
        // evitar división por cero
        float invl = (l > 0.0) ? (1.0 / l) : 0.0;
        uv += p * invl * (sin(z) + 1.0) * abs(sin(l * 9.0 - z - z));
        vec2 m = mod(uv, 1.0) - 0.5;
        float denom = length(m);
        // evitar división por cero en el denominador
        if (denom < 1e-6) denom = 1e-6;
        c[i] = 0.01 / denom;
    }
    // si l es cero, usar 1.0 para evitar NaN
    float L = (l > 0.0) ? l : 1.0;
    fragColor = vec4(c / L, t);
}

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