                                                                                                         #version 330
 
uniform sampler2D t;
uniform sampler2D t2;
uniform float xfade;
uniform int fadetype;

uniform sampler2D m;
uniform int mask;
uniform vec2 size; 

in vec2 uv;
in vec2 uv1;
in vec4 color;
in float vertexFade;

smooth in vec4 maskPosition;
smooth in vec4 vPosition;
smooth in vec4 vPrevPosition;

layout(location = 0) out vec4 output;
layout(location = 1) out vec4 motion;

vec4 stripes(in vec4 texColor0, in vec4 texColor1, in float dif, in float zo)
{
    vec4 texColor = texColor0;

    float cw = 0.01;
    vec4 borderColor = mix(    vec4(0,0,0,1.0),
                               texColor1,
                               max(0.0,min(1.0,zo * 2.5 - 1.0))
                          );

    
    float bpow = 1.0;
    float bf = 2.0;
       
    if (dif < -0.5 + cw && 1.0 - zo < uv.x)
    {
      texColor =  mix(texColor0, borderColor, bf*(1.0 - clamp((dif + 0.5) / cw, 0.0, 1.0)));
    }
    else if (dif > - cw && 1.0 - zo < uv.x)
    {      
      texColor =  mix(texColor0, borderColor, bf*(1.0 - clamp(bf*-dif/cw, 0.0, 1.0)));
    }

    if (dif < cw && zo > uv.x)
    {
      texColor =  mix(texColor0, borderColor, bf*(1.0 - clamp(dif / cw, 0.0, 1.0)));
    }
    else if (dif > 0.5 - cw && zo > uv.x)
    {      
      texColor =  mix(texColor0, borderColor, bf*(1.0 - clamp(-(dif - 0.5) / cw, 0.0, 1.0)));
    } 
        
    if (dif < 0 && zo > uv.x)
    {
        texColor = mix(texColor1, borderColor, bf*(1.0 - min(1,-dif/cw)));
        
        texColor = mix(texColor, borderColor, bf*(1.0 - clamp((dif + 0.5) / cw, 0.0, 1.0)));
    } 
    else if (dif >= 0 && 1.0 - zo < uv.x)
    {
        texColor = mix(texColor1, borderColor, bf*(1.0 - min(1,dif / cw)));
        
        texColor = mix(texColor, borderColor, bf*(1.0-clamp(-(dif - 0.5) / cw, 0.0, 1.0)));
    }
    
    return texColor;
}

