Jump to content
  • Advertisement
Sign in to follow this  

Problem normalmap and shader

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello everyone,

I have a problem with a normal map, I have a heightmap:


and normalmap:


So I use the RGB channels to define the normal at a pixel, but it doesn't work.
Here is my shader code:

Texture2D t:register(t0);
Texture2D normalmap:register(t1);
SamplerState s:register(s0);

cbuffer ConstantBuffer{
Matrix mFull;
float4 lightDirection;
float4 ambientColor;
float4 diffuseColor;
float2 sizeMap;

float4 PSmain( PSin Pin ) : SV_Target {
float4 color;
float4 nMap=normalmap.Sample(s,Pin.tex);
float3 lightDir;
float lightIntensity;
// Set the default output color to the ambient light value for all pixels.
color = ambientColor;
// Invert the light direction for calculations.
lightDir = -lightDirection;
// Calculate the amount of light on this pixel.
lightIntensity = saturate(dot(nMap.xyz, lightDir));

if(lightIntensity > 0.0f)
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
color += (diffuseColor * lightIntensity);
// Saturate the final light color.
color = saturate(color);
return color;

Share this post

Link to post
Share on other sites

Can you elaborate on what your current result is? (What’s wrong with it? What happens / does not happen?)

Normal maps store the normals in tangent space. Usually you have to transform the normals by the TBN matrix from tangent space to object space and then via world matrix to world space before you can do any lighting calculations. The TBN (=tangent, binormal, normal) matrix defines a coordinate system at each vertex. You can precompute the tangent and include it (along with your normal) in the vertex buffer or you can compute it on-the-fly.

Besides, normal maps store the normals usually with a scale-bias.
So the tangent space normal would be:
float3 nMap=normalmap.Sample(s,Pin.tex).rgb * 2.0 – 1.0;


Share this post

Link to post
Share on other sites
In my opinion, In this particular case the tangent space and the world space are the same. For that reason you don't have to worry about the tangent space and you don't need the TBN matrix. If you had a detail normal map which would repeat over the terrain, you'd need to calculate the TBN matrix.

Just read the normal from the texture as suggested by Tsus and you should be fine.

Best regards!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!