Hello,
I have recendly added normal mapping to my terrain engine, but after diagnosing a bit, it looks like there is some problem with the binormal (it is somehow too sharp in some places). Here are some screenshots:
Full (there area visible artifacts in some places): http://img833.imageshack.us/img833/89/lzwv.png
Tangent color: http://img23.imageshack.us/img23/6396/8bs.png
Binormal color (compare it with the full screenshot): http://img543.imageshack.us/img543/3219/dyu7.png
Normal color: http://img541.imageshack.us/img541/5279/85ss.png
Why this happens? This is the code responsible for generating TBN:
- Terrain has shared vertices.
- vertexPosition below is a position not transformed by any matrix.
- normal is calculated on CPU and is send (not transformed by anything) to the VS which sends it to the PS as is.
float3 computeNormalWithTBN(float3 vertexPosition, float2 texCoord, float3 normal ) {
float3 p_dx = ddx(vertexPosition);
float3 p_dy = ddy(vertexPosition);
float2 tc_dx = ddx(texCoord);
float2 tc_dy = ddy(texCoord);
float3 t = normalize( tc_dy.y * p_dx - tc_dx.y * p_dy );
float3 b = normalize( tc_dy.x * p_dx - tc_dx.x * p_dy );
float3 n = normalize(normal);
float3 x = cross(n, t);
t = cross(x, n);
t = normalize(t);
x = cross(b, n);
b = cross(n, x);
b = normalize(b);
float4 detail = normalMap.Sample( SampleType, texCoord );
detail = (detail * 2.0f) - 1.0f;
detail *= 6.0f;
return normalize( normal + detail.x * t + detail.y * b );
}
main PS function:
// ...
input.normal = computeNormalWithTBN( input.rawPosition.xyz, input.tex.xy, input.normal;
float light = saturate( dot( input.normal, float3( 0, 0.73, -0.69 ) ) );
float4 color = 0.3f;
color += light;
return color;
}
Thank you for any hints!