// philip.bertani@gmail.com

#define oct 9   //number of fbm octaves
#define pi  3.14159265

float random(vec3 p) {
    //a random modification of the one and only random() func
    return fract( sin( dot( p, vec3(12., 90., -.8)))* 1e5 );
}


//this is taken from Visions of Chaos shader "Sample Noise 2D 4.glsl"
//and mangled to use vec3s at corners of tetrahedron
float noise(vec3 p) {
    vec3 i = floor(p);
    vec3 f = fract(p);
    float a = random(i + vec3(1.,1.,1.));
    float b = random(i + vec3(1.,-1.,-1.));
    float c = random(i + vec3(-1.,1.,1.));
    float d = random(i + vec3(-1.,1.,-1.));
    vec2 u = f.yz *f.xy*(3.-2.*f.xz); //smoothstep here, it also looks ok with u=f.yz
    
    return mix(a,b,u.x) + (c-a)*u.y*(1.-u.x) + (d-b)*u.x*u.y;

}

float fbm3d(vec3 p) {
    float v = 0.;
    float a = .5;
  
    for (int i=0; i<oct; i++) {
        v += a * noise(p);
        p = p * 2.;
        a *= .57*(1.+max(.1*sin(iTime/2.),0.));  //small increase causes increased brightness
    }
    return v;
}

mat3 rxz(float an){
    float cc=cos(an),ss=sin(an);
    return mat3(cc,0.,-ss,
                0.,1.,0.,
                ss,0.,cc);                
}
mat3 ryz(float an){
    float cc=cos(an),ss=sin(an);
    return mat3(1.,0.,0.,
                0.,cc,-ss,
                0.,ss,cc);
}   

struct RayInfo  {
    vec3 p1,p2;
    bool hit;
};

RayInfo RaySphereIntersect(vec3 ro, vec3 rd, vec3 spherepos, float r) {

    vec3  a = (spherepos - ro);
    float b = dot(rd, a);
    float c = dot(a,a) - r*r;
    float d = b*b - c;

    RayInfo ri; ri.hit=false;

    if ( d < 0.0 ) return ri;

    float sd = sqrt(d);
    float t1 = b - sd, t2 = b + sd;

    ri.p1 = ro + rd * t1;
    ri.p2 = ro + rd * t2;
  
    ri.hit = true;

    return ri;

}


vec3 get_color(vec3 p) {
    vec3 q;
    q.x = fbm3d(p);
    q.y = fbm3d(p.yzx);
    q.z = fbm3d(p.zxy);

    float f = fbm3d(p + q);
    
    return f*q;
}

vec3 sphere_colors(vec3 ro, vec3 rd) {

    
    vec3 p = ro *(2.+cos(iTime/7.));  //zoom in and out a little
    
    vec3 cc = vec3(0.);

    float stepsize = .01;
    float totdist = stepsize;
    
    for (int i=0; i<40; i++) {
       vec3 cx = get_color(p+vec3(0.,0.,iTime/5.));
       p += stepsize*rd;
       //float fi = float(i);
       cc += exp(-totdist*totdist)* cx;
       totdist += stepsize;
       //rd = ryz(.5)*rd;   //yz rotation here
    }
    
    
    cc = .5 + 2.3*(cc-.5);  //more contrast makes nice shimmering blobs
    cc = pow( cc / 50., vec3(6.));    //play with this
    
    return cc;

}


void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

    vec2 uv = (2.*fragCoord-iResolution.xy)/iResolution.y;
    //vec2 mm = (2.*iMouse.xy-iResolution.xy)/iResolution.y;

    vec3 rd = normalize( vec3(uv, -2.) );  
    vec3 ro = vec3(0.); 
    vec3 spherepos = vec3(0.,0.,-2.5);
    vec3 cc = vec3(0.);
    
   
    ro += vec3(0.,0., max(-1.3,min(0.,1.3*sin(iTime/4.))));
    
    RayInfo ri = RaySphereIntersect(ro,rd,spherepos,1.);    
    
    if ( ri.hit ) {
    
        mat3 rot2 = rxz(-iTime/9.)*ryz(iTime/20.);
    
        ro = rot2*ri.p1;
        rd = normalize( spherepos - ro);  //new rd point to center of sphere
    
        cc = sphere_colors(ro,rd);
    }
    
    fragColor = vec4(cc,1.0);
    
    
}
