/**

    License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

    Year of Truchets #050
    08/06/2023  @byt3_m3chanic
    Truchet Core \M/->.<-\M/ 2023

*/

#define R iResolution
#define T iTime
#define M iMouse

#define PI          3.14159265359
#define PI2         6.28318530718

#define MIN_DIST    1e-4
#define MAX_DIST    50.

float hash21( vec2 p ) { return fract(sin(dot(p,vec2(23.43,84.21))) *4832.3234); }
mat2 rot(float a){ return mat2(cos(a),sin(a),-sin(a),cos(a));}

//@iq hue palettes
vec3 hue(float t){ return .35 + .4*cos(PI2*t*(vec3(1.,.95,.65)+vec3(.05,.5,.95))); }

float scale = 4.;
void mainImage( out vec4 fragColor, in vec2 F )
{
    vec2 uv = (2.*F.xy-R.xy)/max(R.x,R.y);
    vec2 vv = uv;
    uv*=rot(T*.1+(M.x/R.x)*PI);

    float y = .3*sin(T*.5), x = .3*cos(T*.5);
    if(M.z>0.) {x=0.;y=0.;}

    uv *= rot(y+(M.y/R.y)*2.);
    uv = vec2(log(length(uv)), atan(uv.y, uv.x))*.637;//.31825;//1.274;//.637;//.31825;//

    uv.x+=.35*sin(uv.y*PI+T);
    uv.x+=T*.5;

    vec3 clb = hue(uv.x*.2);
    vec3 cla = vec3(.8);
    vec3 clx = mix(vec3(.1),vec3(.4),clamp(vv.y+.5,0.,1.));
    vec3 C = clx;

    float px = fwidth(uv.x)*4.;

    vec2 id=floor(uv*scale);
    float ck=mod(id.x+id.y,2.)*2.-1.;

    //mod for long slices
    vec2 cd=vec2(mod(id.x,1.),mod(id.y,3.))*2.-1.;

    uv=fract(uv*scale)-.5;

    float hs = hash21(id);
    if(hs>.5) uv.x=-uv.x;

    float rd = fract(hs*37.39);

    vec2 p = length(uv-.5) < length(uv+.5) ? uv-.5 : uv+.5;

    float d = length(p)-.5;

    if(rd>.85) d = min(length(uv.x),length(uv.y));
    //skip section logic
    if(cd.x>.5 ) d = length(uv.y);
    if(cd.y>.5 ) d = length(uv.x);

    if(cd.y>.5 && cd.x>.5) {
        d = 1.;
    }

    C = mix(C,C*.45,smoothstep(.05+px,-px,abs(d)-.2));
    C = mix(C,cla,smoothstep(px,-px,abs(d)-.14));
    C = mix(C,clb,smoothstep(px,-px,abs(abs(d)-.14)-.075));

    hs=hash21(vv);
    if(hs<.5) C=mix(clamp(C+(hs*.1),vec3(0),vec3(1)),C,smoothstep(px,-px,abs(d)-.2));
    // Gamma & Output
    C = pow(C,vec3(.4545));
    fragColor = vec4(C,1.0);
}
