Hiya, first off I know there have been quesitons about this before, but I have not found anything with my particular issue.
In order to debug my lighting shader, I tried to set it up so it purely rendered the color of the normal map, instead of any lighting, as the normal mapping wasn’t working so I was wondering if the normalMap texture was sampling correctly.
I’m using a shadow system based off generating traingles and rendering over them. This is working fine with normal lighting, just not when I try and implement normal mapping.
My rendering code for the shadows is as follows
VertexPositionTexture[] vertices = getTriangles(pWorld);
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
graphicsDevice.RasterizerState = rasterizerState;
graphicsDevice.BlendState = BlendState.Additive;
// Samplers states are set by the shader itself
graphicsDevice.DepthStencilState = DepthStencilState.None;
//lightEffect.Parameters["lightSource"].SetValue(position);
//lightEffect.Parameters["lightColor"].SetValue(color.ToVector3() * intensity);
//lightEffect.Parameters["lightRadius"].SetValue(radius);
lightEffect.Parameters["normalMap"].SetValue(normalTarget);
lightEffect.Techniques[0].Passes[0].Apply();
ProjectVertices(vertices, 1080, 700);
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);
this code worked fine for normal lighting (Hence the commented out lighting parameters)
My shader
const float3 black = float3(0.0f, 0.0f, 0.0f);
float2 lightSource; // Light position in 2D world space
float3 lightColor; // Color of the light
float lightRadius; // Radius of the light
texture normalMap;
sampler normalSampler = sampler_state
{
Texture = (normalMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = LINEAR;
MinFilter = LINEAR;
Mipfilter = LINEAR;
};
struct VertexShaderInput
{
float3 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 WorldPos : TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = float4(input.Position, 1);
output.WorldPos = input.TexCoord; // Vertex position in 2D world space
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float3 normalCol = tex2D(normalSampler, input.WorldPos).rgb;
return float4(normalCol, 1.0f);
}
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_5_0 VertexShaderFunction();
PixelShader = compile ps_5_0 PixelShaderFunction();
}
}
Whilst I expected this simply to render the normal map color in the triangles, it instead renders
The shadows are therefore rendering correctly, but the normal map doesn’t appear to be being sampled correctly?(the normal map should just be a brick normal map texture, which is being generated correctly as I have tested just rendering the normal map rendertarget without a shader and it looks fine)
Any help would be greatly appreciated.
I’m sorry if this issue is completely trivial, I am new to shaders and am just stuck with this, I have tried looking up the answer all of google but to no avail.
Thank you