SpriteSortMode.FrontToBack and CustomEffects

Hope somebody can help me there…

I set SpriteSortMode.FrontToBack and want make use of CustomEffects
How does that work together?


works fine with SpriteSortMode.Immediate, but that mess up with all my drawingorder, where i defined layerdepth,

Character can stand behind objects and in front of it.
Character is drawn in to parts with diffrent layerdepth. other objects as well.

Is there a solution, without building a whole new construct to sort it myself? (using SpriteSortMode.Immediate)

Maybe i missed something…

You can set the shader effect in the spritebatch itself, no need to apply the shaders yourself. Just set up all the parameters up front and you are good to go.

I hope i understood the question correctly


spriteBatch.Begin(sortMode: SpriteSortMode.FrontToBack, effect: shader);
spriteBatch.Draw(DummyTexture, new Rectangle(0,0,width, height), Color.White);

Maybe a bit visual helps.
I have multiple layers with different depth-
like 5 rendertargets foreach layer of the map,+ 1 normalRendertarget foreach of it(For lightning effect)
Character,Torches has its own depth.

My current solution, wich i come up with, was like 5 additional rendertargets,where save the result and draw it in a second step to the screen with its depth.

This works fine, but looks like a work around.

Hi !
How are you specifying a layer’s depth in the spritebatch ? It may be the problem, as i’ve already seen this kind of problem here and solved it

I use the Draw method overload with layerDepths parameter.

spriteBatch.Draw(layer.RenderTarget, Vector2.Zero, null, Color.White, 0, Vector2.Zero,
                        SpriteEffects.None, layer.Depth);

Like this.
And get sorted at the end with FrontToBack.

So each time you really draw the SpriteBatch by calling SpriteBatch.End() the whole batch gets drawn the way you specified when calling SpriteBatch.Begin(). If you specified an order and provided layer-depth for each sprite, then they get sorted in the SpriteBatcher. If you specified an effect there, then it gets used at that moment and for all of the batched sprites. So you could do more batches and apply the effect appropriately.
You cannot specify an effect for a sub-set of already batched sprites. If you want to do that you’ll have to split your batch.

Okay, lets make more clear what happens here. Because i think what you suggest wouldnt work.

Layer 1 = 0.1f depth
Layer 2 = 0.2f depth

Layer 5 = 0.5f depth

UpperBody = 0.3f
LowerBody = 0.2f

MapObjects like torches work the same way.(Statues not seen on pic are made of four parts each higher depth value)
Player can be behind and in front, just like the pillars.
I draw it all between one spriteBatch.Begin(…); and one spriteBatch.End(); with rendertarget stuff before that

Hope you got an idea what happens in my “game”.

Its my very first project that got something bigger… :slight_smile:
My newbie way works somehow…
Cant be that wrong.:smiley:

I am not sure what your idea were. A batch for each layer? but everything has its normal(layers/mapobjects/player),too.
I am confused.
Its me ,not you. :slight_smile:

I’m sorry. I had no idea whatsoever. :slight_smile:
I was just stating how SpriteBatch works and why you can’t put an effect ‘in between’ a single batch.

I remember a similar discussion on MonoGame.Extended a while ago…
found it.
It’s rather long. Sorry for that.

cf this maybe ?

where a layers problem was solved.


Thanks read it all. Very interesting the part with the “depth buffer” approach, seems good.
I believe, i use what they called “painters alogrithm”, what is fine for my needs i guess.


Thanks for the input, but doesnt seem to be fit.

You really read it all :wink: :slight_smile:

Np. Anytime.
If you need further assistance, just ask.

Here is a list of useful links that I made some time ago. I’m still maintaining it.
And here is another list created by @MrValentine on this board.

Contributions are welcome on both.
Maybe you find some of the stuff useful as well.

1 Like