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

// 5 intersecting octahedra

// less than 1 tweet in twigl.app (no cubemap there):
// https://twigl.app/?ol=true&ss=-NTOvYWIEieDpcGD3IMg

//  explanation of spiral-stepping:
// ---------------------------------
//
//  special thing here is that im stepping in a narrow spiral in view direction
//  the normal is then calculated from the triangle formed by last 3 step-points
//
//  i think it works quite well, and might be even practicable for non-golfed code
//


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

void mainImage( out vec4 o, in vec2 c )
{
    vec3 z=vec3(0,0,85),q=z,p,u,v;
    for(float w,d,i=0.;i++<80.;v=u,u=q,q+=(FC.rgb*2.-r.xyx)/r.x*d+vec3(sin(i),cos(i),0)) // sin,cos = spiral offs in x,y
       for(d=w=6.;w>0.;w-=1.26){
          p=q;p.xz*=rotate2D(t+w);p.xy*=rotate2D(1.);d=min(d,dot(p+sin(p)*.2,sign(p)*.3)-9.);
       }
    u=cross(q-u,q-v); // normal = cross product of 2 triangle edges (last 3 step points u,v,q)
    o.xyz=texture(iChannel0,reflect(q-z,u)).xyz-u*.1;
}

