XNA 4.0 - Normal mapping shader - strange texture artifacts
Posted
by
Taylor
on Game Development
See other posts from Game Development
or by Taylor
Published on 2012-07-13T15:11:46Z
Indexed on
2012/09/14
21:49 UTC
Read the original article
Hit count: 421
I recently started using custom shader. Shader can do diffuse and specular lighting and normal mapping. But normal mapping is causing really ugly artifacts (some sort of pixeling noise) for textures in greater distance.
It looks like this:
Image link
This is HLSL code:
// Matrix
float4x4 World : World;
float4x4 View : View;
float4x4 Projection : Projection;
//Textury
texture2D ColorMap;
sampler2D ColorMapSampler = sampler_state
{
Texture = <ColorMap>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
MaxAnisotropy = 16;
};
texture2D NormalMap;
sampler2D NormalMapSampler = sampler_state
{
Texture = <NormalMap>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
MaxAnisotropy = 16;
};
// Light
float4 AmbientColor : Color;
float AmbientIntensity;
float3 DiffuseDirection : LightPosition;
float4 DiffuseColor : Color;
float DiffuseIntensity;
float4 SpecularColor : Color;
float3 CameraPosition : CameraPosition;
float Shininess;
// The input for the VertexShader
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 Normal : NORMAL0;
float3 Binormal : BINORMAL0;
float3 Tangent : TANGENT0;
};
// The output from the vertex shader, used for later processing
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float3 View : TEXCOORD1;
float3x3 WorldToTangentSpace : TEXCOORD2;
};
// The VertexShader.
VertexShaderOutput VertexShaderFunction(VertexShaderInput input, float3 Normal : NORMAL)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.TexCoord = input.TexCoord;
output.WorldToTangentSpace[0] = mul(normalize(input.Tangent), World);
output.WorldToTangentSpace[1] = mul(normalize(input.Binormal), World);
output.WorldToTangentSpace[2] = mul(normalize(input.Normal), World);
output.View = normalize(float4(CameraPosition,1.0) - worldPosition);
return output;
}
// The Pixel Shader
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 color = tex2D(ColorMapSampler, input.TexCoord);
float3 normalMap = 2.0 *(tex2D(NormalMapSampler, input.TexCoord)) - 1.0;
normalMap = normalize(mul(normalMap, input.WorldToTangentSpace));
float4 normal = float4(normalMap,1.0);
float4 diffuse = saturate(dot(-DiffuseDirection,normal));
float4 reflect = normalize(2*diffuse*normal-float4(DiffuseDirection,1.0));
float4 specular = pow(saturate(dot(reflect,input.View)), Shininess);
return color * AmbientColor * AmbientIntensity +
color * DiffuseIntensity * DiffuseColor * diffuse +
color * SpecularColor * specular;
}
// Techniques
technique Lighting
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
Any advice? Thanks!
© Game Development or respective owner