precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform float time;
uniform vec2      resolution;
uniform vec2      mouse;

const float PI = 3.14159265;
float ptime = time * 0.1;


float pixel_noise(vec2 pos) {
	float ntime = floor(time*10.0);// * 0.0001;
    return fract(sin(dot(pos, vec2(13.42-ntime,20.232+ntime))) * 10000.0);
}

void DoRain()
{
    vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y  );
   
    float sum = pixel_noise(vec2(floor(pos.x/3.0),floor(pos.y/3.0)));

    if(gl_FragCoord.y < 150.0)
    {
    	sum = 0.8 + (sum*0.2);
    }
    else
    {
    	sum = 1.0;
    }
    
    vec4 col = vec4(texture2D(uSampler, vTextureCoord));
    gl_FragColor = vec4(col.r*sum,col.g*sum,col.b*sum, 0);//col.a * sum / 100.0);
}



   
float noise(vec2 pos) {
    return fract(sin(dot(pos, vec2(12.9898 - time,78.233 + time))) * 43758.5453);
}


void DoStreetLights(void)
{
    float color1, color2, color;
    float size = 0.03;
    vec2 pos = vec2(gl_FragCoord.x , gl_FragCoord.y  );

    color1 = (sin(dot(pos.xy, vec2(sin(ptime * 3.0), cos(ptime * 3.0))) * 0.02 + ptime * 3.0) + 1.0) / 2.0;
    vec2 center = vec2(resolution.x / 2.0, resolution.y / 2.0) + vec2(640.0 / 2.0 * sin(-ptime * 3.0), 360.0 / 2.0 * cos(-ptime * 3.0));
    color2 = (cos(length(pos.xy - center) * size) + 1.0) / 2.0;
    color = (color1 + color2) / 2.0;

    //vec3 colorShift = vec3(0.5,0.5,0.5);
    //float red = (cos(PI * color / colorShift.r + ptime * 3.0) + 1.0) / 2.0;
    //float green = (sin(PI * color / colorShift.g + ptime * 3.0) + 1.0) / 2.0;
    //float blue = (sin(PI * color / colorShift.b + ptime * 3.0) + 1.0) / 2.0;
	//float sum = (red + green + blue ) /3.0;
    
    float sum = (cos(PI * color / 0.5 + ptime * 3.0) + 1.0) / 2.0;
     //gl_FragColor = vec4(red,red,red,1);return;

    //--------------
    sum = 0.8 + (sum*0.2);
    vec4 col = vec4(texture2D(uSampler, vTextureCoord));
    gl_FragColor = vec4(col.r*sum,col.g*sum,col.b*sum, 0);//col.a * sum / 100.0);

}


void DoFog(void)
{
    float color1, color2, color;
    float size = 0.03;
    vec3 colorShift = vec3(0.5,0.5,0.5);

    color1 = (sin(dot(gl_FragCoord.xy, vec2(sin(ptime * 3.0), cos(ptime * 3.0))) * 0.02 + ptime * 3.0) + 1.0) / 2.0;
    vec2 center = vec2(640.0 / 2.0, 360.0 / 2.0) + vec2(640.0 / 2.0 * sin(-ptime * 3.0), 360.0 / 2.0 * cos(-ptime * 3.0));
    color2 = (cos(length(gl_FragCoord.xy - center) * size) + 1.0) / 2.0;
    color = (color1 + color2) / 2.0;

    float red = (cos(PI * color / colorShift.r + ptime * 3.0) + 1.0) / 2.0;
    float green = (sin(PI * color / colorShift.g + ptime * 3.0) + 1.0) / 2.0;
    float blue = (sin(PI * color / colorShift.b + ptime * 3.0) + 1.0) / 2.0;
    gl_FragColor = vec4(red, green, blue, 1.0);	

    //--------------
    vec4 col = vec4(texture2D(uSampler, vTextureCoord));
    float y = gl_FragCoord.y / (720.0);
    if( y < 0.33)
    {
	    float sum = (red + green + blue ) /3.0;
	 
	    float factor = 1.0 - (y)/0.33;
	    gl_FragColor = col + vec4(sum,sum,sum, 0.0) * factor * 0.25; 
    }
    else
    {
    	gl_FragColor = col;
    }
}
void DoGlow2(void)
{
	vec4 sum = vec4(0);
	vec2 texcoord = vTextureCoord;
	for(int xx = -4; xx <= 4; xx++) {
		for(int yy = -3; yy <= 3; yy++) {
			float dist = sqrt(float(xx*xx) + float(yy*yy));
			float factor = 0.0;
			if (dist == 0.0) {
				factor = 2.0;
			} else {
				factor = 2.0/abs(float(dist));
			}
			sum += texture2D(uSampler, texcoord + vec2(xx, yy) * 0.002) * factor;
		}
	}

	sum *= 0.015;

	vec4 col = vec4(texture2D(uSampler, texcoord));
	float redness = sum.r - (sum.g + sum.b)*0.5;
	//float avg = (sum.r + sum.g + sum.b)/3.0;
	float avg = clamp(redness, 0.0,1.0)*5.0;
	gl_FragColor = sum * avg + col;

	

}

