First of all, im new to shaders and xna.
I've been trying to follow this tutorial: http://www.xnahub.com/simple-2d-lighting-system-in-c-and-monogame/
I've done everything he said, I even ended up copy/pasting some parts to be totally sure although – it still won't work.
sampler s0;
texture lightMask;
sampler lightSampler=sampler_state {
Texture=<lightMask>;
}
;
float4 PixelShaderLight(float2 coords:TEXCOORD0):COLOR0 {
float4 color=tex2D(s0, coords);
float4 lightColor=tex2D(lightSampler, coords);
return color * lightColor;
}
technique Technique1 {
pass P0 {
PixelShader=compile ps_4_0_level_9_1 PixelShaderLight();
}
}
The problem is that when I apply the pass 0 everything goes black.
My guess is that the lightcolor is returning zero. The lightmask is a renderTarget where I've painted my lights on.
I really dont know why lightcolor would return zero. If that is the case, could anyone give me a hint in what I'm doing wrong?
Here is my main class if you want to look at it:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace TestingGame
{
public class TestingGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Location loc;
public static Texture2D lightMask;
public static Texture2D img;
public static Effect effect1;
RenderTarget2D lightsTarget;
RenderTarget2D mainTarget;
public TestingGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
loc = new Location(20,20);
var pp = GraphicsDevice.PresentationParameters;
lightsTarget = new RenderTarget2D(
GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
mainTarget = new RenderTarget2D(
GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
lightMask = Content.Load<Texture2D>("lightmask.png");
img = Content.Load<Texture2D>("img.png");
effect1 = Content.Load<Effect>("lighteffect");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if(loc.Equals(new Location(21,20)))
System.Console.WriteLine("Working");
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(lightsTarget);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive);
spriteBatch.Draw(lightMask, new Vector2(20, 20), Color.Red);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(mainTarget);
GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
spriteBatch.Draw(img, new Vector2(50,50));
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect1.Parameters["lightMask"].SetValue(lightsTarget);
effect1.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(mainTarget, Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}