0
votes


I'm having difficulty figuring how to do something in XNA. I have something like this:

public void Draw()
{
     spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
     DrawFirstObject();    // Depth = 0.5f
     spriteBatch.End();

     spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Additive);
     DrawSecondObject();    // Depth = 0.2f
     spriteBatch.End();
}

Basically I need to have 2 different spritebatch begin calls one with AlphaBlend and one with Additive BlendState. But the problem is when I make this the drawn objects from the second call are always drawn on top of the first ones instead behind them where they need to be. I can't reformat my code so the second call is on the top and I need the keep the depth order. So I would be thankful if you have any suggestion.

1
Why can't you re-order your code? If you want to alpha-blend, objects behind transparent objects have to be drawn first. There is no (easy) way around that.Nico Schertler
I can't reorder my code because for example I have a tilemap object which draws the tiles which need to be with alpha blend and also I draw the tiles at different depths and the i call the Draw with Additive and draw some particles. This particles must be above some tiles and behind other but they are drawn above everything.Rokner
Then you basically need to depth-sort your tiles and particles and issue spriteBatch.Begin() dynamically if you encounter a state change. It's probably possible to optimize this sorting procedure for minimal state changes but that would depend on your scene.Nico Schertler

1 Answers

0
votes

As you are using transparent images in your first Draw call, ideally you should be using SpriteSortMode.BackToFront.

SpriteSortMode