// created by florian berger (flockaroo) - 2023
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

// 5 intersecting tetrahedra
// the edges form a dodecahedron

// less than 1 tweet in twigl.app:
// https://twigl.app/?ol=true&ss=-NTBw5cA8Txbmj0wIHQx

// ...a more elaborate version:
// https://www.shadertoy.com/view/3tsXD4

/*
#define rotate2D(x) mat2(cos(x-vec4(0,1.57,-1.57,0)))
#define t iTime
#define r iResolution
#define FC vec4(c,0,1)

void mainImage( out vec4 o, in vec2 c )
{
    vec3 p,q=vec3(0,0,6),a,s=(FC.rgb*2.-r.xyx)/r.x;
    float w,d,i=0.;
    for(;i<80.;i++,q+=s*d)
        for(d=w=6.+t;w>t;w-=1.3){
            p=q;p.xz*=rotate2D(w);p.xy*=rotate2D(1.);
            for(a=abs(p);a.z<a.x||a.z<a.y;p=p.zxy,a=a.zxy);
            d=min(d,length(p.xz-vec2(p.y,1)*a.z/p.z)*.7-.07);
        }
    o.rgb=fwidth(q*40.); // ...Xor's fwidth trick always handy for tiny-code-normals
}

*/

// fabrices optimizations so far:

#define R(x) mat2(cos(x-vec4(0,33,11,0)))

void mainImage( out vec4 o, vec2 c )
{
    vec3 a, r = iResolution,
         q = 6./r, p;

    for(float w,d,i=0. ; i++<80.; q += ( vec3(c+c,0) - r.xyx ) / r.x*d )
        for( d=w=6. ; w>0. ; w -= 1.3 ){
            p = q;
            p.xz *= R(iTime+w);
            for( p.xy *= R(1.) ; ( a = abs(p) ).z < max(a.x,a.y) ; p = p.zxy );
            d = min(d, length( p.xz - vec2(p.y,1)*a.z/p.z ) *.7 -.07 );
        }
    o.rgb = fwidth(q*r/9.);
}
