#include <metal_stdlib>
using namespace metal;

// Octograms — whisky_shusuky
// MSL port of octograms.vk.glsl. The Shadertoy original uses a mutable
// file-scope `gTime`; in MSL we pass it as a parameter through the chain.

struct ShadertoyUBO {
    float iTime;
    float2 iResolution;
};

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

static float2x2 rot(float a) {
    float c = cos(a), s = sin(a);
    return float2x2(c, s, -s, c);
}

static float sdBox(float3 p, float3 b) {
    float3 q = abs(p) - b;
    return length(max(q, float3(0.0))) + min(max(q.x, max(q.y, q.z)), 0.0);
}

static float boxGeom(float3 pos, float scale) {
    pos *= scale;
    float base = sdBox(pos, float3(0.4, 0.4, 0.1)) / 1.5;
    pos.xy = (pos.xy * 5.0 - float2(0.0, 3.5)) * rot(0.75);
    return -base;
}

static float box_set(float3 pos, float gTime) {
    float3 pos_origin = pos;

    pos = pos_origin;
    pos.y += sin(gTime * 0.4) * 2.5;
    pos.xy = pos.xy * rot(0.8);
    float box1 = boxGeom(pos, 2.0 - abs(sin(gTime * 0.4)) * 1.5);

    pos = pos_origin;
    pos.y -= sin(gTime * 0.4) * 2.5;
    pos.xy = pos.xy * rot(0.8);
    float box2 = boxGeom(pos, 2.0 - abs(sin(gTime * 0.4)) * 1.5);

    pos = pos_origin;
    pos.x += sin(gTime * 0.4) * 2.5;
    pos.xy = pos.xy * rot(0.8);
    float box3 = boxGeom(pos, 2.0 - abs(sin(gTime * 0.4)) * 1.5);

    pos = pos_origin;
    pos.x -= sin(gTime * 0.4) * 2.5;
    pos.xy = pos.xy * rot(0.8);
    float box4 = boxGeom(pos, 2.0 - abs(sin(gTime * 0.4)) * 1.5);

    pos = pos_origin;
    pos.xy = pos.xy * rot(0.8);
    float box5 = boxGeom(pos, 0.5) * 6.0;

    pos = pos_origin;
    float box6 = boxGeom(pos, 0.5) * 6.0;

    return max(max(max(max(max(box1, box2), box3), box4), box5), box6);
}

fragment float4 octograms_fs(PassthroughVOut in [[stage_in]],
                              constant ShadertoyUBO& U [[buffer(0)]]) {
    float2 fragCoord = in.uv * U.iResolution;
    float2 p = (fragCoord * 2.0 - U.iResolution) / min(U.iResolution.x, U.iResolution.y);

    float3 ro = float3(0.0, -0.2, U.iTime * 4.0);
    float3 ray = normalize(float3(p, 1.5));
    ray.xy = ray.xy * rot(sin(U.iTime * 0.03) * 5.0);
    ray.yz = ray.yz * rot(sin(U.iTime * 0.05) * 0.2);
    float t = 0.1;
    float ac = 0.0;

    for (int i = 0; i < 99; i++) {
        float3 pos = ro + ray * t;
        pos = fract((pos - 2.0) / 4.0) * 4.0 - 2.0;
        float gTime = U.iTime - float(i) * 0.01;

        float d = box_set(pos, gTime);
        d = max(abs(d), 0.01);
        ac += exp(-d * 23.0);
        t += d * 0.55;
    }

    float3 col = float3(ac * 0.02);
    col += float3(0.0, 0.2 * abs(sin(U.iTime)), 0.5 + sin(U.iTime) * 0.2);

    return float4(col, 1.0 - t * (0.02 + 0.02 * sin(U.iTime)));
}
