uniform sampler2D layer0;
uniform sampler2D layer0d;
uniform sampler2D layer1;
uniform sampler2D layer1d;
uniform sampler2D layer2;
uniform sampler2D layer2d;
uniform sampler2D layer3;
uniform sampler2D layer3d;

uniform float l0opacity;
uniform float l1opacity;
uniform float l2opacity;

void main(){
  //gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0); return;

  vec2 texCoord = gl_TexCoord[0].st;

  vec4 l0 = texture2D(layer0, texCoord);
  vec4 l0d = texture2D(layer0d, texCoord);
  vec4 l1 = texture2D(layer1, texCoord);
  vec4 l1d = texture2D(layer1d, texCoord);
  vec4 l2 = texture2D(layer2, texCoord);
  vec4 l2d = texture2D(layer2d, texCoord);
  vec4 l3 = texture2D(layer3, texCoord);
  vec4 l3d = texture2D(layer3d, texCoord);


  l0.a *= l0opacity;
  l1.a *= l1opacity;
  l2.a *= l2opacity;
  
  
  //gl_FragDepth = l1d;  
  //gl_FragColor = vec4(pow(min(l1d, l0d), vec4(10.5)));
    
  //l0.a = 0.0;
  
  //gl_FragColor = vec4(vec3(pow(l0d.r, 10.0)),1.0);
  //gl_FragColor = l1;
  //gl_FragColor = l1d;
  //return;
  
  
  //l0.a *= 0.0;

/**  if (l0d.r < l2d.r)
    gl_FragColor = vec4(l0.rgb, 1.0);
  else
    gl_FragColor = vec4(l2.rgb, 1.0);
   */ 
    //gl_FragColor = vec4(l2d.rgb, 1.0);


  /*if (l0d.r < l1d.r && l0d.r < l2d.r)
  {
    gl_FragColor = vec4(l0.rgb, 1.0);
  }
  else
  if (l1d.r < l2d.r)
  {
    gl_FragColor = vec4(l1.rgb, 1.0);
  }
  else
  {
    gl_FragColor = vec4(l2.rgb, 1.0);
  }*/
  
  vec4 c0;
  vec4 c1;
  vec4 c2;
  
  if (l0d.r >= l1d.r && l0d.r >= l2d.r)
  {
    c0 = l0;    
    if (l1d.r > l2d.r)
    {
      c1 = l1;
      c2 = l2;
    } 
    else
    {
      c1 = l2;
      c2 = l1;
    }
  }
  if (l1d.r >= l0d.r && l1d.r >= l2d.r)
  {
    c0 = l1;    
    if (l0d.r > l2d.r)
    {
      c1 = l0;
      c2 = l2;
    } 
    else
    {
      c1 = l2;
      c2 = l0;
    }
  }
  if (l2d.r >= l0d.r && l2d.r >= l1d.r)
  {
    c0 = l2;    
    if (l0d.r > l1d.r)
    {
      c1 = l0;
      c2 = l1;
    } 
    else
    {
      c1 = l1;
      c2 = l0;
    }
  }
  
  //vec4 c_ = vec4(c0.rgb + c1.rgb * (1 - c0.a), c0.a + c1.a * (1.0 - c0.a));
  //c_ = vec4(c_.rgb + c2.rgb * (1 - c_.a), c_.a + c2.a * (1.0 - c_.a));
  
  vec4 cx = vec4(1,1,1,1); 
  
  vec4 c_ = vec4(c0.rgb * c0.a + cx.rgb * (1.0 - c0.a), clamp(cx.a + c0.a, 0.0, 1.0)); 
       c_ = vec4(c1.rgb * c1.a + c_.rgb * (1.0 - c1.a), clamp(c_.a + c1.a, 0.0, 1.0));
       c_ = vec4(c2.rgb * c2.a + c_.rgb * (1.0 - c2.a), clamp(c_.a + c2.a, 0.0, 1.0));

  //l3.a *= 0.5;
  //l3.rgb = vec3(1.0 - l3.r);
  //l3.a = l3.b;
  //l3.a = pow(l3.b, 1.0);
  //l3.a = 1-l3.b;
         
       //c_ = vec4(l3.rgb * l3.a + c_.rgb * (1.0 - l3.a), clamp(c_.a + l3.a, 0.0, 1.0));

  //gl_FragColor = vec4(l2.rgb, 1.0);
  //return;
    
  //c_ = vec4(0.8, 0.82, 0.9, 1.0);
  

  /*vec3 gray = vec3(0.5,0.6,1);
  l3.a *= 0.15;
       c_ = vec4(gray * l3.a + c_.rgb * (1.0 - l3.a), clamp(c_.a + l3.a, 0.0, 1.0));

  vec3 white = vec3(1,1,1);
  l3.b = pow(l3.b, 0.5);
       c_ = vec4(white * l3.b + c_.rgb * (1.0 - l3.b), clamp(c_.a + l3.b, 0.0, 1.0));

  vec3 black = vec3(0);
  l3.r = 1.0 - l3.r;
       c_ = vec4(black * l3.r + c_.rgb * (1.0 - l3.r), clamp(c_.a + l3.r, 0.0, 1.0));*/

  //gl_FragColor = vec4(vec3(c_.rgb), 1.0);  
    
  //float depth = min(l0d.r, min(l1d.r, min(l2d.r, l3d.r)));

  
  
  //float depth = min(l0d.r, min(l1d.r, min(l2d.r, l2d.r)));
  
  float _0 = l0opacity == 0.0 ? 2.0 : l0d.r;
  float _1 = l1opacity == 0.0 ? 2.0 : l1d.r;
  float _2 = l2opacity == 0.0 ? 2.0 : l2d.r;
  float depth = min(_0, min(_1, _2));
  
  gl_FragColor = vec4(vec3(c_.rgb), 1.0);
  
  //gl_FragColor = l3;
 
  gl_FragDepth = depth;  
  
  //gl_FragColor = l3d;
  //gl_FragDepth = l3d.r;
 
     
  //gl_FragData[1] = vec4(vec3(depth), 1.0);
  
  

  //gl_FragColor = vec4(vec3(depth), 1.0);
  
  //gl_FragColor = vec4(vec3(depth), 1.0);
  
  //depth = l0d.r;
  //gl_FragColor=vec4(vec3(depth), 1.0);
  
  //vec3 bg = vec3(1.0, 1.0, 1.0);
  //gl_FragColor = vec4(_dst.a * bg + _dst.rgb, 1.0 - _dst.a); 
  
   
}