Skip to content
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

Transform hierarchy split #2789

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_gltf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_asset = { path = "../bevy_asset", version = "0.5.0" }
bevy_core = { path = "../bevy_core", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.5.0" }
bevy_pbr = { path = "../bevy_pbr", version = "0.5.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", features = ["bevy"] }
bevy_render = { path = "../bevy_render", version = "0.5.0" }
Expand Down
6 changes: 2 additions & 4 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_asset::{
};
use bevy_core::Name;
use bevy_ecs::world::World;
use bevy_hierarchy::prelude::{BuildWorldChildren, WorldChildBuilder};
use bevy_log::warn;
use bevy_math::Mat4;
use bevy_pbr::prelude::{PbrBundle, StandardMaterial};
Expand All @@ -18,10 +19,7 @@ use bevy_render::{
texture::{AddressMode, FilterMode, ImageType, SamplerDescriptor, TextureError, TextureFormat},
};
use bevy_scene::Scene;
use bevy_transform::{
hierarchy::{BuildWorldChildren, WorldChildBuilder},
prelude::{GlobalTransform, Transform},
};
use bevy_transform::prelude::{GlobalTransform, Transform};
use gltf::{
mesh::Mode,
texture::{MagFilter, MinFilter, WrappingMode},
Expand Down
19 changes: 19 additions & 0 deletions crates/bevy_hierarchy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "bevy_hierarchy"
version = "0.5.0"
edition = "2018"
description = "Provides hierarchy and transform functionality for Bevy Engine"
homepage = "https://bevyengine.org"
repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", features = ["bevy"] }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# other
smallvec = { version = "1.6", features = ["serde", "union", "const_generics"] }
3 changes: 3 additions & 0 deletions crates/bevy_hierarchy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bevy Hierarchy

Provides the ability to work with a parent-child hierarchy of entities.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be expanded somewhat to describe how it propagates transforms down the tree.

5 changes: 5 additions & 0 deletions crates/bevy_hierarchy/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod children;
mod parent;

pub use children::Children;
pub use parent::{Parent, PreviousParent};
Original file line number Diff line number Diff line change
Expand Up @@ -71,101 +71,3 @@ pub fn parent_update_system(
commands.entity(*e).insert(Children::with(v));
});
}
#[cfg(test)]
mod test {
use bevy_ecs::{
schedule::{Schedule, Stage, SystemStage},
system::CommandQueue,
world::World,
};

use super::*;
use crate::{hierarchy::BuildChildren, transform_propagate_system::transform_propagate_system};

#[test]
fn correct_children() {
let mut world = World::default();

let mut update_stage = SystemStage::parallel();
update_stage.add_system(parent_update_system);
update_stage.add_system(transform_propagate_system);

let mut schedule = Schedule::default();
schedule.add_stage("update", update_stage);

// Add parent entities
let mut command_queue = CommandQueue::default();
let mut commands = Commands::new(&mut command_queue, &world);
let mut children = Vec::new();
let parent = commands
.spawn()
.insert(Transform::from_xyz(1.0, 0.0, 0.0))
.id();
commands.entity(parent).with_children(|parent| {
children.push(
parent
.spawn()
.insert(Transform::from_xyz(0.0, 2.0, 0.0))
.id(),
);
children.push(
parent
.spawn()
.insert(Transform::from_xyz(0.0, 3.0, 0.0))
.id(),
);
});
command_queue.apply(&mut world);
schedule.run(&mut world);

assert_eq!(
world
.get::<Children>(parent)
.unwrap()
.0
.iter()
.cloned()
.collect::<Vec<_>>(),
children,
);

// Parent `e1` to `e2`.
(*world.get_mut::<Parent>(children[0]).unwrap()).0 = children[1];

schedule.run(&mut world);

assert_eq!(
world
.get::<Children>(parent)
.unwrap()
.iter()
.cloned()
.collect::<Vec<_>>(),
vec![children[1]]
);

assert_eq!(
world
.get::<Children>(children[1])
.unwrap()
.iter()
.cloned()
.collect::<Vec<_>>(),
vec![children[0]]
);

assert!(world.despawn(children[0]));

schedule.run(&mut world);

assert_eq!(
world
.get::<Children>(parent)
.unwrap()
.iter()
.cloned()
.collect::<Vec<_>>(),
vec![children[1]]
);
}
}
34 changes: 34 additions & 0 deletions crates/bevy_hierarchy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pub mod components;
pub mod hierarchy;

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

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

#[derive(Default)]
pub struct HierarchyPlugin;

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
pub struct ParentUpdate;

impl Plugin for HierarchyPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Children>()
.register_type::<Parent>()
.register_type::<PreviousParent>()
// add hierarchy system to startup so the first update is "correct"
.add_startup_system_to_stage(
StartupStage::PostStartup,
parent_update_system.label(ParentUpdate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
parent_update_system.label(ParentUpdate),
);
}
}
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ bevy_core = { path = "../bevy_core", version = "0.5.0" }
bevy_derive = { path = "../bevy_derive", version = "0.5.0" }
bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.5.0" }
bevy_input = { path = "../bevy_input", version = "0.5.0" }
bevy_log = { path = "../bevy_log", version = "0.5.0" }
bevy_math = { path = "../bevy_math", version = "0.5.0" }
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy_diagnostic::DiagnosticsPlugin;
use bevy_gilrs::GilrsPlugin;
#[cfg(feature = "bevy_gltf")]
use bevy_gltf::GltfPlugin;
use bevy_hierarchy::HierarchyPlugin;
use bevy_input::InputPlugin;
use bevy_log::LogPlugin;
#[cfg(feature = "bevy_pbr")]
Expand All @@ -33,6 +34,7 @@ use bevy_winit::WinitPlugin;
/// This plugin group will add all the default plugins:
/// * [`LogPlugin`]
/// * [`CorePlugin`]
/// * [`HierarchyPlugin`]
/// * [`TransformPlugin`]
/// * [`DiagnosticsPlugin`]
/// * [`InputPlugin`]
Expand All @@ -57,6 +59,7 @@ impl PluginGroup for DefaultPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
group.add(LogPlugin::default());
group.add(CorePlugin::default());
group.add(HierarchyPlugin::default());
group.add(TransformPlugin::default());
group.add(DiagnosticsPlugin::default());
group.add(InputPlugin::default());
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub mod ecs {
pub use bevy_ecs::*;
}

