Skip to content

Commit

Permalink
add space flags to bevy_mod_xr
Browse files Browse the repository at this point in the history
Signed-off-by: Schmarni <marnistromer@gmail.com>
  • Loading branch information
Schmarni-Dev committed Oct 18, 2024
1 parent 616bfff commit 20f8c47
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
25 changes: 20 additions & 5 deletions crates/bevy_openxr/src/openxr/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{mem::MaybeUninit, ptr, sync::Mutex};
use bevy::{prelude::*, utils::hashbrown::HashSet};
use bevy_mod_xr::{
session::{XrFirst, XrHandleEvents},
spaces::{XrDestroySpace, XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace, XrVelocity},
spaces::{
XrDestroySpace, XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace, XrSpaceLocationFlags,
XrSpaceVelocityFlags, XrVelocity,
},
types::XrPose,
};
use openxr::{
Expand Down Expand Up @@ -155,16 +158,20 @@ fn update_space_transforms(
Option<&mut XrVelocity>,
Option<&XrReferenceSpace>,
&mut OxrSpaceLocationFlags,
&mut XrSpaceLocationFlags,
Option<&mut OxrSpaceVelocityFlags>,
Option<&mut XrSpaceVelocityFlags>,
)>,
) {
for (
mut transform,
space,
velocity,
ref_space,
mut space_location_flags,
space_velocity_flags,
mut oxr_space_location_flags,
mut xr_space_location_flags,
oxr_space_velocity_flags,
xr_space_velocity_flags,
) in &mut query
{
let ref_space = ref_space.unwrap_or(&default_ref_space);
Expand All @@ -186,11 +193,17 @@ fn update_space_transforms(
if flags.linear_valid() {
velocity.linear = space_velocity.linear_velocity.to_vec3();
}
let Some(mut vel_flags) = space_velocity_flags else {
let Some(mut vel_flags) = oxr_space_velocity_flags else {
error!("XrVelocity without OxrSpaceVelocityFlags");
return;
};
let Some(mut xr_vel_flags) = xr_space_velocity_flags else {
error!("XrVelocity without XrSpaceVelocityFlags");
return;
};
*vel_flags = flags;
xr_vel_flags.linear_valid = vel_flags.linear_valid();
xr_vel_flags.angular_valid = vel_flags.angular_valid();
Ok(location)
}
Err(err) => Err(err),
Expand All @@ -206,7 +219,9 @@ fn update_space_transforms(
if flags.rot_valid() {
transform.rotation = space_location.pose.orientation.to_quat();
}
*space_location_flags = flags;
*oxr_space_location_flags = flags;
xr_space_location_flags.position_tracked = flags.pos_valid() && flags.pos_tracked();
xr_space_location_flags.rotation_tracked = flags.rot_valid() && flags.rot_tracked();
}
}
}
Expand Down
46 changes: 44 additions & 2 deletions crates/bevy_xr/src/spaces.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use bevy::{
ecs::component::StorageType,
prelude::*,
render::{extract_component::ExtractComponent, extract_resource::ExtractResource},
};

/// Any Spaces will be invalid after the owning session exits
#[repr(transparent)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, Reflect, Debug, Component, ExtractComponent)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, Reflect, Debug, ExtractComponent)]
pub struct XrSpace(u64);

#[derive(Clone, Copy, Reflect, Debug, Component, ExtractComponent, Default)]
#[derive(Clone, Copy, Reflect, Debug, ExtractComponent, Default)]
pub struct XrVelocity {
/// Velocity of a space relative to it's reference space
pub linear: Vec3,
Expand Down Expand Up @@ -42,6 +43,22 @@ pub struct XrReferenceSpace(pub XrSpace);
)]
pub struct XrPrimaryReferenceSpace(pub XrReferenceSpace);

#[derive(
Clone, Copy, Hash, PartialEq, Eq, Reflect, Debug, Component, ExtractComponent, Default,
)]
pub struct XrSpaceLocationFlags {
pub position_tracked: bool,
pub rotation_tracked: bool,
}

#[derive(
Clone, Copy, Hash, PartialEq, Eq, Reflect, Debug, Component, ExtractComponent, Default,
)]
pub struct XrSpaceVelocityFlags {
pub linear_valid: bool,
pub angular_valid: bool,
}

impl XrSpace {
/// # Safety
/// only call with known valid handles
Expand All @@ -52,3 +69,28 @@ impl XrSpace {
self.0
}
}

impl Component for XrSpace {
const STORAGE_TYPE: StorageType = StorageType::Table;

fn register_component_hooks(hooks: &mut bevy::ecs::component::ComponentHooks) {
hooks.on_add(|mut world, entity, _| {
world
.commands()
.entity(entity)
.insert(XrSpaceLocationFlags::default());
});
}
}
impl Component for XrVelocity {
const STORAGE_TYPE: StorageType = StorageType::Table;

fn register_component_hooks(hooks: &mut bevy::ecs::component::ComponentHooks) {
hooks.on_add(|mut world, entity, _| {
world
.commands()
.entity(entity)
.insert(XrSpaceVelocityFlags::default());
});
}
}

0 comments on commit 20f8c47

Please sign in to comment.