// Name: Voxel Descent
// Author: Jaenman
// URL: https://www.shadertoy.com/view/Wc3cRr
#version 330 core
precision highp float;

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

/*================================
=         Voxel Descent          =
=         Author: Jaenam         =
================================*/
// Date:    2025-11-14
// License: Creative Commons (CC BY-NC-SA 4.0)

// Thanks to @diatribes for the plasma :D

void mainImage( out vec4 O, vec2 I )
{
    float i=0.,d=0.,s=0.,t = iTime*.8;
    vec3 p,q,z,k;
    vec4 c = vec4(1,2,3,1);
    mat2 R = mat2(cos(t/3.+vec4(0,33,11,0)));

    // Initialize O before loop (was O*=i where i=0, reading uninitialized out parameter)
    O = vec4(0.0);

    for(; i++<1e2;O+=sin(.5*c+i*.2)/s)

        z = normalize(vec3(I+I, 0) - vec3(iResolution.xy, iResolution.y)),
        z.xy*=R,
        p = d*z+sqrt(i)*i,
        k=z/=length(z.xy),
        z.z-=t, p.z+=t,

        d+=s=.012+.07*abs(mix(cos(dot(sin(floor(p/8.)).yzx,cos(ceil(z/.1)))),length(k),.2)-i/1e2);

    O*= exp(-.02*d*d);
    // Add epsilon protection to prevent division overflow
    O=tanh(O*O*mix(length(abs(c/max(dot(cos(t+p),vec3(6)), 0.001))),length(k),.5)/6e4);

}

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