# Normal mapping dependent on view.

This topic is 2135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I've got a problem that normal mapping is dependent on view. It looks like this (sorry for the watermarks):

The normal mapping is calculated in the pixel shader as follows (it is actually taken from http://stackoverflow.com/questions/5255806/how-to-calculate-tangent-and-binormal):

- vertexPosition is a position in SV_POSITION semantic

- shared vertices on in the triangles

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

// in main function
// ...
input.normal = computeNormalWithTBN( input.position.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;
}

Why this happens?

Thanks for help.

##### Share on other sites

What coordinate system (aka "space") is the normal in?

In the vertex shader how do you generate the normal?

Your light direction is constant (i.e. float3( 0, 0.73, -0.69 )), so I guess it's in world-space / world-coordinates.

##### Share on other sites

Hey, thanks for the interest.

I think it's in world space, but I will explain how it is calculated to not make a stupid mistake.

The normal is calculated on CPU and is send to the VS. To calculate normal for the "5" vertex:

1 2 3
4 5 6
7 8 9

I calculate:

XMFLOAT3 normal;
XMVECTOR v1 = {  2, height_value_of_3 - height_value_of_7, -2 };
XMVECTOR v2 = { -2, height_value_of_1 - height_value_of_9, -2 };
XMStoreFloat3( &normal, XMVector3Normalize( XMVector3Cross( v1, v2 ) ) );
return normal;

VS only passes the normals to the PS as is.

• 18
• 19
• 11
• 21
• 16