owned this note changed 9 days ago
Published Linked with GitHub

2d Transforms & Sorting

The purpose of this working group is to improve Bevy’s 2D support by adding a specialized 2D transform.

There's an abnormally large number of closely linked PRs in development that touch this topic: A 2d transform PR, a UI transform PR, a y-sorting PR, as well as the new tilemap renderer. So part of the purpose of this document is cross-initiative coordination.

Proposal

  • Merge the ui transform PR basically as is.
  • New PR: Replace ZIndex and GlobalZIndex with a field on UiTransform.
  • Clean up and merge the 2d transform PR.
  • Merge the y-sorting PR, modified so that ZIndex and YSort are fields of Transform2d (leave out SortBias for now) and ZIndex actually effects the GlobalTransform.

Demonstration

Here's an overview of what this would look like. (WIP)

#[derive(Component, Default)] pub struct Transform2d { /// X-Y Position translation: Vec2, /// Rotation about Z Axis rotation: Rot2, /// Scale in X and Y scale: f32, /// Z component of position depth: Depth2d, /// Opt-in to y sort y_sort: bool } /// Determines the layer a 2d object is drawn on. #[derive(Default)] pub enum Depth2d { Relative(f32), // Parent layer + i Absolute(f32), // Set layer to i } impl Default for Depth2d { fn default() -> Depth2d { // This default makes children be drawn slightly infront // of their parents. Depth2d::Relative(1.0) } }
  • Mixing 2d and 3d transforms in the same hierarchy is allowed. Both compute GlobalTransform given the parent's GlobalTransform (if it exists).

  • We will use hooks to enforce archetype invariants: Adding one transform type automatically removes the other transform type.

  • Relative values for depth are incorperated into transform prop. depth is effectivly the same as the z position component on Transform3d, but with the option to specify it in absolute terms.

  • Camera will no longer require Transform. Instead Camera3d will require Transform3d and Camera2d will require Transform2d.

Select a repo