2024-04-11 03:45:25 +00:00
|
|
|
mod node;
|
|
|
|
pub use node::*;
|
|
|
|
|
|
|
|
mod world_transform;
|
|
|
|
pub use world_transform::*;
|
2024-03-03 21:21:55 +00:00
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
use lyra_ecs::{query::{Entities, ViewOne}, relation::ChildOf, Bundle, Component, World};
|
|
|
|
use lyra_math::Transform;
|
2024-03-03 21:21:55 +00:00
|
|
|
|
|
|
|
// So we can use lyra_ecs::Component derive macro
|
|
|
|
pub(crate) mod lyra_engine {
|
|
|
|
pub(crate) mod ecs {
|
|
|
|
pub use lyra_ecs::*;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A flag spawned on all scene node entities
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct SceneNodeFlag;
|
|
|
|
|
|
|
|
/// A flag spawned on only the scene root node
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct SceneNodeRoot;
|
|
|
|
|
2024-04-11 03:55:48 +00:00
|
|
|
/// A Graph of nodes that represents the hierarchy of a Scene.
|
2024-03-03 21:21:55 +00:00
|
|
|
///
|
|
|
|
/// This SceneGraph is special in the sense that it is literally just an ECS world with methods
|
|
|
|
/// implemented for it that make it easier to use for a SceneGraph.
|
|
|
|
//#[derive(Default)]
|
2024-03-25 02:40:38 +00:00
|
|
|
pub struct SceneGraph {
|
|
|
|
pub(crate) world: World,
|
2024-03-03 21:21:55 +00:00
|
|
|
root_node: SceneNode,
|
|
|
|
}
|
|
|
|
|
2024-03-25 02:40:38 +00:00
|
|
|
impl SceneGraph {
|
2024-03-03 21:21:55 +00:00
|
|
|
/// Create a new SceneGraph with its own ECS World.
|
|
|
|
pub fn new() -> Self {
|
2024-03-25 02:40:38 +00:00
|
|
|
let world = World::new();
|
2024-03-03 21:21:55 +00:00
|
|
|
|
2024-03-25 02:40:38 +00:00
|
|
|
Self::from_world(world)
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new SceneGraph inside an existing ECS World.
|
2024-03-25 02:40:38 +00:00
|
|
|
pub fn from_world(mut world: World) -> Self {
|
2024-04-11 03:45:25 +00:00
|
|
|
let root_en = world.spawn((WorldTransform::default(), Transform::default(), SceneNodeRoot));
|
2024-03-03 21:21:55 +00:00
|
|
|
let root = SceneNode::new(None, root_en);
|
|
|
|
|
|
|
|
Self {
|
2024-03-25 02:40:38 +00:00
|
|
|
world,
|
2024-03-03 21:21:55 +00:00
|
|
|
root_node: root,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a node to the root node of the SceneGraph.
|
|
|
|
///
|
|
|
|
/// The spawned entity will have a `ChildOf` relation targeting the root node, the
|
|
|
|
/// `SceneNodeFlag` component is also added to the entity.
|
2024-04-11 03:45:25 +00:00
|
|
|
pub fn add_node<B: Bundle>(&mut self, bundle: B) -> SceneNode {
|
2024-03-03 21:21:55 +00:00
|
|
|
let node = self.root_node.clone();
|
2024-04-11 03:45:25 +00:00
|
|
|
self.add_node_under(&node, bundle)
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a node under a parent node.
|
|
|
|
///
|
|
|
|
/// The spawned entity will have a `ChildOf` relation targeting the provided parent node,
|
|
|
|
/// the `SceneNodeFlag` component is also added to the entity.
|
2024-04-11 03:45:25 +00:00
|
|
|
pub fn add_node_under<B: Bundle>(&mut self, parent: &SceneNode, bundle: B) -> SceneNode {
|
|
|
|
world_add_child_node(&mut self.world, parent, bundle)
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 04:32:31 +00:00
|
|
|
/// Insert a component bundle to a SceneNode.
|
|
|
|
///
|
|
|
|
/// See [`lyra_ecs::World::insert`].
|
|
|
|
pub fn insert<B: Bundle>(&mut self, node: &SceneNode, bundle: B) {
|
|
|
|
self.world.insert(node.entity(), bundle);
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:15:18 +00:00
|
|
|
pub fn add_empty_node_under(&mut self, parent: &SceneNode, local_transform: Transform) -> SceneNode {
|
|
|
|
let e = self.world.spawn((SceneNodeFlag, local_transform));
|
|
|
|
self.world.add_relation(e, ChildOf, parent.entity());
|
|
|
|
|
|
|
|
SceneNode::new(Some(parent.entity()), e)
|
|
|
|
}
|
|
|
|
|
2024-03-03 21:21:55 +00:00
|
|
|
/// Traverses down the SceneGraph, calling `callback` with each SceneNode and its world transform.
|
|
|
|
///
|
|
|
|
/// The traversal does not include the root scene node.
|
2024-04-11 03:45:25 +00:00
|
|
|
pub fn traverse_down<F, Q>(&self, mut callback: F)
|
2024-03-03 21:21:55 +00:00
|
|
|
where
|
2024-04-11 03:45:25 +00:00
|
|
|
F: FnMut(&World, &SceneNode, ViewOne<Q::Query>),
|
|
|
|
Q: lyra_ecs::query::AsQuery,
|
2024-03-03 21:21:55 +00:00
|
|
|
{
|
2024-04-11 03:45:25 +00:00
|
|
|
self.traverse_down_from::<F, Q>(self.root_node.clone(), &mut callback);
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 21:28:27 +00:00
|
|
|
/// Recursively Traverses down the SceneGraph from a starting node, calling `callback` with each
|
2024-03-03 21:21:55 +00:00
|
|
|
/// SceneNode and its world transform.
|
2024-04-11 03:45:25 +00:00
|
|
|
fn traverse_down_from<F, Q>(&self, start: SceneNode, callback: &mut F)
|
2024-03-03 21:21:55 +00:00
|
|
|
where
|
2024-04-11 03:45:25 +00:00
|
|
|
F: FnMut(&World, &SceneNode, ViewOne<Q::Query>),
|
|
|
|
Q: lyra_ecs::query::AsQuery,
|
2024-03-03 21:21:55 +00:00
|
|
|
{
|
2024-03-25 02:40:38 +00:00
|
|
|
let v = self.world
|
2024-04-11 03:45:25 +00:00
|
|
|
.view::<Entities>()
|
2024-03-03 21:21:55 +00:00
|
|
|
.relates_to::<ChildOf>(start.entity());
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
for (e, _rel) in v.iter() {
|
2024-03-03 21:21:55 +00:00
|
|
|
let node = SceneNode::new(Some(start.entity()), e);
|
2024-04-11 03:45:25 +00:00
|
|
|
//let world_pos = node.world_transform(self);
|
|
|
|
let v = self.world.view_one::<Q>(e);
|
|
|
|
callback(&self.world, &node, v);
|
2024-03-03 21:21:55 +00:00
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
self.traverse_down_from::<F, Q>(node, callback);
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-04 03:15:18 +00:00
|
|
|
|
|
|
|
pub fn root_node(&self) -> SceneNode {
|
|
|
|
self.root_node.clone()
|
|
|
|
}
|
2024-03-31 04:32:31 +00:00
|
|
|
|
|
|
|
/// Retrieve a borrow of the world that backs the Scene
|
|
|
|
pub fn world(&self) -> &World {
|
|
|
|
&self.world
|
|
|
|
}
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a node under a parent node.
|
|
|
|
///
|
|
|
|
/// The spawned entity will have a `ChildOf` relation targeting the provided parent node,
|
|
|
|
/// the `SceneNodeFlag` component is also added to the entity.
|
2024-04-11 03:45:25 +00:00
|
|
|
pub(crate) fn world_add_child_node<B: Bundle>(world: &mut World, parent: &SceneNode, bundle: B) -> SceneNode {
|
2024-03-03 21:21:55 +00:00
|
|
|
let e = world.spawn(bundle);
|
2024-04-11 03:45:25 +00:00
|
|
|
world.insert(e, (SceneNodeFlag,));
|
2024-03-03 21:21:55 +00:00
|
|
|
world.add_relation(e, ChildOf, parent.entity());
|
|
|
|
|
|
|
|
SceneNode::new(Some(parent.entity()), e)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
2024-04-11 03:45:25 +00:00
|
|
|
use lyra_ecs::{query::{filter::{Has, Not}, Entities}, relation::{ChildOf, RelationOriginComponent}, Component};
|
2024-03-03 21:21:55 +00:00
|
|
|
use lyra_math::{Transform, Vec3};
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
use crate::{lyra_engine, WorldTransform, SceneGraph};
|
2024-03-03 21:21:55 +00:00
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct FakeMesh;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_node_hierarchy() {
|
|
|
|
let mut scene = SceneGraph::new();
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
let a = scene.add_node((Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh));
|
2024-03-03 21:21:55 +00:00
|
|
|
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_node_hierarchy() {
|
|
|
|
let mut scene = SceneGraph::new();
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
let a = scene.add_node((Transform::from_translation(Vec3::new(10.0, 10.0, 10.0)), FakeMesh));
|
2024-03-03 21:21:55 +00:00
|
|
|
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
let b = a.add_node(&mut scene, (Transform::from_translation(Vec3::new(50.0, 50.0, 50.0)), FakeMesh));
|
2024-03-03 21:21:55 +00:00
|
|
|
assert!(b.parent(&scene).unwrap() == a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn traverse_down() {
|
|
|
|
let v2s = vec![Vec3::new(10.0, 10.0, 10.0), Vec3::new(50.0, 50.0, 50.0)];
|
|
|
|
|
|
|
|
let mut scene = SceneGraph::new();
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
let a = scene.add_node((WorldTransform::default(), Transform::from_translation(v2s[0]), FakeMesh));
|
2024-03-03 21:21:55 +00:00
|
|
|
assert!(a.parent(&scene).unwrap() == scene.root_node);
|
2024-04-11 03:45:25 +00:00
|
|
|
let b = a.add_node(&mut scene, (WorldTransform::default(), Transform::from_translation(v2s[1]), FakeMesh));
|
2024-03-03 21:21:55 +00:00
|
|
|
assert!(b.parent(&scene).unwrap() == a);
|
|
|
|
|
2024-04-11 03:45:25 +00:00
|
|
|
let view = scene.world.view::<(Entities, &mut WorldTransform, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>();
|
|
|
|
crate::system_update_world_transforms(&scene.world, view).unwrap();
|
2024-03-03 21:21:55 +00:00
|
|
|
|
|
|
|
let mut idx = 0;
|
2024-04-11 03:45:25 +00:00
|
|
|
scene.traverse_down::<_, &WorldTransform>(|_, _, v| {
|
|
|
|
let pos = v.get().unwrap();
|
2024-03-03 21:21:55 +00:00
|
|
|
if idx == 0 {
|
2024-04-11 03:45:25 +00:00
|
|
|
assert_eq!(**pos, Transform::from_translation(v2s[idx]));
|
2024-03-03 21:21:55 +00:00
|
|
|
} else if idx == 1 {
|
|
|
|
let t = v2s.iter().sum();
|
2024-04-11 03:45:25 +00:00
|
|
|
assert_eq!(**pos, Transform::from_translation(t));
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idx += 1;
|
|
|
|
});
|
2024-04-11 03:45:25 +00:00
|
|
|
}
|
2024-03-03 21:21:55 +00:00
|
|
|
}
|