HLSL Bump Mapping Problem (Solved)

Started by
14 comments, last by Migi0027 9 years, 8 months ago

:)

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

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

:)

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

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

:)

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

:)

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

:)

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

This topic is closed to new replies.

Advertisement