uniform sampler2D Normal; // eye space position
uniform sampler2D Color1;   // rain color

varying vec3 lightPos;
varying vec3 eyePos;
varying vec3 lightColor;
varying vec4 projPos;
varying vec3 normal;

varying float invRadius;
varying float extraParam;

const vec2 bitSh = vec2(1.0 / 256.0, 1.0);

uniform float Timer;

void main()
{

  // noise effect
  
  vec2 t = gl_TexCoord[0].st;		
	float x=t.x*t.y*123456.0 * Timer * 0.001;
	x=mod(x,13.0) * mod(x,123.0);	
	float dx = mod(x,0.01);
	float dy = mod(x,0.12);
	t+= vec2( dx,dy );
  

  vec2 texCoord = projPos.xy / projPos.w * .5 + .5;
  
  // normalize eye vector
  vec3 e = normalize( eyePos );

  vec4 buff = texture2D( Normal, texCoord );
  
  // recover position from gbuffer
  float depth = buff.w;
  vec3 objPos = e * depth;
  
  // distance from light to geometry
  vec3 l = objPos - lightPos;
  float attenuation = length( l ) * invRadius; // radius is 1 over radius
  // discard if outside radius
  attenuation = 0.95 - attenuation;

  //if( attenuation < 0.0 ) discard;

  attenuation = max( attenuation, 0.0 );
  // quadric vs linear fall off
  attenuation *= attenuation;//

  //attenuation = 0.1;
  
  // unpack normal
  vec3 norm = 2.0 * buff.xyz - 1.0;
  
  // standard ndotl
  l = normalize( l );
  float ndotl = max( dot( norm, -l ), 0.0 );

  vec3 h = -normalize( e + l );
  float ndoth = max( dot( norm, h ), 0.0 );

  float spec = pow( ndoth, 32.0 );
  spec *= 0.33 * ndotl;
/*
  // apply! factor of 2 to push the light
  vec3 color = ( attenuation * ndotl ) * lightColor;// + spec;//vec3( spec, spec, spec );
  color *= 2.54;
  spec  *= 2.54 * attenuation;

  gl_FragColor = vec4( color, spec );
  */

  // unpack color
  vec3 color = texture2D( Color1, gl_TexCoord[0].st + t ).rgb;

  float rainColor = color.r;
  vec2  texOffset = color.gb;

  //rainColor += 0.8 * texture2D( Color1, texOffset ).r;

  // apply! factor of 2 to push the light
  color = rainColor * ( attenuation * ndotl ) * lightColor + spec;//vec3( spec, spec, spec );
  color *= 2.54;

  float ndote = dot( normal, -e );
  ndote*=ndote;

  gl_FragColor = vec4( ndote, ndote, ndote, 1.0 );//vec4( color, 1.0 );//vec4( 1.0, 0.0, 1.0, 0.1 );//
}
