//varying vec4 ViewPos;
varying vec3 normal;
varying vec3 tangent;
varying vec3 binormal;
varying vec3 WorldNormal;
varying float pos;

varying vec3 Nscreen;

uniform sampler2D Height; // mix
uniform sampler2D Normal; // normal map

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

//const float scale = 0.2;
//const float bias  = -0.2;
void main()
{
  vec3 norm;
  //vec3 e = -normalize( ViewPos.xyz );

  // 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 );

  /*vec2 xprojscale = 0.112233 * gl_TexCoord[0].st;
  vec2 yprojscale = 0.112233 * gl_TexCoord[1].st;
  vec2 zprojscale = 0.112233 * gl_TexCoord[2].st;*/

  vec3 tangentspacenormal = (texture2D( Normal, gl_TexCoord[0].st /*+ offsetx*/ ).xyz * 2.0 - 1.0) * Nscreen.x +
                            (texture2D( Normal, gl_TexCoord[1].st /*+ offsety*/ ).xyz * 2.0 - 1.0) * Nscreen.y +
                            (texture2D( Normal, gl_TexCoord[2].st /*+ offsetz*/ ).xyz * 2.0 - 1.0) * Nscreen.z;
  tangentspacenormal = vec3(0.0, 0.0, 1.0);//normalize(tangentspacenormal);
  // tangentspace scaled... magic number here
  //tangentspacenormal = tangentspacenormal * 0.86 - 1.0;

  // // normal ///////////////////////////////////////////////////////////////////////////////////////
  //vec3 h;
  //h.x = 1.0 - texture2D( Height, gl_TexCoord[0].st ).x * Nscreen.x;
  //h.y = 1.0 - texture2D( Height, gl_TexCoord[1].st ).x * Nscreen.y;
  //h.z = 1.0 - texture2D( Height, gl_TexCoord[2].st ).x * Nscreen.z;
  //h *= scale;
  //h += bias;
  //
  //vec2 offsetx = h.x * e.yz;//vec2( 0.0 );//
  //vec2 offsety = h.y * e.xz;//vec2( 0.0 );//
  //vec2 offsetz = h.z * e.yx;//vec2( 0.0 );//

  //vec2 xprojscale = 0.112233 * gl_TexCoord[0].st;
  //vec2 yprojscale = 0.112233 * gl_TexCoord[1].st;
  //vec2 zprojscale = 0.112233 * gl_TexCoord[2].st;

  //vec3 tangentspacenormal = (texture2D( Normal, xprojscale        + offsetx/**/ ).xyz * 2.0 - 1.0) * Nscreen.x +
  //                          (texture2D( Normal, yprojscale        + offsety/**/ ).xyz * 2.0 - 1.0) * Nscreen.y +
  //                          (texture2D( Normal, zprojscale        + offsetz/**/ ).xyz * 2.0 - 1.0) * Nscreen.z;// +
  //                          //(texture2D( Normal, gl_TexCoord[0].st + offsetx/**/ ).xyz * 2.0 - 1.0) * Nscreen.x +
  //                          //(texture2D( Normal, gl_TexCoord[1].st + offsety/**/ ).xyz * 2.0 - 1.0) * Nscreen.y +
  //                          //(texture2D( Normal, gl_TexCoord[2].st + offsetz/**/ ).xyz * 2.0 - 1.0) * Nscreen.z;
  //tangentspacenormal = normalize(tangentspacenormal);
  
  /* normalized ver */
  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 );
  
    // position //////////////////////////////
  // compute linear eye depth
  //float pos = length( ViewPos.xyz );//-ViewPos.z;

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