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

uniform sampler2D Color0; // celshadey
uniform sampler2D Color1; // ground
uniform sampler2D Normal; // normal map with bump!

uniform sampler2D SceneLight;
uniform sampler2D SceneNormal;

const float scale = 1.0;

void main()
{
  // parallax bump
  float height = scale * texture2D( Normal, gl_TexCoord[0].st ).r;

  // offset texture look up
  vec2 texCoord = gl_TexCoord[0].st + height * taneye.xy;

  // look up into scene
  vec2 texScene = projPos.xy / projPos.w * .5 + .5;

  vec4 light = texture2D( SceneLight, texScene );

  // compute normal for cel-shading like
  vec4 buff = texture2D( SceneNormal, texScene );

  vec3 norm = 2.0 * buff.xyz - 1.0;

  // color is offset
  vec3 color = texture2D( Color1, texCoord ).rgb;

  // weird lighting thing
  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 ) * texCoord.y * 0.9+ 0.1;
 
  // modulate with texture color
  color *= texture2D( Color0, texCoord ).rgb;
  
  //float p = 1.0; // plasticity
  //light.a *= 2.54;
  vec3 lightColor = light.rgb + light.a;// * (( 1.0 - p ) * light.rgb + vec3( p, p, p ));
  color *= lightColor;

  gl_FragColor = vec4( color, 1.0 );
}