void main()
{
  float d = length(uv1 - vec2(0.5, 0.5));

  if (vertexFade > 0.0 && d > 1.0 - vertexFade) discard;

  float alphaMask = 1.0;
  vec3 ndc = maskPosition.xyz / maskPosition.w; 
  vec2 muv = (ndc.xy * .5 + .5);  
  if (mask == 1)
  {
    alphaMask = 1.0-texture(m, gl_FragCoord.xy / size).r;
  }
  else
  if (mask == 2)
  {
    alphaMask = texture(m, gl_FragCoord.xy / size).r;
  }


  vec4 texColor0 = texture(t, uv);
  vec4 texColor1 = texture(t2, uv);
  

  
  vec4 texColor = texColor0;
  
  if (fadetype == 2)
  {
    float zo = max(0.0, min(1.0, (xfade - 0.5) * 4.0)); 
    
    float width = 0.3;
    float md = mod(uv.y + uv.x, width) / width;
    float dif = md - 0.5;
                             
    texColor = stripes(texColor0, texColor1, dif, zo);        
  }
  else if (fadetype == 3)
  {
    float zo = max(0.0, min(1.0, (xfade - 0.25) * 4.0)); 
    
    float width = 0.5;
    float md = mod(uv.y + uv.y*uv.x*2.0, width) / width;
    float dif = md - 0.5;
                             
    texColor = stripes(texColor0, texColor1, dif, zo);
    
    
    float zo2 = clamp((xfade - 0.75) * 4.0, 0.0, 1.0);
    

    /*float dif2 = (-0.09 + (zo2 * (1 + 0.18))) - (uv.x + 0.09 * cos((zo2 * 2.0 + uv.y) * 2.0 * 3.141592 * 0.5)); 
    if (dif2 > 0.0)
    {
      texColor.a = 0.0;
    }*/   
    float dd = length((uv - vec2(0.5, 0.5)) * vec2(1.333,1.0));
    
    float angle = atan(uv.y - 0.5, uv.x - 0.5);
    
    float dif2 = zo2 - (dd + (0.5 + 0.5 * cos(zo2 * 20.0 + 7.0 * angle)) * mix(0.1, 0.4, zo2));
    
    float w2 = 0.005;
    float m2 = 3.0;
    if (dif2 > -w2)
    {
      if (dif2 > 0.0)
        texColor.a = 0.0;

       if (dif2 < 0.0)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (w2 - dif2 / w2), 0.0, 1.0));       
       else if (dif2 < w2)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (dif2 / w2), 0.0, 1.0));
    }           
                
  }
  else if (fadetype == 4)
  {
    float zo = max(0.0, min(1.0, (xfade - 0.25) * 2.0)); 
    
    
    float dif2 = zo - (uv.x + 0.03 * sin((zo * 2.0 + uv.y) * 2.0 * 3.141592 * 3.0) - 0.005 * cos((-zo * 2.0 + uv.y) * 2.0 * 3.141592 * 8.0)); 

    float w2 = 0.003 * pow(zo, 0.1);
    float m2 = 1.0;    
    if (dif2 > -w2)
    {
      if (dif2 > 0.0)
        texColor = texColor1;

       if (dif2 < 0.0)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (w2 - dif2 / w2), 0.0, 1.0));       
       else if (dif2 < w2)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (dif2 / w2), 0.0, 1.0));
    }             
  }  
  else if (fadetype == 5)
  {
    float zo = max(0.0, min(1.0, (xfade - 0.25) * 4.0)); 
    
    /*float dif = (-0.07 + (zo * (1 + 0.14))) - (uv.y + 0.07 * sin((zo * 2.0 + uv.x) * 2.0 * 3.141592 * 0.5)); 
    if (dif > 0.0)
    {
      texColor = texColor1;
    }*/
    
    float dd = length((uv - vec2(0.5, 0.5)) * vec2(1.333,1.0));
    /*if (zo > dd)
    {
      texColor = texColor1;
    }     */
    
    float dif2 = zo - dd; 
    
    float w2 = 0.003 * pow(zo, 0.1);
    float m2 = 1.0;
    if (dif2 > -w2)
    {
      if (dif2 > 0.0)
        texColor = texColor1;

       if (dif2 < 0.0)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (w2 - dif2 / w2), 0.0, 1.0));       
       else if (dif2 < w2)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (dif2 / w2), 0.0, 1.0));
    }           
               
  }
  else if (fadetype == 55)
  {
    float zo = max(0.0, min(1.0, (xfade) * 1.5)); 
    
    float dd = length((uv - vec2(0.5, 0.5)) * vec2(1.333,1.0));

    float dif2 = zo - dd; 
    
    float w2 = 0.003 * pow(zo, 0.1);
    float m2 = 1.0;
    if (dif2 > -w2)
    {
      if (dif2 > 0.0)
        texColor = texColor1;

       if (dif2 < 0.0)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (w2 - dif2 / w2), 0.0, 1.0));       
       else if (dif2 < w2)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (dif2 / w2), 0.0, 1.0));
    }           
               
  }       
  else if (fadetype == 555)
  {
    float zo = max(0.0, min(1.0, (xfade) * 2.0)); 
    
    float dd = length((uv - vec2(0.5, 0.5)) * vec2(1.333,1.0));

    float dif2 = zo - dd; 
    
    float w2 = 0.003 * pow(zo, 0.1);
    float m2 = 1.0;
    if (dif2 > -w2)
    {
      if (dif2 > 0.0)
        texColor = texColor1;

       if (dif2 < 0.0)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (w2 - dif2 / w2), 0.0, 1.0));       
       else if (dif2 < w2)
        texColor = mix(texColor, vec4(0,0,0,1), m2 * clamp(1.0 - (dif2 / w2), 0.0, 1.0));
    }           
               
  }
  else if (fadetype == 6)
  {
    float amp = 0.015;
    float freq = 0.0 + 5.0 * (1.0 - xfade) + 10.0 * pow(uv.y,6.0);
    float phase = 2.0 - xfade * 23.5;
    float width = 0.10  + 0.01 * pow(uv.y,3.0);
    float ratio = uv.x -1.0 + 2.0 * (xfade - 0.25) * 2.5 -0.5 + 0.5 * uv.y;
    
    float md1 = mod(uv.y + amp * sin(phase + freq * uv.x * 2.0 * 3.14), width) / width;
    
    float md = md1;
    
    md -= 0.5;
    
    float it = 1.0;
    float dif = abs(md) - ratio * 0.5;
    
    float feather = 0.03;
    if (dif > -feather && dif < feather)
    {
      dif += feather;
      dif /= (2.0 * feather);
    }
    else if (dif <= -feather)
    {
      dif = 0.0;
    }
    else if (dif >= feather)
    {
      dif = 1.0;
    }
    texColor = mix(texColor1, texColor0, dif);
  }
  else if (fadetype == 66)
  {
    texColor = mix(texColor0, texColor1, clamp(xfade * 4.0, 0.0, 1.0));
  }      
  else if (fadetype == 7)
  {
    float amp = 0.015;
    float freq = 0.0 + 5.0 * (1.0 - xfade) + 10.0 * pow(uv.y,6.0);
    float phase = 2.0 - xfade * 23.5;
    float width = 0.10  + 0.01 * pow(uv.y,3.0);
    float ratio = uv.x -1.0 + 2.0 * (xfade - 0.25) * 2.5 -0.5 + 0.5 * uv.y;
    
    float md1 = mod(uv.x + amp * sin(phase + freq * uv.y * 2.0 * 3.14), width) / width;
    float md2 = mod(uv.y + amp * sin(phase + freq * uv.x * 2.0 * 3.14), width) / width;
    
    float md = min(md1, md2);
    
    md -= 0.5;
    
    float it = 1.0;
    float dif = abs(md) - ratio * 0.5;
    
    float feather = 0.03;
    if (dif > -feather && dif < feather)
    {
      dif += feather;
      dif /= (2.0 * feather);
    }
    else if (dif <= -feather)
    {
      dif = 0.0;
    }
    else if (dif >= feather)
    {
      dif = 1.0;
    }
    texColor = mix(texColor1, texColor0, dif);

                                            
    /*if (mod(uv.y + amp * sin(phase + freq * uv.x * 2.0 * 3.14), width) < width * ratio)
    {
      texColor = texColor1;
    }*/
     
  }
  else if (fadetype == 8)
  {
    texColor = mix(texColor0, texColor1, clamp(xfade * 2.0, 0.0, 1.0));
  }         
  else if (fadetype == 9)
  {
    texColor = mix(texColor0, texColor1, clamp(xfade, 0.0, 1.0));
  }         
  
  
  else if (fadetype == 0)
  {
    if (xfade > uv.x)
    {
      texColor = texColor1;
    }
  }
  else
  {
    float amp = 0.015;
    float freq = 0.0 + 5.0 * (1.0 - xfade) + 50.0 * pow(uv.y,6.0);
    float phase = 2.0 - xfade * 23.5;
    float width = 0.04  + 0.01 * pow(uv.y,3.0);
    float ratio = uv.x -1.0 + 2.0 * xfade -0.5 + 0.5 * uv.y;
    
    float md = mod(uv.y + amp * sin(phase + freq * uv.x * 2.0 * 3.14), width) / width;
    
    md -= 0.5;
    
    float it = 1.0;
    float dif = abs(md) - ratio * 0.5;
    
    float feather = 0.03;
    if (dif > -feather && dif < feather)
    {
      dif += feather;
      dif /= (2.0 * feather);
    }
    else if (dif <= -feather)
    {
      dif = 0.0;
    }
    else if (dif >= feather)
    {
      dif = 1.0;
    }
    texColor = mix(texColor1, texColor0, dif);

                                            
    /*if (mod(uv.y + amp * sin(phase + freq * uv.x * 2.0 * 3.14), width) < width * ratio)
    {
      texColor = texColor1;
    }*/
     
  }
  
  /*if (vertexFade > 0.0 && d > 1.0 - vertexFade)
    texColor.a = 0.0;*/
  
  texColor.a *= alphaMask;
  
  if (texColor.a < 0.005) discard;
  
  //texColor.a = pow(texColor.a, 0.3);
  
  //if (vPosition.z > 0.5) discard;

  output = vec4(mix(texColor.rgb, vec3(1.0), pow(vertexFade, 4.0)), texColor.a) * color;
  
  //output = vec4(1,xfade,1,1);
                  
  vec2 a = (vPosition.xy / vPosition.w) * 0.5 + 0.5;
  vec2 b = (vPrevPosition.xy / vPrevPosition.w) * 0.5 + 0.5;
  vec2 velocity = a - b;
    
  motion = vec4(velocity * 0.5 + 0.5,0,1);
  
  //output = texColor;
  
  //output = vec4(1,0,0,1);
  
  
  /*float ndcDepth = 
    (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
    (gl_DepthRange.far - gl_DepthRange.near);
  float clipDepth = ndcDepth / gl_FragCoord.w;


  output = vec4(clipDepth, clipDepth, clipDepth, 1.0);*/       
  
}