Hi and thanks for all your work.
I’m trying to add normal mapping to my 2D game. I made it work with one sprite with a shader (if you need it I’ll attach it), but I don’t understand how should I draw the whole scene. Basically the question is: “what’s the correct order to draw a lot of sprites with different normal maps?”
Consider I have to draw a ground tile map and on top of it I have some other objects like a jar.
I did this:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_normalMapShader.Parameters["LightDirection"].SetValue(_lightDirection);
// Tile map ground draw
_normalMapShader.Parameters["NormalTexture"].SetValue(_cellNormalMap);
_normalMapShader.CurrentTechnique.Passes[0].Apply();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, _normalMapShader);
for (int col = 0; col < GraphicsDevice.Viewport.Width; col += _cell.Width)
for (int row = 0; row < GraphicsDevice.Viewport.Height; row += _cell.Height)
{
_spriteBatch.Draw(_cell, new Vector2(col, row), Color.White);
}
_spriteBatch.End();
// Jar draw
_normalMapShader.Parameters["NormalTexture"].SetValue(_jarNormalMap);
_normalMapShader.CurrentTechnique.Passes[0].Apply();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, _normalMapShader);
_spriteBatch.Draw(_jar, new Vector2(200, 200), Color.White);
_spriteBatch.End();
// Other different objects the same way
// ...
// ...
base.Draw(gameTime);
}
It works, but I wonder if it is bad to call SpriteBatch.Begin
for each kind of object, in this case for each different normal map I would have different begin call.
Are there other possibilities? Am I doing it wrong?
Thanks!