Hello everyone, when I try to pass a texture to my shader, it gives me a null reference error.
Shader:
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
float3 LightDirection;
float3 LightColor = 1.0;
float3 AmbientColor = 0.35;
Texture2D ScreenTexture;
sampler TextureSampler = sampler_state
{
texture = <ScreenTexture>;
};
Texture2D NormalTexture;
sampler NormalSampler = sampler_state
{
texture = <NormalTexture>;
};
float4 main(float4 pos : SV_POSITION, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0
{
//Look up the texture value
float4 tex = ScreenTexture.Sample(TextureSampler, texCoord);
//Look up the normalmap value
float4 normal = 2 * NormalTexture.Sample(NormalSampler, texCoord) - 1;
// Compute lighting.
float lightAmount = saturate( dot(normal.xyz, LightDirection));
color.rgb *= AmbientColor + (lightAmount * LightColor);
return color*tex;
}
technique Normalmap
{
pass Pass1
{
PixelShader = compile PS_SHADERMODEL main();
}
}
draw function:
protected override void Draw(GameTime gameTime)
{
Vector2 dir = new Vector2(0, 0);
dir.Normalize();
Vector3 lightDirection = new Vector3(dir.X, dir.Y, 0.05f);
lightDirection.Normalize();
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.BlendState = BlendState.Opaque;
effect.Parameters["LightDirection"].SetValue(lightDirection);
effect.Parameters["NormalTexture"].SetValue(normalMap);
effect.Parameters["ScreenTexture"].SetValue(texture);
effect.Parameters["LightColor"].SetValue(new Vector3(1f, 1f, 1f));
effect.Parameters["AmbientColor"].SetValue(new Vector3(.25f, 0.25f, 0.25f) * 0.0001f);
effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, effect);
spriteBatch.Draw(texture, Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}