void DoGlow(void)
{
	vec4 sum = vec4(0);
	vec2 texcoord = vTextureCoord;
	for(int xx = -4; xx <= 4; xx++) {
		for(int yy = -3; yy <= 3; yy++) {
			float dist = sqrt(float(xx*xx) + float(yy*yy));
			float factor = 0.0;
			if (dist == 0.0) {
				factor = 1.0;
			} else {
				factor = 1.0/abs(float(dist));
			}
			sum += texture2D(uSampler, texcoord + vec2(xx, yy) * 0.002) * factor;
		}
	}
	vec4 col = vec4(texture2D(uSampler, texcoord));
	gl_FragColor = sum * 0.015 + texture2D(uSampler, texcoord);
/*	
	if(sum.r > sum.g*1.5 || sum.r  >sum.b*1.5)
	{
		//gl_FragColor = vec4(0.0); 
		gl_FragColor = sum * 0.055 * (1.0 + sin(time*5.0)/2.0) + col;
	}
	else
	{
		gl_FragColor = col;
	}
*/

	

}


void DoGreenbars(void)
{
       vec2 normalPos = gl_FragCoord.xy / resolution.xy;
        float pos = (gl_FragCoord.y / resolution.y);
        float distortion = 1.0;
        pos -= (distortion * distortion) * 0.1;

        float c = sin(pos * 400.0) * 0.4 + 0.4;
        c = pow(c, 0.2);
        c *= 0.2;

        float band_pos = fract(time * 0.1) * 3.0 - 1.0;
        c += clamp( (1.0 - abs(band_pos - pos) * 10.0), 0.0, 1.0) * 0.1;

        c += distortion * 0.08;
        // noise
        c += (noise(gl_FragCoord.xy) - 0.5) * (0.09);

        vec4 baseColor = texture2D(uSampler, vTextureCoord);
        gl_FragColor = vec4( baseColor.r, baseColor.g * c, baseColor.b, 1.0 );
}

void DoNoise()
{
        vec4 baseColor = texture2D(uSampler, vTextureCoord);
        baseColor.rgb *= noise(gl_FragCoord.xy);
        gl_FragColor = baseColor;
}

//----------------------------------------------------
void DoContrast(void)
{
	float contrast1 = 1.5;
	float contrast2 = 0.5;
	//float treshold = (1.0 + sin(time/10.0))/2.0;
	float treshold = 0.25;

	vec4 finColor = texture2D(uSampler, vTextureCoord);
	
	if(finColor.r > treshold) {finColor.r = finColor.r * contrast1;}
	else {	finColor.r = finColor.r * contrast2;	}
	if(finColor.g > treshold) {finColor.g = finColor.g * contrast1;	}
	else {	finColor.g = finColor.g * contrast2;	}
	if(finColor.b > treshold) {finColor.b = finColor.b * contrast1;	}
	else {	finColor.b = finColor.b * contrast2;	}

	//finColor = vec4(finColor.x * 0.5,finColor.y,finColor.z,1);
	gl_FragColor = finColor;	
}
//----------------------------------------------------
void DoHilights(void)
{
	float treshold = 0.5;
	float t = (1.0 + sin(time))/2.0;

	vec4 finColor = texture2D(uSampler, vTextureCoord);
	
	if(finColor.r > treshold && finColor.g > treshold && finColor.b > treshold)
	{
		finColor.r *= mix(1.0,2.0,t);
	}
	gl_FragColor = finColor;	
}


//----------------------------------------------------
void main(void)
{
 	vec4 texColor = texture2D(uSampler, vTextureCoord);
    gl_FragColor = texColor;
    DoRain();

   // gl_FragColor.rgb *= 1.0-gl_FragCoord.y/(resolution.y*3.0);

	//DoGlow();

	//DoGlow2();
	//scanlines
	//gl_FragColor = mod(gl_FragCoord.y,2.0) * texture2D(uSampler, vTextureCoord);
}
