fix parenting example, add missing transform components, add parenting to world builder
This commit is contained in:
parent
6ef1c099ff
commit
f6dd6a5ca9
@ -10,12 +10,13 @@ fn main() {
|
|||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rotates the parent, which will result in the child also rotating
|
||||||
fn build_rotator_system() -> Box<dyn Schedulable> {
|
fn build_rotator_system() -> Box<dyn Schedulable> {
|
||||||
SystemBuilder::new("Rotator")
|
SystemBuilder::new("Rotator")
|
||||||
.read_resource::<Time>()
|
.read_resource::<Time>()
|
||||||
.with_query(<(Write<Rotator>, Write<Rotation>)>::query())
|
.with_query(<(Write<Rotator>, Write<Rotation>)>::query())
|
||||||
.build(move |_, world, time, light_query| {
|
.build(move |_, world, time, rotator_query| {
|
||||||
for (_, mut rotation) in light_query.iter_mut(world) {
|
for (_rotator, mut rotation) in rotator_query.iter_mut(world) {
|
||||||
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
|
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -23,86 +24,51 @@ fn build_rotator_system() -> Box<dyn Schedulable> {
|
|||||||
|
|
||||||
fn setup(world: &mut World, resources: &mut Resources) {
|
fn setup(world: &mut World, resources: &mut Resources) {
|
||||||
let cube = Mesh::load(MeshType::Cube);
|
let cube = Mesh::load(MeshType::Cube);
|
||||||
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
|
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||||
|
let cube_handle = mesh_storage.add(cube);
|
||||||
|
|
||||||
let (cube_handle, plane_handle) = {
|
world
|
||||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
.build()
|
||||||
(mesh_storage.add(cube), mesh_storage.add(plane))
|
// parent cube
|
||||||
};
|
.add_archetype(MeshEntity {
|
||||||
|
mesh: cube_handle,
|
||||||
// plane
|
material: StandardMaterial {
|
||||||
world.insert(
|
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
|
||||||
(),
|
|
||||||
vec![(
|
|
||||||
plane_handle,
|
|
||||||
StandardMaterial {
|
|
||||||
albedo: math::vec4(0.1, 0.2, 0.1, 1.0).into(),
|
|
||||||
},
|
},
|
||||||
LocalToWorld::identity(),
|
translation: Translation::new(0.0, 0.0, 1.0),
|
||||||
Translation::new(0.0, 0.0, -5.0),
|
..Default::default()
|
||||||
)],
|
})
|
||||||
);
|
.add(Rotator)
|
||||||
|
// cube
|
||||||
// cube
|
.add_archetype(MeshEntity {
|
||||||
let parent_cube = *world
|
mesh: cube_handle,
|
||||||
.insert(
|
material: StandardMaterial {
|
||||||
(),
|
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
|
||||||
vec![(
|
|
||||||
cube_handle,
|
|
||||||
StandardMaterial {
|
|
||||||
albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
|
|
||||||
},
|
|
||||||
LocalToWorld::identity(),
|
|
||||||
Translation::new(0.0, 0.0, 1.0),
|
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
|
||||||
Rotator,
|
|
||||||
)],
|
|
||||||
)
|
|
||||||
.first()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// cube
|
|
||||||
world.insert(
|
|
||||||
(),
|
|
||||||
vec![(
|
|
||||||
cube_handle,
|
|
||||||
StandardMaterial {
|
|
||||||
albedo: math::vec4(0.5, 0.3, 0.3, 1.0).into(),
|
|
||||||
},
|
},
|
||||||
LocalToWorld::identity(),
|
translation: Translation::new(0.0, 0.0, 3.0),
|
||||||
Translation::new(0.0, 0.0, 3.0),
|
..Default::default()
|
||||||
Parent(parent_cube),
|
})
|
||||||
LocalToParent::identity(),
|
.set_last_entity_as_parent()
|
||||||
)],
|
// light
|
||||||
);
|
.add_archetype(LightEntity {
|
||||||
|
translation: Translation::new(4.0, -4.0, 5.0),
|
||||||
// light
|
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||||
world.insert(
|
..Default::default()
|
||||||
(),
|
})
|
||||||
vec![(
|
// camera
|
||||||
Light::default(),
|
.add_archetype(CameraEntity {
|
||||||
LocalToWorld::identity(),
|
camera: Camera::new(CameraType::Projection {
|
||||||
Translation::new(4.0, -4.0, 5.0),
|
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
|
||||||
)],
|
|
||||||
);
|
|
||||||
|
|
||||||
// camera
|
|
||||||
world.insert(
|
|
||||||
(),
|
|
||||||
vec![(
|
|
||||||
Camera::new(CameraType::Projection {
|
|
||||||
fov: std::f32::consts::PI / 4.0,
|
fov: std::f32::consts::PI / 4.0,
|
||||||
near: 1.0,
|
near: 1.0,
|
||||||
far: 1000.0,
|
far: 1000.0,
|
||||||
aspect_ratio: 1.0,
|
aspect_ratio: 1.0,
|
||||||
}),
|
}),
|
||||||
ActiveCamera,
|
active_camera: ActiveCamera,
|
||||||
LocalToWorld(Mat4::look_at_rh(
|
local_to_world: LocalToWorld(Mat4::look_at_rh(
|
||||||
Vec3::new(3.0, -15.0, 8.0),
|
Vec3::new(5.0, 10.0, 10.0),
|
||||||
Vec3::new(0.0, 0.0, 0.0),
|
Vec3::new(0.0, 0.0, 0.0),
|
||||||
Vec3::new(0.0, 0.0, 1.0),
|
Vec3::new(0.0, 0.0, 1.0),
|
||||||
)),
|
)),
|
||||||
)],
|
})
|
||||||
);
|
.build();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,8 @@ pub struct MeshEntity {
|
|||||||
pub renderable: Renderable,
|
pub renderable: Renderable,
|
||||||
pub local_to_world: LocalToWorld,
|
pub local_to_world: LocalToWorld,
|
||||||
pub translation: Translation,
|
pub translation: Translation,
|
||||||
|
pub rotation: Rotation,
|
||||||
|
pub scale: Scale,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype, Default)]
|
#[derive(EntityArchetype, Default)]
|
||||||
@ -22,6 +24,8 @@ pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
|
|||||||
pub renderable: Renderable,
|
pub renderable: Renderable,
|
||||||
pub local_to_world: LocalToWorld,
|
pub local_to_world: LocalToWorld,
|
||||||
pub translation: Translation,
|
pub translation: Translation,
|
||||||
|
pub rotation: Rotation,
|
||||||
|
pub scale: Scale,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype, Default)]
|
#[derive(EntityArchetype, Default)]
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
use crate::ecs::EntityArchetype;
|
use crate::ecs::EntityArchetype;
|
||||||
|
use bevy_transform::components::{LocalToParent, Parent};
|
||||||
use legion::{
|
use legion::{
|
||||||
filter::{ChunksetFilterData, Filter},
|
filter::{ChunksetFilterData, Filter},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@ -14,6 +15,7 @@ impl WorldBuilderSource for World {
|
|||||||
WorldBuilder {
|
WorldBuilder {
|
||||||
world: self,
|
world: self,
|
||||||
current_entity: None,
|
current_entity: None,
|
||||||
|
last_entity: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -21,11 +23,16 @@ impl WorldBuilderSource for World {
|
|||||||
pub struct WorldBuilder<'a> {
|
pub struct WorldBuilder<'a> {
|
||||||
world: &'a mut World,
|
world: &'a mut World,
|
||||||
current_entity: Option<Entity>,
|
current_entity: Option<Entity>,
|
||||||
|
last_entity: Option<Entity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> WorldBuilder<'a> {
|
impl<'a> WorldBuilder<'a> {
|
||||||
pub fn build_entity(mut self) -> Self {
|
pub fn build_entity(mut self) -> Self {
|
||||||
let entity = *self.world.insert((), vec![()]).first().unwrap();
|
let entity = *self.world.insert((), vec![()]).first().unwrap();
|
||||||
|
if let Some(last_entity) = self.current_entity.take() {
|
||||||
|
self.last_entity = Some(last_entity);
|
||||||
|
}
|
||||||
|
|
||||||
self.current_entity = Some(entity);
|
self.current_entity = Some(entity);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -62,7 +69,24 @@ impl<'a> WorldBuilder<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_archetype(mut self, entity_archetype: impl EntityArchetype) -> Self {
|
pub fn add_archetype(mut self, entity_archetype: impl EntityArchetype) -> Self {
|
||||||
|
if let Some(last_entity) = self.current_entity.take() {
|
||||||
|
self.last_entity = Some(last_entity);
|
||||||
|
}
|
||||||
|
|
||||||
self.current_entity = Some(entity_archetype.insert(self.world));
|
self.current_entity = Some(entity_archetype.insert(self.world));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_last_entity_as_parent(self) -> Self {
|
||||||
|
let current_entity = self.current_entity.unwrap();
|
||||||
|
let _ = self.world.add_component(
|
||||||
|
current_entity,
|
||||||
|
Parent(self.last_entity.unwrap()),
|
||||||
|
);
|
||||||
|
let _ = self
|
||||||
|
.world
|
||||||
|
.add_component(current_entity, LocalToParent::identity());
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user