• Advertisement
Sign in to follow this  

Normal Mapping black spots problem

This topic is 1733 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

Hey guys, I'm having a small problem with my normal mapping.

Here's how it looks:

http://d.pr/i/yszc

 

Now when I comment out this line here (computing of the z component instead of reading it from the file):

	// Sample the normal map, and convert the normal to world space
	normalTS.xyz = NormalMap.Sample(AnisotropicSampler, input.UV).xyz * 2.0f - 1.0f;
	//normalTS.z = sqrt(1.0f - ((normalTS.x * normalTS.x) + (normalTS.x * normalTS.y)));
	normalWS = mul(normalTS, tangentToWorld);
	normalWS = normalWS * rsqrt(dot(normalWS, normalWS));

 

It seems to look fine:

http://d.pr/i/UokG

 

Why is that ? Does the z reconstruction only work for 3Dc compressed normal maps ? But shouldn't this algorithm work for anything ?

Well looking at it in PIX it seems to generate NaN.

 

normalTS ( 0.811, 0.685, -1.#IO ) float3
 
So basically it's doing a sqrt(something negative). What am I doing wrong ?
Edited by lipsryme

Share this post


Link to post
Share on other sites
Advertisement

normalWS = normalWS * rsqrt(dot(normalWS, normalWS));

seems like a very long winded way of saying

normalWS = normalize(normalWS);

Unless you've got a specific reason for the former, I'd just use the explicit normalize version.

 

Why is that ? Does the z reconstruction only work for 3Dc compressed normal maps ?

Wait, are you actually using 3Dc/BC5/ATI2? If so, it's a two-channel format, so there will only be x/y values and no z values, so yes, you'd have to reconstruct z.

If not, can you post your texture file?

Share this post


Link to post
Share on other sites

You've made a small mistake:

normalTS.z = sqrt(1.0f - ((normalTS.x * normalTS.x) + (normalTS.x * normalTS.y)));

 

should be:

normalTS.z = sqrt(1.0f - ((normalTS.x * normalTS.x) + (normalTS.y * normalTS.y)));
//normalTS.z = sqrt(1.0f - (dot(normalTS.xy, normalTS.xy)));

Share this post


Link to post
Share on other sites
 
If your  x and y components are (0.811, 0.685) then the 1-(x*x + y*y) will be negative (1 - 1.1269).
 
Just an idea : have you normalized your mipmaps? 
 
Cheers!
Edited by kauna

Share this post


Link to post
Share on other sites

Um I don't think so...how would I do that ?

Using SampleLevel and going through every mip map and normalizing it ?

 

normalTS seems fine:

http://d.pr/i/pGwC

 

Here's the complete PS code for the normal mapping:

VS code is just transforming to world space and normalizing it.

	// Normal Mapping
	float3 normalWS = input.Normal * rsqrt(dot(input.Normal, input.Normal)); // normalize
	float3 tangentWS = input.Tangent * rsqrt(dot(input.Tangent, input.Tangent)); // normalize
	float3 biTangentWS = cross(input.Normal, input.Tangent);

	float3 normalTS = float3(0, 0, 1);
	float3x3 tangentToWorld = float3x3(tangentWS, biTangentWS, normalWS);

	// Sample the normal map, and convert the normal to world space
	normalTS.xyz = NormalMap.Sample(AnisotropicSampler, input.UV).xyz * 2.0f - 1.0f;
	normalTS.z = sqrt(1.0f - ((normalTS.x * normalTS.x) + (normalTS.y * normalTS.y)));
	normalWS = mul(normalTS, tangentToWorld);
	normalWS = normalWS * rsqrt(dot(normalWS, normalWS)); // normalize
Edited by lipsryme

Share this post


Link to post
Share on other sites

Typically the normal maps are normalized in image editing software such as Photoshop, and if your normal map works fine elsewhere, then there shouldn't be problem with the normal map. 

 

Why don't you use normalize() instead of dots and rsqrts etc? If not anything else, it makes the code and your intentions easier to read. 

 

Have you tried to change the filtering to bilinear / none?

 

Cheers!

Share this post


Link to post
Share on other sites

You mean like so ?

http://d.pr/i/Z5Jo

 

Didn't seem to help.

And yes I've tried linear sampling, also no help.

 

UPDATE: saturate made it a bit better, still black spots there when comparing it to just sampling xyz from the normal map.

I'm gonna do that for now, if anyone has an idea please go ahead and tell me tongue.png

Edited by lipsryme

Share this post


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

  • Advertisement