add space flags to bevy_mod_xr

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2024-10-19 00:22:10 +02:00
parent 616bfff672
commit 20f8c47aba
2 changed files with 64 additions and 7 deletions

View File

@@ -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::{
@@ -155,7 +158,9 @@ fn update_space_transforms(
Option<&mut XrVelocity>,
Option<&XrReferenceSpace>,
&mut OxrSpaceLocationFlags,
&mut XrSpaceLocationFlags,
Option<&mut OxrSpaceVelocityFlags>,
Option<&mut XrSpaceVelocityFlags>,
)>,
) {
for (
@@ -163,8 +168,10 @@ fn update_space_transforms(
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);
@@ -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),
@@ -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();
}
}
}

View File

@@ -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,
@@ -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
@@ -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());
});
}
}