Sprite/Shape Draw Order

Just want to get some clarity on the draw order of Shapes and Sprites, and what the recommended method of controlling draw order is.

I was a bit surprised to find that Shapes seem to be drawn in the reverse order they were added. Furthermore, Shapes seem to always be drawn over Sprites in the same Scene.

The following draws a completely black screen, due to the background being rendered last.

Shape background = new Shape(mBackgroundColor, Engine.Width, Engine.Height) { Name = "Background" };

Shape testShape = new Shape(new Color(1f, 1f, 1f, 1f), 256, 256) { X = Engine.Width / 2, Y = Engine.Height / 2 };            

Sprite testSprite = Res.Assets.logo_arcane_computer.CreateSprite();
testSprite.X = Engine.Width / 2f;
testSprite.Y = Engine.Height / 2f;
testSprite.PivotX = testSprite.Data.Rect.width * 0.5f;
testSprite.PivotY = testSprite.Data.Rect.height * 0.5f;

At first I thought would just need to change the order to get the Sprite to draw over the two Shapes, but alas, the following shows a black screen with a white square; the Sprite is hidden beneath the two Shapes:

Sprite testSprite = Res.Assets.logo_arcane_computer.CreateSprite();
testSprite.X = Engine.Width / 2f;
testSprite.Y = Engine.Height / 2f;
testSprite.PivotX = testSprite.Data.Rect.width * 0.5f;
testSprite.PivotY = testSprite.Data.Rect.height * 0.5f;

Shape testShape = new Shape(new Color(1f, 1f, 1f, 1f), 256, 256) { X = Engine.Width / 2, Y = Engine.Height / 2 };

Shape background = new Shape(mBackgroundColor, Engine.Width, Engine.Height) { Name = "Background" };

I couldn’t find any way to control the “depth” of objects in a Scene, but I do see that Scenes have a depth value. Is there a way to control the depth of rendered entities that I’m missing, or is the intent to use Scenes exclusively to control render depth (that seems like it might be overkill if I want to have fine control over depth per entity)?

Somewhat related; I started down this path because I couldn’t find a way to set the Clear Color of the renderer, so consider that a feature request if it doesn’t exist already! :smile:

1 Like

Great to have you @ekhudson and thanks for posting!

So the answer is simple - right now the order is basically random :clown_face::joy:. There isn’t yet a defined draw order for shapes/sprites/etc, mostly because I haven’t gotten around to it and I’m also not sure what would make the most sense.

I’ve heard (and seen) that most attempts by engines to control it for you tend to be circumvented by developers, so I’m trying to figure out what would be something like best practice here outside of literally just submitting your draw list.

I’m thinking just making a number on entities where smaller means sooner and bigger means later, and then maybe just going from there?

Scenes do have depths and can be used as a sort of layer system (draw looks at scenes first, renders objects in that scene, then next scene, etc). So it could complement that.

What do you think? How have you seen other stuff handle it like you would like?

Ha! Well that explains it then.

I’m not sure what I’d recommend as a simple, robust solution, but what I expected to happen by default was that Scenes would be drawn in depth order, and then Sprites and Shapes drawn in the order they were added to the Scene.

I do like the idea of a depth value on the entities themselves as well, as a way to fine-tune that behavior or override it completely if one wanted.

In my current use-case, I did try to setup a “background” Scene (depth -10) with a full-screen black Shape, and a “foreground” Scene (depth 1) with a Sprite on it; the Shape still drew over the Sprite, so I’m not sure if I’m doing something wrong there…