
float cubefield2(float3 pos, float size, float xo, float zo, float yo) {
   pos.y += 2.0625;
   pos.y -= .1/(0.1+size*0.388);
   pos.x += xo;
   pos.z += zo;
   pos.y += yo;

   pos.xz-=floor(pos.xz);
   pos-=0.5;

   

   return max(max(abs(pos.x),abs(pos.y)),abs(pos.z))-size;

}

float PART2_f(float3 p) {

   
// R(p.xz,sin(time)*0.2);   
   
   //p.z += 10;

   //p.z -= time;
   
   p.y -= 0.3;
  
   float a = 1 + p.y;
   
   for(int i=0;i<9;i++) {
      a = min(a,cubefield2(p, 0.3 - i*0.025, i*0.283, i*0.14, sin(0.51*p.z*i+0.01*p.x*(i+1))*0.3 ));
   }

   return a*.8;
}

float ftest(float3 p) {
	p += float3(0.5,0.5,0.5);
	p += float3(0.5,0.5,0.5);
	p -= floor(p);
	p -= float3(0.5,0.5,0.5);
	return length(p) - 0.15;
}


PS_OUTPUT PART2_ps_main(in PS_INPUT In){
	PS_OUTPUT Out; 
   float2 v = In.Texture;// * 2 - 1;
    
   // p: Position / d: Direction
   float3 p = float3(0,0,0);
   
   
   float3 d = float3(v*float2(16.f/9,1), 1);
   
   p += matView[3].xyz;
   d = mul(d, (float3x3)matView);
   //R(d.xz, -time);
   
   if(scene_select == 2) {
   // braf
   R(v.xy, 1*length(v.xy)*sin(time*0.1));
   d.x = (atan2(v.x,-v.y));
   d.y = -length(v.yx);
   }
   
   // For proper raymarching we need ||d|| = 1
   d = normalize(d);
   
   // Iteration counter
   float i;
   
   // What distance did we march on the ray?
   float t = 0;
   
   // Loop using i, loops from i = 0 to i = 1
   for (i=0; i<1; i+=1/64.){
      //R(p.xy, 0.9*p.z);
      float l = PART2_f(p);
      //R(p.xy, 0.9*-p.z);
      // Advance along our ray into direction d or step back a little in
      // case f(p) is negative - this might actually happen.
      p += l * d;
      
      
      // In order to compute t we want to use the absolute value of f(p) -
      // we need the absolute value anyways to check against the threshold
      l = abs(l);
      t += l;
      
      // If |f(p)| is below a magic threshold we are close enough and can
      // stop here. Using t to determine the threshold might be a good
      // idea here - this implements some kind of cone tracing - for further
      // information you want to read the zeno paper from John C. Hart.
      if (l < .001*t) break;
   }
   
   // In case this is true we didn't hit anything. This also means this ray
   // was very expensive - you should do something to avoid that - bounding
   // spheres are a good start.
   if (i >= 1.) {
      //return float4(0,0,0,1);
   }

   // Compute normal using central differences - for other, faster and
   // possibly more imprecise methods you want to read the toolbox thread
   float2 epsilon = {.001,0};
   float OPTIMIZED = PART2_f(p);
   float3 normal = normalize(
      float3(
         PART2_f(p+epsilon.xyy) - OPTIMIZED,
         PART2_f(p+epsilon.yxy) - OPTIMIZED,
         PART2_f(p+epsilon.yyx) - OPTIMIZED
         )
      );

   // Now let's shade that thing
   
   float4 col = max(0.9 + dot(normal,d), 0);
   col.z += (1-i)*(1-i);
   col.bg += 0.2*log(t*0.25);
   col += 0.015*length(p);
   // hidey spirals
   if(scene_select == 2)
   	col += pow(cos(4*atan2(v.y,v.x)), 80.0);
   
   
   col.w = 1;
   //col.xyz = float3(v*float2(resolution.x/resolution.y,1), 0);
   Out.Color = col;
   //Out.Color.rgb = p.zzz;
   float3 viewpos = mul(p  - matView[3].xyz , transpose((float3x3)matView));
   Out.Depth = (1.0/viewpos.z*(-2*1*1000.0/999) + (1001.0/999) + 1.0)/2.0; // http://en.wikipedia.org/wiki/Z-buffering#Mathematics
   //Out.Color.rgb = viewpos.zzz;
   return Out;
}