 // Title:
 /*
 disclaimer : this is not meant to be good code, but a learning process for me
-going 'back to basic' and learning all about the needed maths, logic, etc 
+going 'back to basic' and learning all about the needed maths, logic, etc
 */
 #ifdef GL_ES
 precision mediump float;

float sdBox( in vec3 p, vec3 b )
{
  	vec3 d = abs(p) - b;//absolute distance to point - bounds
	  //max(d.y,d.z) => what is bigger , dimension along y or z
  	//max(d.x, max(d.y,d.z) => what is bigger dimension along x, y or z
    //min(XX, 0.0) => what is smaller the above or 0 ? ie what is closest to the point
    //length(max(d,0.0)) => what is the length of biggest between 0 & the distance
    return min(max(d.x, max(d.y,d.z)),0.0) + //
         length(max(d,0.0));
}


    float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
    return mix( b, a, h ) - k*h*(1.0-h);
}


float generatDistanceFields(vec3 pos){
    float d = 0.;
 	d= smin(
        sphere(pos+vec3(0.154,0.415,0.371), 0.160),
        sphere(pos+vec3(0.405,0.195,0.250), 0.380)  //length(p) - 0.5; // Distance to sphere of radius 0.5
    ,0.1);
    d = max(d, sdBox(pos+vec3(0.044,0.570,0.001),vec3(0.433,1.000,-.0)));
    d = min(d, sdCylinder(pos+vec3(0.1), vec3(0.074,0.180,0.235)));
    return d;
}

//RAY MARCH one two !
float rayMarch(vec3 origin, vec3 direction, float near, float far){
    float threshold = 0.001;
	float depth = near;//start at near

    for(int i = 0; i < maxSteps; ++i)
    {
        float distance = generatDistanceFields(origin + direction * depth);
        if(distance < threshold)
        {
            /*color = mix( vec4(colorGradient(i,maxSteps), 2.024), color, 0.868); // Sphere color
            //color = vec4(colorGradient(i, maxSteps),1.);
            color = color + vec4(colorGradient(i,maxSteps), 2.024)*0.01;*/
            //break;
            return depth;
        }

        depth += distance;

        if(depth >= far){
        	return 0.;
        }
    }

    //we have not bailed out so far, max distance reached
    return far;
}



//color gradient between blue and red based on iteration
