varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;
varying vec3 WorldNormal;
varying float pos;

uniform sampler2D Normal; // normal map

void main()
{
  vec3 norm;
  
  // scale blending
  vec3 Nscreen = abs( normalize(WorldNormal).xyz ); 
  Nscreen = ( Nscreen - .2 ) * 7.0;
  Nscreen = max( Nscreen, 0.0 );
  Nscreen = Nscreen / ( Nscreen.x + Nscreen.y + Nscreen.z );

  vec3 tangentspacenormal = texture2D(Normal, gl_TexCoord[0].zy).xyz * Nscreen.x +
                            texture2D(Normal, gl_TexCoord[0].xz).xyz * Nscreen.y +
                            texture2D(Normal, gl_TexCoord[0].yx).xyz * Nscreen.z;

  vec3 t1 = normalize(tangent);
  vec3 b1 = normalize(binormal);
  vec3 n1 = normalize(normal);
  norm.x = dot(vec3( t1.x, b1.x, n1.x ), tangentspacenormal);
  norm.y = dot(vec3( t1.y, b1.y, n1.y ), tangentspacenormal);
  norm.z = dot(vec3( t1.z, b1.z, n1.z ), tangentspacenormal);
  norm = normalize(norm);

  gl_FragColor = vec4(norm * 0.5 + 0.5, pos);
}