pub mod hierarchy {
//! Parent-child hierarchy.
pub use bevy_hierarchy::*;
}

pub mod input {
//! Resources and events for inputs, e.g. mouse/keyboard, touch, gamepads, etc.
pub use bevy_input::*;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_internal/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[doc(hidden)]
pub use crate::{
app::prelude::*, asset::prelude::*, core::prelude::*, ecs::prelude::*, input::prelude::*,
log::prelude::*, math::prelude::*, reflect::prelude::*, scene::prelude::*,
app::prelude::*, asset::prelude::*, core::prelude::*, ecs::prelude::*, hierarchy::prelude::*,
input::prelude::*, log::prelude::*, math::prelude::*, reflect::prelude::*, scene::prelude::*,
transform::prelude::*, window::prelude::*, DefaultPlugins, MinimalPlugins,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bevy_ecs::{
schedule::{ParallelSystemDescriptorCoercion, SystemStage},
system::{IntoExclusiveSystem, Res},
};
use bevy_transform::TransformSystem;
use bevy_transform::TransformPropagate;
use bevy_utils::tracing::warn;
use draw::{OutsideFrustum, Visible};

Expand Down Expand Up @@ -189,7 +189,7 @@ impl Plugin for RenderPlugin {
CoreStage::PostUpdate,
camera::visible_entities_system
.label(RenderSystem::VisibleEntities)
.after(TransformSystem::TransformPropagate),
.after(TransformPropagate),
)
.add_system_to_stage(RenderStage::RenderResource, shader::shader_update_system)
.add_system_to_stage(
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ keywords = ["bevy"]
bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_asset = { path = "../bevy_asset", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.5.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", features = ["bevy"] }
bevy_transform = { path = "../bevy_transform", version = "0.5.0" }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# other
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_ecs::{
system::{Command, Commands},
world::World,
};
use bevy_transform::hierarchy::ChildBuilder;
use bevy_hierarchy::prelude::ChildBuilder;

use crate::{Scene, SceneSpawner};

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use bevy_ecs::{
reflect::{ReflectComponent, ReflectMapEntities},
world::{Mut, World},
};
use bevy_hierarchy::prelude::Parent;
use bevy_reflect::TypeRegistryArc;
use bevy_transform::prelude::Parent;
use bevy_utils::{tracing::error, HashMap};
use thiserror::Error;
use uuid::Uuid;
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ keywords = ["bevy"]
# bevy
bevy_app = { path = "../bevy_app", version = "0.5.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" }
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.5.0" }
bevy_math = { path = "../bevy_math", version = "0.5.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.5.0", features = ["bevy"] }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# other
smallvec = { version = "1.6", features = ["serde", "union", "const_generics"] }
4 changes: 3 additions & 1 deletion crates/bevy_transform/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Bevy Transform

This crate is largely a 1:1 port from [legion_transform](https://github.com/AThilenius/legion_transform) (ecs: legion, math: nalgebra) to bevy (ecs: bevy_ecs, math: glam)
This crate was largely a 1:1 port from [legion_transform](https://github.com/AThilenius/legion_transform) (ecs: legion, math: nalgebra) to bevy (ecs: bevy_ecs, math: glam)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is just fully obsolete at this point; we've changed the data structures around since then.


It has since been split into two crates; this one, and `bevy_hierarchy`.
4 changes: 0 additions & 4 deletions crates/bevy_transform/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
mod children;
mod global_transform;
mod parent;
mod transform;

pub use children::Children;
pub use global_transform::*;
pub use parent::{Parent, PreviousParent};
pub use transform::*;
32 changes: 9 additions & 23 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,38 @@
pub mod components;
pub mod hierarchy;
pub mod transform_propagate_system;

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

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

#[derive(Default)]
pub struct TransformPlugin;

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
pub enum TransformSystem {
TransformPropagate,
ParentUpdate,
}
pub struct TransformPropagate;

impl Plugin for TransformPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Children>()
.register_type::<Parent>()
.register_type::<PreviousParent>()
.register_type::<Transform>()
app.register_type::<Transform>()
.register_type::<GlobalTransform>()
// add transform systems to startup so the first update is "correct"
.add_startup_system_to_stage(
StartupStage::PostStartup,
parent_update_system.label(TransformSystem::ParentUpdate),
)
.add_startup_system_to_stage(
StartupStage::PostStartup,
transform_propagate_system::transform_propagate_system
.label(TransformSystem::TransformPropagate)
.after(TransformSystem::ParentUpdate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
parent_update_system.label(TransformSystem::ParentUpdate),
.label(TransformPropagate)
.after(ParentUpdate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
transform_propagate_system::transform_propagate_system
.label(TransformSystem::TransformPropagate)
.after(TransformSystem::ParentUpdate),
.label(TransformPropagate)
.after(ParentUpdate),
);
}
}
Loading