Skip to content

Commit

Permalink
Add Vec2Conv helper and Display warning with message when openxr fail…
Browse files Browse the repository at this point in the history
…s to load (#58)

* add support for Vec2 Actions, slightly changed the way actions are created but no functionality change and changed the get_action_* return result to a custom error

* Whoops

* added warning with error when openxr fails to load and added Vec2Conv helper

* made it possible for the user to spawn OpenXRTrackingRoot
  • Loading branch information
Schmarni-Dev authored Dec 14, 2023
1 parent 248d160 commit 265696b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 65 deletions.
123 changes: 63 additions & 60 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,67 +82,70 @@ impl Plugin for OpenXrPlugin {
let mut system_state: SystemState<Query<&RawHandleWrapper, With<PrimaryWindow>>> =
SystemState::new(&mut app.world);
let primary_window = system_state.get(&app.world).get_single().ok().cloned();
if let Ok((
device,
queue,
adapter_info,
render_adapter,
instance,
xr_instance,
session,
blend_mode,
resolution,
format,
session_running,
frame_waiter,
swapchain,
input,
views,
frame_state,
)) = graphics::initialize_xr_graphics(primary_window.clone())
{
// std::thread::sleep(Duration::from_secs(5));
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
debug!("Configured wgpu adapter Features: {:#?}", device.features());
app.insert_resource(xr_instance.clone());
app.insert_resource(session.clone());
app.insert_resource(blend_mode.clone());
app.insert_resource(resolution.clone());
app.insert_resource(format.clone());
app.insert_resource(session_running.clone());
app.insert_resource(frame_waiter.clone());
app.insert_resource(swapchain.clone());
app.insert_resource(input.clone());
app.insert_resource(views.clone());
app.insert_resource(frame_state.clone());
let xr_data = XrRenderData {
match graphics::initialize_xr_graphics(primary_window.clone()) {
Ok((
device,
queue,
adapter_info,
render_adapter,
instance,
xr_instance,
xr_session: session,
xr_blend_mode: blend_mode,
xr_resolution: resolution,
xr_format: format,
xr_session_running: session_running,
xr_frame_waiter: frame_waiter,
xr_swapchain: swapchain,
xr_input: input,
xr_views: views,
xr_frame_state: frame_state,
};
app.insert_resource(xr_data);
app.insert_resource(ActionSets(vec![]));
app.add_plugins(RenderPlugin {
render_creation: RenderCreation::Manual(
device,
queue,
adapter_info,
render_adapter,
RenderInstance(Arc::new(instance)),
),
});
app.insert_resource(XrEnableStatus::Enabled);
} else {
app.add_plugins(RenderPlugin::default());
app.insert_resource(XrEnableStatus::Disabled);
session,
blend_mode,
resolution,
format,
session_running,
frame_waiter,
swapchain,
input,
views,
frame_state,
)) => {
// std::thread::sleep(Duration::from_secs(5));
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
debug!("Configured wgpu adapter Features: {:#?}", device.features());
app.insert_resource(xr_instance.clone());
app.insert_resource(session.clone());
app.insert_resource(blend_mode.clone());
app.insert_resource(resolution.clone());
app.insert_resource(format.clone());
app.insert_resource(session_running.clone());
app.insert_resource(frame_waiter.clone());
app.insert_resource(swapchain.clone());
app.insert_resource(input.clone());
app.insert_resource(views.clone());
app.insert_resource(frame_state.clone());
let xr_data = XrRenderData {
xr_instance,
xr_session: session,
xr_blend_mode: blend_mode,
xr_resolution: resolution,
xr_format: format,
xr_session_running: session_running,
xr_frame_waiter: frame_waiter,
xr_swapchain: swapchain,
xr_input: input,
xr_views: views,
xr_frame_state: frame_state,
};
app.insert_resource(xr_data);
app.insert_resource(ActionSets(vec![]));
app.add_plugins(RenderPlugin {
render_creation: RenderCreation::Manual(
device,
queue,
adapter_info,
render_adapter,
RenderInstance(Arc::new(instance)),
),
});
app.insert_resource(XrEnableStatus::Enabled);
}
Err(err) => {
warn!("OpenXR Failed to initialize: {}", err);
app.add_plugins(RenderPlugin::default());
app.insert_resource(XrEnableStatus::Disabled);
}
}
}

Expand Down
30 changes: 25 additions & 5 deletions src/xr_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ use crate::xr_input::controllers::XrControllerType;
use crate::xr_input::oculus_touch::setup_oculus_controller;
use crate::xr_input::xr_camera::{xr_camera_head_sync, Eye, XRProjection, XrCameraBundle};
use bevy::app::{App, PostUpdate, Startup};
use bevy::ecs::entity::Entity;
use bevy::ecs::query::With;
use bevy::ecs::system::Query;
use bevy::log::{info, warn};
use bevy::math::Vec2;
use bevy::prelude::{BuildChildren, Component, Deref, DerefMut, IntoSystemConfigs, Resource};
use bevy::prelude::{Commands, Plugin, PreUpdate, Quat, Res, SpatialBundle, Update, Vec3};
use bevy::render::camera::CameraProjectionPlugin;
Expand Down Expand Up @@ -60,7 +64,7 @@ impl Plugin for OpenXrInput {
}
//adopt any new trackers
app.add_systems(PreUpdate, adopt_open_xr_trackers.run_if(xr_only()));
app.add_systems(PreUpdate, action_set_system);
app.add_systems(PreUpdate, action_set_system.run_if(xr_only()));
app.add_systems(
PreUpdate,
xr_camera_head_sync.run_if(xr_only()).after(xr_begin_frame),
Expand Down Expand Up @@ -88,12 +92,19 @@ fn setup_binding_recommendations(
commands.remove_resource::<InteractionProfileBindings>();
}

fn setup_xr_cameras(mut commands: Commands) {
fn setup_xr_cameras(
mut commands: Commands,
tracking_root_query: Query<Entity, With<OpenXRTrackingRoot>>,
) {
//this needs to do the whole xr tracking volume not just cameras
//get the root?
let tracking_root = commands
.spawn((SpatialBundle::default(), OpenXRTrackingRoot))
.id();

let tracking_root = match tracking_root_query.get_single() {
Ok(e) => e,
Err(_) => commands
.spawn((SpatialBundle::default(), OpenXRTrackingRoot))
.id(),
};
let right = commands
.spawn((XrCameraBundle::new(Eye::Right), OpenXRRightEye))
.id();
Expand All @@ -117,6 +128,15 @@ pub fn action_set_system(action_sets: Res<ActionSets>, session: Res<XrSession>)
}
}

pub trait Vec2Conv {
fn to_vec2(&self) -> Vec2;
}

impl Vec2Conv for openxr::Vector2f {
fn to_vec2(&self) -> Vec2 {
Vec2::new(self.x, self.y)
}
}
pub trait Vec3Conv {
fn to_vec3(&self) -> Vec3;
}
Expand Down

0 comments on commit 265696b

Please sign in to comment.