// Remix of MLA's remix of https://twitter.com/zozuar/status/1612919479582728232

vec3 hsv(float h, float s, float v) {
  vec3 rgb = clamp( abs(mod(h*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
  // x²(3-2x) = 3x²-2x³, f'(x) = 6x-6x² = 6x(1-x)
  // f'(x) = 1-x², f = 0.5*(3.0*x-x³)
  rgb = rgb*rgb*(3.0-2.0*rgb); // cubic smoothing       
  return v * mix( vec3(1.0), rgb, s);
}

mat2 rotate2D(float t) {
  return mat2(cos(t),sin(t),-sin(t),cos(t));
}
void mainImage(out vec4 fragColor, vec2 fragCoord) {
  float time = iTime;
  fragCoord.x += 140.0*cos(iTime) * sin(0.01 * fragCoord.y);
  fragCoord.y += 88.88*cos(iTime) * sin(0.01 * fragCoord.x);
  fragColor = vec4(0);
  vec2 uv = 0.3*(2.0*fragCoord-iResolution.xy)/iResolution.y;
  vec3 ro = vec3(0,0,-0.6);
  vec3 rd = vec3(uv,1);
  float t = 0.0;
  vec2 m = 2.0*(iMouse.xy/iResolution.xy - 0.5); 
  for (float i = 0.0; i < 1e2; i++) {
    vec3 p = ro+t*rd-i/2e5;
    
      p.z -= abs(sin(iTime*0.1));
      p.yz *= rotate2D(sin(iTime*0.1));
      p.xz *= rotate2D(cos(iTime*0.1));
    
    p.yz *= rotate2D(0.2);

    float r = length(p);
    float e = asin(-p.z/r)-0.1/r;  // DE
    float rot = cos(iTime*0.2); // rotational symmetry
    vec3 q = vec3(log(r)-time,e,rot*atan(p.x,p.y)); // log spherical?
    for (float scale = 1.0; scale<1e2; scale += scale) {
      e += pow(abs(dot(sin(q.yxz*scale),cos(q*scale))),0.2)/scale; // FBM?
    }
    t += e*r*0.1; // Attenuate DE
    if (t > 50.0) break;
    float k = max(e*r*1e4,0.7);
    k = pow(k,0.4);
    fragColor.rgb += hsv(0.4-.02/r,k,0.02/exp(k));
  }
  fragColor *= 2.0/(1.0+fragColor);
  fragColor = pow(fragColor,vec4(0.4545));
}
