varying vec3 ViewPos;
varying vec3 up;
varying vec4 projPos;

uniform sampler2D Color0; // celshadey

uniform sampler2D SceneLight;
uniform sampler2D SceneNormal;

void main()
{
  vec2 texScene = projPos.xy / projPos.w * .5 + .5;

  // compute normal for cel-shading like
  vec4 buff = texture2D( SceneNormal, texScene );
  vec3 norm = 2.0 * buff.xyz - 1.0;
  vec4 light = texture2D( SceneLight, texScene );

  // weird lighting thing
  vec2 texCoord;
  vec3 e = normalize( ViewPos );
  // v coord is normal dot up for a lighting like effect
  texCoord.y = max( dot( norm, up ), 0.0 ) * 0.45 + 0.5;
  // u coord is normal dot eye for cel-shading like effect
  texCoord.x = max( dot( norm, -e ), 0.0 ) * 0.45 + 0.5;
 
  // modulate with texture color
  vec3 color = texture2D( Color0, texCoord ).rgb;
  vec3 lightColor = light.rgb + light.a;
  color *= lightColor;
  
  gl_FragColor = vec4( color, 1.0 );
}