Hi. I’m fairly new to Monogame and game development in general. I have just started looking into shaders. I have been looking around online for simple lighting shaders for a 2d game.
As I am just testing around with it there isn’t anything complex, to it. So far it just takes in a rendertarget2d, which would be my camera view, and a rendertarget2d for a mask. However, the shader just returns black pixels. I have an example which is a near exact version of what I am doing and that runs fine. So I am unsure as to what may be causing it.
This is my code in the draw method:
//Draw to mask
GraphicsDevice.SetRenderTarget(LightMask);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(blendState: BlendState.Additive);
spriteBatch.Draw(LightMaskTexture, Vector2.Zero, Color.Orange);
spriteBatch.End();
//Draw the 'camera view'
GraphicsDevice.SetRenderTarget(BackgroundRT);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(blendState: BlendState.AlphaBlend);
spriteBatch.Draw(Background, Vector2.Zero, Color.White);
spriteBatch.End();
//Draw to screen using shader
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
plainEffect.Parameters["LightMaskRT"].SetValue(LightMask);
spriteBatch.Begin(blendState: BlendState.AlphaBlend, effect: plainEffect);
spriteBatch.Draw(BackgroundRT, Vector2.Zero, Color.White);
spriteBatch.End();
Here is the 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
Texture2D BackgroundRT;
Texture2D LightMaskRT;
sampler2D BackgroundRTSampler = sampler_state
{
Texture = <BackgroundRT>;
};
sampler2D LightMaskRTSampler = sampler_state
{
Texture = <LightMaskRT>;
};
float4 LightPS(float2 TextureCoords : TEXCOORD0) : COLOR
{
float4 BackgroundPixelColour = tex2D(BackgroundRTSampler, TextureCoords);
float4 LightMaskPixelColour = tex2D(LightMaskRTSampler, TextureCoords);
return BackgroundPixelColour * LightMaskPixelColour;
}
technique Lighting
{
pass P0
{
PixelShader = compile PS_SHADERMODEL LightPS();
}
};
Any help on why it seems to be just displaying black would be great. Thanks!