I’m using the following code to test shaders (Lightning Engine) but my second spritebatch.Draw() call turns the textures to red
in my Game1 class i use the following code to draw the scene:
GraphicsDevice.Clear(Color.CornflowerBlue);
LightEffect.Parameters["lightMapSampler"].SetValue(LightMap);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, LightEffect);
spriteBatch.Draw(Background, new Rectangle(0,0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
spriteBatch.End();
GraphicsDevice.Textures[1] = null;
spriteBatch.Begin();
spriteBatch.Draw(MidGround, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
spriteBatch.End();
The code is based on the following “tutorial” http://rabidlion.com/?p=11 I’m using the same textures as well. The shader looks like this:
sampler textureSampler : register(s0);
sampler lightMapSampler : register(s1);
float4 PixelShaderFunction(float2 texcoords : TEXCOORD0) : COLOR0
{
float4 backgroundCol = tex2D(textureSampler, texcoords);
float4 lightMapCol = tex2D(lightMapSampler, texcoords);
return backgroundCol * lightMapCol;
}
technique Light
{
pass BasicLight
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
Now my midground isn’t grey after drawing it. the texture turns red:
I’m using Monogame 3.4 on a Windows 7 64 bit.
What is my mistake? Is this a Monogame bug?
I already got this Problem with Monogame 3.2 Problem with HLSL Shaders in Monogame V 3.2 but didn’t try anymore since then … shame on me and sorry tom but thanks for the help anyway.
Update
If I stop to apply the shader the colors are normal. (Background and Midground, Lightmap doesn’t get drawed).
Update 2
If I change my second render to the following:
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, DefaultEffect);
spriteBatch.Draw(MidGround, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
spriteBatch.End();
It just works fine. The DefaultEffect looks like this:
sampler TextureSampler : register(s0);
float4 main(float4 position : SV_Position, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
float4 tex = tex2D(TextureSampler, texCoord);
return tex;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_3_0 main();
}
}
If I change the return of the shader to:
return tex * color;
the textures are red again.