remove XrSpatialTransform in favor of making XrSpace a Component and adding XrSpatialOffset
Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
@@ -13,7 +13,7 @@ use bevy_openxr::{
|
||||
};
|
||||
use bevy_xr::{
|
||||
session::{session_available, XrSessionCreated},
|
||||
spaces::{XrSpace, XrSpatialTransform},
|
||||
spaces::XrSpace,
|
||||
types::XrPose,
|
||||
};
|
||||
use openxr::Posef;
|
||||
@@ -136,7 +136,7 @@ fn spawn_hands(
|
||||
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
||||
..default()
|
||||
},
|
||||
XrSpatialTransform::from_space(left_space),
|
||||
left_space,
|
||||
Controller,
|
||||
))
|
||||
.id();
|
||||
@@ -148,7 +148,7 @@ fn spawn_hands(
|
||||
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
||||
..default()
|
||||
},
|
||||
XrSpatialTransform::from_space(right_space),
|
||||
right_space,
|
||||
Controller,
|
||||
))
|
||||
.id();
|
||||
|
||||
@@ -45,7 +45,7 @@ impl ToTransform for openxr::Posef {
|
||||
impl ToXrPose for openxr::Posef {
|
||||
fn to_xr_pose(&self) -> XrPose {
|
||||
XrPose {
|
||||
position: self.position.to_vec3(),
|
||||
translation: self.position.to_vec3(),
|
||||
rotation: self.orientation.to_quat(),
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ impl ToPosef for XrPose {
|
||||
fn to_posef(&self) -> openxr::Posef {
|
||||
openxr::Posef {
|
||||
orientation: self.rotation.to_quaternionf(),
|
||||
position: self.position.to_vector3f(),
|
||||
position: self.translation.to_vector3f(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,7 @@ use std::{mem::MaybeUninit, ptr, sync::Mutex};
|
||||
use bevy::{prelude::*, utils::hashbrown::HashSet};
|
||||
use bevy_xr::{
|
||||
session::{session_available, session_running},
|
||||
spaces::{
|
||||
XrDestroySpace, XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace, XrSpatialTransform,
|
||||
},
|
||||
spaces::{XrDestroySpace, XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace, XrSpatialOffset},
|
||||
types::XrPose,
|
||||
};
|
||||
use openxr::{
|
||||
@@ -38,12 +36,12 @@ fn destroy_space_event(instance: Res<OxrInstance>, mut events: EventReader<XrDes
|
||||
}
|
||||
}
|
||||
|
||||
pub static OXR_DO_NOT_CALL_DESTOY_SPACE_FOR: Mutex<Option<HashSet<u64>>> = Mutex::new(None);
|
||||
pub static OXR_DO_NOT_CALL_DESTOY_SPACE_FOR_SPACES: Mutex<Option<HashSet<u64>>> = Mutex::new(None);
|
||||
pub static OXR_ORIGINAL_DESTOY_SPACE: Mutex<Option<openxr::sys::pfn::DestroySpace>> =
|
||||
Mutex::new(None);
|
||||
|
||||
fn patch_destroy_space(instance: ResMut<OxrInstance>) {
|
||||
OXR_DO_NOT_CALL_DESTOY_SPACE_FOR
|
||||
OXR_DO_NOT_CALL_DESTOY_SPACE_FOR_SPACES
|
||||
.lock()
|
||||
.unwrap()
|
||||
.replace(HashSet::new());
|
||||
@@ -58,7 +56,7 @@ fn patch_destroy_space(instance: ResMut<OxrInstance>) {
|
||||
}
|
||||
}
|
||||
unsafe extern "system" fn patched_destroy_space(space: openxr::sys::Space) -> openxr::sys::Result {
|
||||
if !OXR_DO_NOT_CALL_DESTOY_SPACE_FOR
|
||||
if !OXR_DO_NOT_CALL_DESTOY_SPACE_FOR_SPACES
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
@@ -82,14 +80,16 @@ fn update_spatial_transforms(
|
||||
frame_state: Res<OxrFrameState>,
|
||||
mut query: Query<(
|
||||
&mut Transform,
|
||||
&XrSpatialTransform,
|
||||
&XrSpace,
|
||||
Option<&XrSpatialOffset>,
|
||||
Option<&XrReferenceSpace>,
|
||||
)>,
|
||||
) {
|
||||
for (mut transform, spatial, ref_space) in &mut query {
|
||||
for (mut transform, space, offset, ref_space) in &mut query {
|
||||
let offset = offset.copied().unwrap_or_default();
|
||||
let ref_space = ref_space.unwrap_or(&default_ref_space);
|
||||
if let Ok(space_location) = session.locate_space(
|
||||
&spatial.space,
|
||||
&space,
|
||||
ref_space,
|
||||
if pipelined.is_some() {
|
||||
openxr::Time::from_nanos(
|
||||
@@ -104,16 +104,15 @@ fn update_spatial_transforms(
|
||||
.location_flags
|
||||
.contains(SpaceLocationFlags::POSITION_VALID)
|
||||
{
|
||||
transform.translation = spatial
|
||||
.offset
|
||||
transform.translation = offset
|
||||
.to_transform()
|
||||
.transform_point(space_location.pose.position.to_vec3())
|
||||
}
|
||||
if space_location
|
||||
.location_flags
|
||||
.contains(SpaceLocationFlags::ORIENTATION_VALID)
|
||||
{
|
||||
transform.rotation =
|
||||
spatial.offset.rotation * space_location.pose.orientation.to_quat();
|
||||
transform.rotation = offset.rotation * space_location.pose.orientation.to_quat();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,7 +286,10 @@ pub fn locate_hand_joints_with_velocities(
|
||||
})
|
||||
}
|
||||
}
|
||||
pub fn destroy_space(instance: &openxr::Instance, space: sys::Space) -> openxr::Result<sys::Result> {
|
||||
pub fn destroy_space(
|
||||
instance: &openxr::Instance,
|
||||
space: sys::Space,
|
||||
) -> openxr::Result<sys::Result> {
|
||||
let result = unsafe { (instance.fp().destroy_space)(space) };
|
||||
cvt(result)
|
||||
}
|
||||
@@ -418,7 +420,7 @@ unsafe impl OxrSpaceExt for XrSpace {
|
||||
|
||||
fn from_raw_openxr_space(space: sys::Space) -> Self {
|
||||
let raw = space.into_raw();
|
||||
OXR_DO_NOT_CALL_DESTOY_SPACE_FOR
|
||||
OXR_DO_NOT_CALL_DESTOY_SPACE_FOR_SPACES
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_mut()
|
||||
|
||||
@@ -3,21 +3,19 @@ use bevy::{
|
||||
render::{extract_component::ExtractComponent, extract_resource::ExtractResource},
|
||||
};
|
||||
|
||||
use crate::types::XrPose;
|
||||
|
||||
/// Any Spaces will be invalid after the owning session exits
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, Reflect, Debug)]
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, Reflect, Debug, Component, ExtractComponent)]
|
||||
pub struct XrSpace(u64);
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Reflect, Debug, Component, ExtractComponent)]
|
||||
pub struct XrSpatialTransform {
|
||||
pub space: XrSpace,
|
||||
pub offset: Transform,
|
||||
}
|
||||
impl XrSpatialTransform {
|
||||
pub const fn from_space(space: XrSpace) -> Self {
|
||||
Self { space, offset: Transform::IDENTITY }
|
||||
}
|
||||
}
|
||||
// Does repr(transparent) even make sense here?
|
||||
#[repr(transparent)]
|
||||
#[derive(
|
||||
Clone, Copy, PartialEq, Reflect, Debug, Component, ExtractComponent, Default, Deref, DerefMut,
|
||||
)]
|
||||
pub struct XrSpatialOffset(pub XrPose);
|
||||
|
||||
#[derive(Event, Clone, Copy, Deref, DerefMut)]
|
||||
pub struct XrDestroySpace(pub XrSpace);
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
use bevy::math::{Quat, Vec3};
|
||||
use bevy::{
|
||||
math::{Quat, Vec3},
|
||||
reflect::Reflect,
|
||||
transform::components::Transform,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Reflect, Debug)]
|
||||
pub struct XrPose {
|
||||
pub position: Vec3,
|
||||
pub translation: Vec3,
|
||||
pub rotation: Quat,
|
||||
}
|
||||
|
||||
impl Default for XrPose {
|
||||
fn default() -> Self {
|
||||
Self::IDENTITY
|
||||
}
|
||||
}
|
||||
impl XrPose {
|
||||
pub const IDENTITY: XrPose = XrPose {
|
||||
position: Vec3::ZERO,
|
||||
translation: Vec3::ZERO,
|
||||
rotation: Quat::IDENTITY,
|
||||
};
|
||||
pub const fn to_transform(self) -> Transform {
|
||||
Transform::from_translation(self.translation).with_rotation(self.rotation)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user