I am trying to implement a flexible solution for applying shaders to sprites in my XNA game. I have troubles coming
up with an efficient way to maintain back-to-front draw order (or any other way to draw translucent sprites correctly)
while having the possibility to draw each sprite with a different shader.
The only thing I can think of right now is sorting the sprites by depth and then starting a new SpriteBatch Begin/End block
everytime the current sprite has a different shader than the previous one, like that:
List sprites; //Depth-sorted List of sprites
Sprite prevSprite = sprites[0];
spriteBatch.Begin(..., sprites[0].shader); //begin with the parameters for the first sprite
foreach( sprite in sprites)
{
if(prevSprite.Shader != sprite.Shader) //if we have to switch the shader
{
spriteBatch.End(); //end old, begin new block
spriteBatch.Begin(...,sprite.Shader)
}
spriteBatch.Draw(sprite); //draw the sprite
}
spriteBatch.End();
This could potentially end in a lot of draw calls, though in a 2D game it might be probable for sprites with the same shader to be
drawn at the same depth. Has anyone a better idea on how to do this?
bonus.2113






