varying vec3 ViewPos;
varying vec4 projPos;
varying vec4 lightPos;

uniform sampler2D Color0;   // scene color
uniform sampler2D Color1;   // radial mask
uniform sampler2D Position; // for soft particle effect

void main()
{
  // read mask
  vec3 mask = texture2D( Color1, gl_TexCoord[0].st ).rgb;

  // big conditional, maybe not helpful?
  if( (mask.r + mask.g + mask.b) > 0.001 )
  { 
    // get current texture pos
    vec2 texCoordAt     = projPos.xy  / projPos.w  * 0.5 + 0.5;
    vec2 texCoordCenter = lightPos.xy / lightPos.w * 0.5 + 0.5;

    // get offset, scale
    float density = 1.0;
    vec2 offset = texCoordAt - texCoordCenter;
    offset /= ( 8.0 * density );//16.0 * density );//
    
    float extinction = 0.25;//18.0/9.0;//
    float extinctionFactor = 0.95;
    
    // sample along offset
    vec3 color = extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
/*
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
    texCoordCenter += offset; extinction *= extinctionFactor;
    color += extinction * texture2D( Color0, texCoordCenter ).rgb;
*/
    //color *= color;
    //color *= mask;
  /*  
    float exposure = 1.5;//0.50;//
    float cluma = dot( color, vec3(0.299, 0.567, 0.114));
    float mluma = dot( mask,  vec3(0.299, 0.567, 0.114));
    color *= (exposure) / (cluma + 1.0);//(mluma * mluma);
    color *= mask;
*/
    float exposure = 0.75;//1.0;//0.5;
    color *= 0.2 * mask.b;
    color *= exposure;


    // attenuate based on scene
    //const float minDepth = 10.0;
    float bbDepth = length( ViewPos );
    float sDepth = texture2D( Position, texCoordAt ).w;
    float dDepth = sDepth - bbDepth;
    //float att = dDepth / minDepth; // converts attenuation to [0, some pos value]
    //att = min( att, 1.0 ); // clamp to [0,1]
    float att = smoothstep(0.0, 10.0, dDepth);
    color *= att; // attenuate as approaches scenery

    gl_FragColor = vec4( color, 0.0 );
  }
  else 
    gl_FragColor = vec4( 0.0, 0.0, 0.0, 0.0 );
}
