开发者

Is it possible to combine more than one SpriteSortMode for SpriteBatch.Begin?

I want to sort sprites by layer depth (i.e. SpriteSortMode.BackToFront) but also sort them by SpriteSortMode.Immediate (which my understanding is the last thing to be drawn gets drawn on top of everything else).

The reason I want this behav开发者_开发知识库iour is so that if I draw two sprites on the same layer depth the one I drew last gets put on top. Right now it's just random what gets rendered on top and what doesn't (I assume it's something similar to z fighting).

I've tried this:

  SpriteBatch.Begin(SpriteSortMode.Immediate|SpriteSortMode.BackToFront, BlendState.AlphaBlend);

But it didn't work...


Not the way you're doing it. SpriteSortMode is not a [Flags], so you're not supposed combine them like that.

One simple idea for getting the effect you want would be to add a less-significant offset to the depth for each sprite you draw. So for each draw you would write:

spriteBatch.Draw(..., 0.5f + depthOffset);
depthOffset += 0.0001f;

And so the depths being drawn might end up like:

0.5f  ->  0.5f
0.5f  ->  0.5001f
0.8f  ->  0.8002f
0.8f  ->  0.8003f
0.6f  ->  0.6004f
0.5f  ->  0.5005f

And when this gets sorted, you will end up with a stable sort (ie: identical values passed in are in the same order when output). Just be aware this is only valid if the largest increment (ie: of the final sprite) is smaller than the smallest difference in desired depth values.


I had got a similiar problem in my game a while ago. I resolved this similiar to Andrew Russell answer. I think that I have smaler offset (something like 0.0000001f). I was developing a tile based game and I added one offset unit for each unit in y-dimension. In this way sprites that should be draw behind were drawn correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