• Advertisement
Sign in to follow this  

HLSL Bump Mapping Problem (Solved)

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

Advertisement
How are you wrapping the texture? (if you say that all is fine with sphere/ cylinder, but not with cube)

Share this post


Link to post
Share on other sites
Have you played around with other wrapping types?
Another cause could be the UV coordinates in your mesh data, they should range from 0 to 1 for a basic cube

Share this post


Link to post
Share on other sites

Just skimmed over your shader but this part looks odd and is probably where things go wrong:

if(N.x == 1.0f)
  p1.y += 0.5f;
else
   p1.x += 0.5f;

Shaders that use normal(!) mapping typically require a face normal and a tangent to work. The tangent is parallel to the surface and always perpendicular to the normal and exported meshes that are UV mapped should have one by default.

 

When implementing bump mapping you should also make use of the tangent. You can take a normal mapping shader as a reference. The only conceptual difference is that instead of sampling once to get the the normal of a fragment you sample the bump map multiple times in the direct neighbourhood. From the difference between samples you calculate a gradient along the tangent and along the binormal (binormal = tangent cross normal) and based on the gradient you skew the normal to receive the bump effet.

 

Solution B: Convert your bump map to a normal map using Nvidia's Texture tools. https://developer.nvidia.com/nvidia-texture-tools-adobe-photoshop

Edited by Lithander

Share this post


Link to post
Share on other sites

Try this:

float3 GetBump(Pixel input)
{
 float3 N = normalize(input.Normal);
 // Calculate the size of a single pixel in the bump texture.
 float2 bumpTextureSize;
 BumpTexture.GetDimensions(bumpTextureSize[0], bumpTextureSize[1]);
 float2 pixelSize = 1.0f / bumpTextureSize;
 float2 uv = UVTransform(input.UV, BumpTextureScale, BumpTextureRotate, BumpTextureTranslate);
 // Get the height value of this pixel.
 float3 mid = BumpTexture.Sample(TrilinearSampler, uv).r;
 // Get the height values of all neighbouring pixels.
 float left = BumpTexture.Sample(TrilinearSampler, uv + float2(-pixelSize.x, 0)).r;
 float right = BumpTexture.Sample(TrilinearSampler, uv + float2(pixelSize.x, 0)).r;
 float top = BumpTexture.Sample(TrilinearSampler, uv + float2(0, -pixelSize.y)).r;
 float bottom = BumpTexture.Sample(TrilinearSampler, uv + float2(0, pixelSize.y)).r;


 // Calculate the amount of height difference.
 float3 p1 = ((bottom - mid) - (top - mid)) * normalize(input.Binormal);
 float3 p2 = ((left - mid) - (right - mid)) * normalize(input.Tangent);


 // Calculate the final offset to add to the normal.
 float3 No = (p1 + p2) * -BumpDepth;


 // Add the offset to the normal and normalize it.
 float3 Nn = normalize(N + No);


 return Nn;
}

Share this post


Link to post
Share on other sites

this looks wrong to me

 

   float3 result = N + (uv.x * xGrad) + (uv.y * yGrad);

 

Why are you using the input UV coordinates to modify the normal?

 

I would try changing it to

 

float3 result = N + float3(xGrad,yGrad,0);

 

and see what happens

Share this post


Link to post
Share on other sites

The problem would be easier to solve if you could upload a sample where we can make changes to the shader and immediately see the results. I just changed the existing code to use tanget and binormal, but what stainless points out looks odd indeed. Probably there are multiple issues that influence each other.

Share this post


Link to post
Share on other sites

Dear Community,

the Bump Problem is solved :)

Don't be sad now if you don't figure out the solution, i bet i have in the future Problems that i can't fix and where can the first one who post the right solution earn good money :)

Lovely Greetings
SharinganShuriken

Share this post


Link to post
Share on other sites

Btw. If the posts are restored, could you post the solution so other users with the same problem as yours could get their answer?

Share this post


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

  • Advertisement