#include <metal_stdlib>
using namespace metal;

// Creation by Silexars — Danguafer
// MSL port of creation.vk.glsl.

struct ShadertoyUBO {
    float iTime;
    float2 iResolution;
};

struct PassthroughVOut {
    float4 pos [[position]];
    float2 uv;
};

fragment float4 creation_fs(PassthroughVOut in [[stage_in]],
                            constant ShadertoyUBO& U [[buffer(0)]]) {
    float2 fragCoord = in.uv * U.iResolution;
    float t = U.iTime;
    float2 r = U.iResolution;

    float3 c = float3(0.0);
    float l = 0.0;
    float z = t;
    for (int i = 0; i < 3; i++) {
        float2 p = fragCoord / r;
        float2 uv = p;
        p -= 0.5;
        p.x *= r.x / r.y;
        z += 0.07;
        l = length(p);
        float invl = (l > 0.0) ? (1.0 / l) : 0.0;
        uv += p * invl * (sin(z) + 1.0) * abs(sin(l * 9.0 - z - z));
        float2 m = fract(uv) - 0.5;
        float denom = length(m);
        if (denom < 1e-6) denom = 1e-6;
        c[i] = 0.01 / denom;
    }
    float L = (l > 0.0) ? l : 1.0;
    return float4(c / L, t);
}
