uniform sampler2D texture;
uniform sampler2D texture_rays;
uniform sampler2D texture_normals;

uniform float timer;

void main()
{
    //vec4 color = vec4(texture2D(texture,gl_TexCoord[0].xy-vec2(0.005,0.0)).x, texture2D(texture,gl_TexCoord[0].xy+vec2(0.005,0.0)).y, texture2D(texture,gl_TexCoord[0].xy).z, 1.0);
    //vec4 color = texture2D(texture,vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/5.0))/40.0, gl_TexCoord[0].y));
    //vec4 color = texture2D(texture,gl_TexCoord[0].xy);


    //vec2 TexCoord = vec2(gl_TexCoord[0].xy);
    //vec2 TexCoord = vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/5.0))/400.0, gl_TexCoord[0].y);
    vec2 TexCoord;
    vec4 color;

    if(timer<113142.85714285714285714285714286){
        TexCoord = vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/5.0))/400.0, gl_TexCoord[0].y);
        color = vec4(texture2D(texture,gl_TexCoord[0].xy-vec2(0.005,0.0)).x, texture2D(texture,gl_TexCoord[0].xy+vec2(0.005,0.0)).y, texture2D(texture,gl_TexCoord[0].xy).z, 1.0);}
    else if(timer<192000.0){
        TexCoord = vec2(gl_TexCoord[0].xy);
        color = texture2D(texture,TexCoord);}
    else if(timer<210000.0){
        TexCoord = vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/(5.0+(210000-timer)/10000.0)))/400.0, gl_TexCoord[0].y);
        color = texture2D(texture,TexCoord);}
    else if(timer<222000.0){
        TexCoord = vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/5.0))/400.0, gl_TexCoord[0].y);
        color = texture2D(texture,TexCoord);}
    else if(timer<230000.0){
        TexCoord = vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/5.0))/400.0, gl_TexCoord[0].y);
        color = vec4(texture2D(texture,gl_TexCoord[0].xy-vec2(0.005*(timer-222000.0)/(230000.0-222000.0),0.0)).x, texture2D(texture,gl_TexCoord[0].xy+vec2(0.005*(timer-222000.0)/(230000.0-222000.0),0.0)).y, texture2D(texture,gl_TexCoord[0].xy).z, 1.0);}
    else{
        TexCoord = vec2(gl_TexCoord[0].x+(sin(gl_FragCoord.y/5.0))/400.0, gl_TexCoord[0].y);
        color = vec4(texture2D(texture,gl_TexCoord[0].xy-vec2(0.005,0.0)).x, texture2D(texture,gl_TexCoord[0].xy+vec2(0.005,0.0)).y, texture2D(texture,gl_TexCoord[0].xy).z, 1.0);}


    vec4 glow = vec4(0.0, 0.0, 0.0, 0.0);

    float this_normal = texture2D(texture_normals,TexCoord.xy).x;
    vec4 airlight = vec4(0.0);
    float size = 0.0006;
    float size_border = 0.001;
    int ratio = 5;
    float step = size/(ratio/2);
    float step_border = size_border/(ratio/2);

    for(int x=0;x<ratio;x++)
    {
        for(int y=0;y<ratio;y++)
        {
            if((0.004001)<abs(this_normal-texture2D(texture_normals,vec2(TexCoord.x-size_border+x*step_border, TexCoord.y-size_border+y*step_border)).x))
            {
                color-=abs(this_normal-texture2D(texture_normals,vec2(TexCoord.x-size_border+x*step_border, TexCoord.y-size_border+y*step_border)).x);
            }

            airlight += texture2D(texture_rays,vec2(TexCoord.x-size+x*step, TexCoord.y-size+y*step));
        }
    }

    float size_glow = 0.034;
    ratio = 15;
    float step_glow = size_glow/(ratio/2);
    int center = (ratio-1)/2;

    for(int x=0;x<ratio;x++)
    {
        for(int y=0;y<ratio;y++)
        {
            if(texture2D(texture_normals,vec2(TexCoord.x-size_glow+x*step_glow, TexCoord.y-size_glow+y*step_glow)).z == 1.0)
            {
                if(distance(vec2(center, center), vec2(x, y))==0)
                {
                     glow += texture2D(texture,vec2(TexCoord.x-size_glow+x*step_glow, TexCoord.y-size_glow+y*step_glow));
                }else
                {
                    glow += texture2D(texture,vec2(TexCoord.x-size_glow+x*step_glow, TexCoord.y-size_glow+y*step_glow))/distance(vec2(center, center), vec2(x, y));
                }
            }
        }
    }


    vec4 fog_color = vec4(0.01, 0.01, 0.02, 1.0);
    if(texture2D(texture_normals,TexCoord.xy).y == 0.0)
    {
        color = fog_color;
    }
    else if(this_normal>0.85)
    {
        color = (1.0-this_normal)*(1.0/0.15)*color + (this_normal-0.85)*(1.0/0.15)*fog_color;
    }

    color += glow/75.0;

    airlight /= 20;
    //airlight = texture(texture_rays,TexCoord.xy).x;
    gl_FragColor = (color+airlight);//*0.5 + vec4(TexCoord.x, TexCoord.y, TexCoord.y+ TexCoord.x, 1.0)*0.5;
}
