Skip to content

Add TransformBundle and use it as Sub-Bundle #2331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
8 changes: 4 additions & 4 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,8 @@ use bevy_render::{
use bevy_scene::Scene;
use bevy_transform::{
hierarchy::{BuildWorldChildren, WorldChildBuilder},
prelude::{GlobalTransform, Transform},
prelude::Transform,
TransformBundle,
};
use gltf::{
mesh::Mode,
@@ -280,7 +281,7 @@ async fn load_gltf<'a, 'b>(
let mut world = World::default();
world
.spawn()
.insert_bundle((Transform::identity(), GlobalTransform::identity()))
.insert_bundle(TransformBundle::identity())
.with_children(|parent| {
for node in scene.nodes() {
let result = load_node(&node, parent, load_context, &buffer_data);
@@ -450,9 +451,8 @@ fn load_node(
) -> Result<(), GltfError> {
let transform = gltf_node.transform();
let mut gltf_error = None;
let mut node = world_builder.spawn_bundle((
let mut node = world_builder.spawn_bundle(TransformBundle::from_transform(
Transform::from_matrix(Mat4::from_cols_array_2d(&transform.matrix())),
GlobalTransform::identity(),
));

if let Some(name) = gltf_node.name() {
11 changes: 5 additions & 6 deletions crates/bevy_pbr/src/entity.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ use bevy_render::{
prelude::Visible,
render_graph::base::MainPass,
};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_transform::prelude::TransformBundle;

/// A component bundle for "pbr mesh" entities
#[derive(Bundle)]
@@ -19,8 +19,8 @@ pub struct PbrBundle {
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}

impl Default for PbrBundle {
@@ -35,7 +35,6 @@ impl Default for PbrBundle {
main_pass: Default::default(),
draw: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}
}
@@ -44,6 +43,6 @@ impl Default for PbrBundle {
#[derive(Debug, Bundle, Default)]
pub struct PointLightBundle {
pub point_light: PointLight,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}
21 changes: 8 additions & 13 deletions crates/bevy_render/src/entity.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ use crate::{
use base::MainPass;
use bevy_asset::Handle;
use bevy_ecs::bundle::Bundle;
use bevy_transform::components::{GlobalTransform, Transform};
use bevy_transform::{components::Transform, TransformBundle};

/// A component bundle for "mesh" entities
#[derive(Bundle, Default)]
@@ -21,8 +21,8 @@ pub struct MeshBundle {
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub main_pass: MainPass,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}

/// Component bundle for camera entities with perspective projection
@@ -33,8 +33,8 @@ pub struct PerspectiveCameraBundle {
pub camera: Camera,
pub perspective_projection: PerspectiveProjection,
pub visible_entities: VisibleEntities,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}

impl PerspectiveCameraBundle {
@@ -51,7 +51,6 @@ impl PerspectiveCameraBundle {
perspective_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}
}
@@ -66,7 +65,6 @@ impl Default for PerspectiveCameraBundle {
perspective_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}
}
@@ -79,8 +77,8 @@ pub struct OrthographicCameraBundle {
pub camera: Camera,
pub orthographic_projection: OrthographicProjection,
pub visible_entities: VisibleEntities,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}

impl OrthographicCameraBundle {
@@ -99,8 +97,7 @@ impl OrthographicCameraBundle {
..Default::default()
},
visible_entities: Default::default(),
transform: Transform::from_xyz(0.0, 0.0, far - 0.1),
global_transform: Default::default(),
transform: Transform::from_xyz(0.0, 0.0, far - 0.1).into(),
}
}

@@ -117,7 +114,6 @@ impl OrthographicCameraBundle {
},
visible_entities: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}

@@ -130,7 +126,6 @@ impl OrthographicCameraBundle {
orthographic_projection: Default::default(),
visible_entities: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}
}
12 changes: 5 additions & 7 deletions crates/bevy_sprite/src/entity.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use bevy_render::{
prelude::{Draw, Visible},
render_graph::base::MainPass,
};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_transform::TransformBundle;

#[derive(Bundle, Clone)]
pub struct SpriteBundle {
@@ -21,8 +21,8 @@ pub struct SpriteBundle {
pub draw: Draw,
pub visible: Visible,
pub render_pipelines: RenderPipelines,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}

impl Default for SpriteBundle {
@@ -41,7 +41,6 @@ impl Default for SpriteBundle {
sprite: Default::default(),
material: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}
}
@@ -60,8 +59,8 @@ pub struct SpriteSheetBundle {
pub render_pipelines: RenderPipelines,
pub main_pass: MainPass,
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub transform: Transform,
pub global_transform: GlobalTransform,
#[bundle]
pub transform: TransformBundle,
}

impl Default for SpriteSheetBundle {
@@ -80,7 +79,6 @@ impl Default for SpriteSheetBundle {
sprite: Default::default(),
texture_atlas: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
}
}
}
9 changes: 4 additions & 5 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ use bevy_render::{
renderer::RenderResourceBindings,
};
use bevy_sprite::{TextureAtlas, QUAD_HANDLE};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_transform::prelude::{GlobalTransform, TransformBundle};
use bevy_window::Windows;
use glyph_brush_layout::{HorizontalAlign, VerticalAlign};

@@ -27,10 +27,10 @@ pub struct Text2dBundle {
pub draw: Draw,
pub visible: Visible,
pub text: Text,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub main_pass: MainPass,
pub text_2d_size: Text2dSize,
#[bundle]
pub transform: TransformBundle,
}

impl Default for Text2dBundle {
@@ -44,12 +44,11 @@ impl Default for Text2dBundle {
..Default::default()
},
text: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
main_pass: MainPass {},
text_2d_size: Text2dSize {
size: Size::default(),
},
transform: Default::default(),
}
}
}
13 changes: 2 additions & 11 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
@@ -7,8 +7,9 @@ use std::ops::Mul;
/// Describe the position of an entity relative to the reference frame.
///
/// * To place or move an entity, you should set its [`Transform`].
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
/// * To get the global position of an entity, you should get its [`GlobalTransform`].
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
/// * You may use the [`TransformBundle`] to guarantee this.
///
/// ## [`Transform`] and [`GlobalTransform`]
///
@@ -20,16 +21,6 @@ use std::ops::Mul;
/// [`GlobalTransform`] is updated from [`Transform`] in the system
/// [`transform_propagate_system`](crate::transform_propagate_system::transform_propagate_system).
///
/// In pseudo code:
/// ```ignore
/// for entity in entities_without_parent:
/// set entity.global_transform to entity.transform
/// recursively:
/// set parent to current entity
/// for child in parent.children:
/// set child.global_transform to parent.global_transform * child.transform
/// ```
///
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
/// before the [`GlobalTransform`] is updated.
13 changes: 2 additions & 11 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
@@ -8,8 +8,9 @@ use std::ops::Mul;
/// to its parent position.
///
/// * To place or move an entity, you should set its [`Transform`].
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
/// * To get the global position of an entity, you should get its [`GlobalTransform`].
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
/// * You may use the [`TransformBundle`] to guarantee this.
///
/// ## [`Transform`] and [`GlobalTransform`]
///
@@ -21,16 +22,6 @@ use std::ops::Mul;
/// [`GlobalTransform`] is updated from [`Transform`] in the system
/// [`transform_propagate_system`](crate::transform_propagate_system::transform_propagate_system).
///
/// In pseudo code:
/// ```ignore
/// for entity in entities_without_parent:
/// set entity.global_transform to entity.transform
/// recursively:
/// set parent to current entity
/// for child in parent.children:
/// set child.global_transform to parent.global_transform * child.transform
/// ```
///
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
/// before the [`GlobalTransform`] is updated.
64 changes: 62 additions & 2 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -4,13 +4,73 @@ pub mod transform_propagate_system;

pub mod prelude {
#[doc(hidden)]
pub use crate::{components::*, hierarchy::*, TransformPlugin};
pub use crate::{components::*, hierarchy::*, TransformBundle, TransformPlugin};
}

use bevy_app::prelude::*;
use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
use bevy_ecs::{
bundle::Bundle,
schedule::{ParallelSystemDescriptorCoercion, SystemLabel},
};
use prelude::{parent_update_system, Children, GlobalTransform, Parent, PreviousParent, Transform};

/// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`]
/// [`Component`](bevy_ecs::component::Component)s, which describe the position of an entity.
///
/// * To place or move an entity, you should set its [`Transform`].
/// * To get the global position of an entity, you should get its [`GlobalTransform`].
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
/// * You may use the [`TransformBundle`] to guarantee this.
///
/// ## [`Transform`] and [`GlobalTransform`]
///
/// [`Transform`] is the position of an entity relative to its parent position, or the reference
/// frame if it doesn't have a [`Parent`](Parent).
///
/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
///
/// [`GlobalTransform`] is updated from [`Transform`] in the system
/// [`transform_propagate_system`](crate::transform_propagate_system::transform_propagate_system).
///
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
/// before the [`GlobalTransform`] is updated.
#[derive(Default, Bundle, Clone, Debug)]
pub struct TransformBundle {
pub local: Transform,
pub global: GlobalTransform,
}

impl TransformBundle {
/// Creates a new [`TransformBundle`] from a [`Transform`] and leaving [`GlobalTransform`] with
/// no translation, rotation, and a scale of 1 on all axes.
#[inline]
pub fn from_transform(transform: Transform) -> Self {
TransformBundle {
local: transform,
..Default::default()
}
}

/// Creates a new identity [`TransformBundle`], with no translation, rotation, and a scale of 1
/// on all axes.
#[inline]
pub const fn identity() -> Self {
TransformBundle {
local: Transform::identity(),
// Note: `..Default::default()` cannot be used here, because it isn't const
global: GlobalTransform::identity(),
}
}
}

impl From<Transform> for TransformBundle {
#[inline]
fn from(transform: Transform) -> Self {
Self::from_transform(transform)
}
}

#[derive(Default)]
pub struct TransformPlugin;

Loading