bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add `ZIndex`, `YSort`, and `SortBias` 2d components

Open tychedelia opened this issue 7 months ago • 10 comments

Objective

Using Z transform for sprites and 2d meshes is gross! Users expect familiar sorting strategies.

Solution

Adds 3 new sorting components for 2d:

  • ZIndex: sorts entities into layers and always takes priority.
  • YSort: sorts entities in each ZIndex layer by their Y transform.
  • SortBias: an arbitrary bias that is either added to Y transform or used as a secondary sort value.

TODO:

  • What should we do with the name conflict with the existing ui ZIndex.
  • How should 2d mesh opaque behave? Right now, using any of these components opts you into transparent sorting on the CPU, but ideally we'd be able to do GPU based fragment sorting as well. This is an advantage that using the transform Z has that we should take into consideration.

Testing

Tested a bunch of examples, might be more broken.

tychedelia avatar Jun 02 '25 05:06 tychedelia

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

github-actions[bot] avatar Jun 02 '25 06:06 github-actions[bot]

It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note.

Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes.

github-actions[bot] avatar Jun 02 '25 06:06 github-actions[bot]

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke! You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-19463

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

github-actions[bot] avatar Jun 02 '25 06:06 github-actions[bot]

What about propagation? It feels like this needs some sort of local ordering support so children are drawn above their ancestors and inherit their parent's z-ordering.

We just merged https://github.com/bevyengine/bevy/pull/17575 which could help. I'm the wrong person to ask about 2d design patterns, but it's not clear to me that children should always be drawn above their ancestors, although I realize this is a pattern that using z position plus transform propagation makes trivial and we are breaking. Would appreciate input here from others.

tychedelia avatar Jun 02 '25 20:06 tychedelia

it's not clear to me that children should always be drawn above their ancestors

I don't think we can assume this. We need to make it possible for children to be drawn in front of behind their ancestors, depending on artist intent.

NthTensor avatar Jun 02 '25 21:06 NthTensor

I don't think we can assume it, but I do think it's a good default :)

alice-i-cecile avatar Jun 02 '25 21:06 alice-i-cecile

I don't think we can assume it, but I do think it's a good default :)

I guess the question is, what does this look like? Do we have a GlobalZIndex component? Or do we just have some kind of mechanism that silently updates ZIndex += 1 behind the scenes? What if you don't want this behavior?

tychedelia avatar Jun 02 '25 22:06 tychedelia

It feels to me like y-sorting is more a property of an individual layer rather than individual sprites. Wouldn't you want all sprites in the same layer to be y sorted?

Does the y sort take into account the anchor point at all?

hymm avatar Jun 03 '25 17:06 hymm

Wouldn't you want all sprites in the same layer to be y sorted?

Probably? But again I think this is more a function of questions around patterns for inheritance/propagation. At the end of the day, they will be sorted one by one and so I think it still makes sense for the components to apply to individual entities if you want. I wouldn't want to take away this fine grained control just add other tools for making common use cases easier.

tychedelia avatar Jun 03 '25 18:06 tychedelia

mechanism that silently updates ZIndex += 1 behind the scenes? What if you don't want this behavior?

bevy_lunex does this. by default every child is one z step above its parent. and there's an override component (with set and add variants). i like that those can also be propagated now :)

i wonder if different variants like that can be useful here

tomara-x avatar Jun 03 '25 19:06 tomara-x

I fundamentally like this design, but there's too many loose ends and moving parts for me to be comfortable merging this, even as an iterative step. We need a real design doc for our transform overhaul which tackles these details and lays out a sequence of PRs to allow us to migrate.

alice-i-cecile avatar Jul 21 '25 17:07 alice-i-cecile