I'm writing my own 2D engine using MonoGame and I want to be able to draw 2D primitives (using DrawPrimitives) along with sprites using SpriteBatch. But when I draw them, the primitives are always drawn below the sprites.

I tried to create a new Texture2D for each primitive, draw the primitives on these Texture2D's and draw these Texture2D's using SpriteBatch. That approach would allow me to draw primitives on any desired layer, but seems a little bit inflexible.

Simple Primitives, like rectangles and lines are already drawn using SpriteBatch and a 1x1 pixel sized Texture2D, stretched over the desired area. But more complex primitives would be difficult to draw.

image showing all types of primitives drawn using SpriteBatch and one line, drawn using DrawPrimitives below them In the image above you can see rectangles (outlined and filled), circles (outlined and filled) a line (on the right side) and a yellow curve. All of them are drawn using SpriteBatch. However, the white line in the middle of the screen is drawn using DrawPrimitives and it's below every one of the sprites. Also the yellow curve and the outlines of the circles aren't as pretty as when I drew them using DrawPrimitives.

I couldn't find any information, whether it's possible or not, to draw using DrawPrimitives and make use of layers, like with SpriteBatch. Does anyone know if it's possible, or if my approach is already the way to go when it comes to 2D primitives?

Some help or some useful links would be really appreciated. Thanks in advance.


1 Answer 1


Use the layerDepth parameter of spriteBatch.Draw to set the Z value of the generated primitive when batched.

Your custom primitives will be drawn with a Z value of 0.

I recommend splitting the spriteBatch into two separate foreground and background batches, if needed.

This value works irrespective of Immediate or Deferred draw modes.

  • \$\begingroup\$ Ok, thanks. So it's not possible to use a Z value with the DrawPrimitives function and it will always draw underneath the sprites. I think i will use the approach to draw my primitives on a newly generated Texture2D und let spriteBatch.Draw render this. \$\endgroup\$ Commented Feb 14, 2023 at 14:37
  • \$\begingroup\$ It is completely possible to use DrawPrimitives with a Z value, simply use Vector3s instead. For simplicity in 2D, adjusting the spritebatche's quads seems easier. For completeness, you can think of the 3D aspect as a fixed , offset-origin, scaled to viewport orthographic projection. \$\endgroup\$
    – user122973
    Commented Feb 16, 2023 at 21:24
  • \$\begingroup\$ You mean, the Z value of the Vector3's of the VertexPositionColor's determines how the triangles within the same Primitive overlap, right? But what I don't understand is "adjusting the spritebatche's quads". Is that the approach to draw primitives to a new Texture2D first? \$\endgroup\$ Commented Feb 21, 2023 at 9:25
  • \$\begingroup\$ Yes,, just a clarification that the spritebatches are flat quads. in other words, passing through a spritebatch quad requires additional alignment calculations. "Is that the approach to draw primitives to a new Texture2D first?" No, not at all, you can draw them at any point as you have been. If any one Z crosses the spritebatch plane, The cross location must be accounted for. So my original answer stands for its simplicity. Follow it, and you will do no wrong. \$\endgroup\$
    – user122973
    Commented Feb 23, 2023 at 3:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .