Fallback to flat when no oxr runtime is found (#51)
* basics done? now to the fun part: changing the ENTIRE lib to work with xr and non xr * updated stuff and renamed file * actually add the renamed file into git lol :3 * made lib fallback to flat when no runtime is found but can't compile with default settings under those circumstances
This commit is contained in:
@@ -35,10 +35,10 @@ pub fn initialize_xr_graphics(
|
||||
vulkan::initialize_xr_graphics(window)
|
||||
}
|
||||
|
||||
pub fn xr_entry() -> xr::Entry {
|
||||
pub fn xr_entry() -> anyhow::Result<xr::Entry> {
|
||||
#[cfg(feature = "linked")]
|
||||
let entry = xr::Entry::linked();
|
||||
let entry = Ok(xr::Entry::linked());
|
||||
#[cfg(not(feature = "linked"))]
|
||||
let entry = unsafe { xr::Entry::load().unwrap() };
|
||||
let entry = unsafe { xr::Entry::load().map_err(|e| anyhow::anyhow!(e)) };
|
||||
entry
|
||||
}
|
||||
|
||||
@@ -44,10 +44,10 @@ pub fn initialize_xr_graphics(
|
||||
)> {
|
||||
use wgpu_hal::{api::Vulkan as V, Api};
|
||||
|
||||
let xr_entry = super::xr_entry();
|
||||
let xr_entry = super::xr_entry()?;
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
xr_entry.initialize_android_loader().unwrap();
|
||||
xr_entry.initialize_android_loader()?;
|
||||
|
||||
let available_extensions = xr_entry.enumerate_extensions()?;
|
||||
assert!(available_extensions.khr_vulkan_enable2);
|
||||
|
||||
196
src/lib.rs
196
src/lib.rs
@@ -2,25 +2,41 @@ mod graphics;
|
||||
pub mod input;
|
||||
pub mod resource_macros;
|
||||
pub mod resources;
|
||||
pub mod xr_init;
|
||||
pub mod xr_input;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::xr_init::RenderRestartPlugin;
|
||||
use crate::xr_input::hands::hand_tracking::DisableHandTracking;
|
||||
use crate::xr_input::oculus_touch::ActionSets;
|
||||
use bevy::app::PluginGroupBuilder;
|
||||
use bevy::ecs::system::SystemState;
|
||||
use bevy::ecs::system::{RunSystemOnce, SystemState};
|
||||
use bevy::prelude::*;
|
||||
use bevy::render::camera::{ManualTextureView, ManualTextureViewHandle, ManualTextureViews};
|
||||
use bevy::render::camera::{
|
||||
CameraPlugin, ManualTextureView, ManualTextureViewHandle, ManualTextureViews,
|
||||
};
|
||||
use bevy::render::globals::GlobalsPlugin;
|
||||
use bevy::render::mesh::morph::MorphPlugin;
|
||||
use bevy::render::mesh::MeshPlugin;
|
||||
use bevy::render::pipelined_rendering::PipelinedRenderingPlugin;
|
||||
use bevy::render::renderer::{render_system, RenderInstance};
|
||||
use bevy::render::render_asset::RenderAssetDependency;
|
||||
use bevy::render::render_resource::ShaderLoader;
|
||||
use bevy::render::renderer::{
|
||||
render_system, RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue,
|
||||
};
|
||||
use bevy::render::settings::RenderCreation;
|
||||
use bevy::render::{Render, RenderApp, RenderPlugin, RenderSet};
|
||||
use bevy::render::view::{self, ViewPlugin, WindowRenderPlugin};
|
||||
use bevy::render::{color, primitives, Render, RenderApp, RenderPlugin, RenderSet};
|
||||
use bevy::window::{PresentMode, PrimaryWindow, RawHandleWrapper};
|
||||
use input::XrInput;
|
||||
use openxr as xr;
|
||||
use resources::*;
|
||||
use xr::FormFactor;
|
||||
use xr_init::{
|
||||
init_non_xr_graphics, update_xr_stuff, xr_only, RenderCreationData, XrEnableRequest,
|
||||
XrEnableStatus, XrRenderData, XrRenderUpdate,
|
||||
};
|
||||
use xr_input::controllers::XrControllerType;
|
||||
use xr_input::hands::emulated::HandEmulationPlugin;
|
||||
use xr_input::hands::hand_tracking::{HandTrackingData, HandTrackingPlugin};
|
||||
@@ -63,14 +79,10 @@ pub struct FutureXrResources(
|
||||
|
||||
impl Plugin for OpenXrPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let future_xr_resources_wrapper = Arc::new(Mutex::new(None));
|
||||
app.insert_resource(FutureXrResources(future_xr_resources_wrapper.clone()));
|
||||
|
||||
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();
|
||||
|
||||
let (
|
||||
if let Ok((
|
||||
device,
|
||||
queue,
|
||||
adapter_info,
|
||||
@@ -87,126 +99,121 @@ impl Plugin for OpenXrPlugin {
|
||||
input,
|
||||
views,
|
||||
frame_state,
|
||||
) = graphics::initialize_xr_graphics(primary_window).unwrap();
|
||||
// std::thread::sleep(Duration::from_secs(5));
|
||||
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
|
||||
debug!("Configured wgpu adapter Features: {:#?}", device.features());
|
||||
let mut future_xr_resources_inner = future_xr_resources_wrapper.lock().unwrap();
|
||||
*future_xr_resources_inner = Some((
|
||||
xr_instance,
|
||||
session,
|
||||
blend_mode,
|
||||
resolution,
|
||||
format,
|
||||
session_running,
|
||||
frame_waiter,
|
||||
swapchain,
|
||||
input,
|
||||
views,
|
||||
frame_state,
|
||||
));
|
||||
app.insert_resource(ActionSets(vec![]));
|
||||
app.add_plugins(RenderPlugin {
|
||||
render_creation: RenderCreation::Manual(
|
||||
device,
|
||||
queue,
|
||||
adapter_info,
|
||||
render_adapter,
|
||||
RenderInstance(Arc::new(instance)),
|
||||
),
|
||||
});
|
||||
)) = 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 {
|
||||
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)),
|
||||
),
|
||||
});
|
||||
} else {
|
||||
app.add_plugins(RenderPlugin::default());
|
||||
app.insert_resource(XrEnableStatus::Disabled);
|
||||
}
|
||||
}
|
||||
|
||||
fn ready(&self, app: &App) -> bool {
|
||||
app.world
|
||||
.get_resource::<FutureXrResources>()
|
||||
.and_then(|frr| frr.0.try_lock().map(|locked| locked.is_some()).ok())
|
||||
.get_resource::<XrEnableStatus>()
|
||||
.map(|frr| *frr != XrEnableStatus::Waiting)
|
||||
.unwrap_or(true)
|
||||
}
|
||||
|
||||
fn finish(&self, app: &mut App) {
|
||||
if let Some(future_renderer_resources) = app.world.remove_resource::<FutureXrResources>() {
|
||||
let (
|
||||
xr_instance,
|
||||
session,
|
||||
blend_mode,
|
||||
resolution,
|
||||
format,
|
||||
session_running,
|
||||
frame_waiter,
|
||||
swapchain,
|
||||
input,
|
||||
views,
|
||||
frame_state,
|
||||
) = future_renderer_resources.0.lock().unwrap().take().unwrap();
|
||||
|
||||
let action_sets = app.world.resource::<ActionSets>().clone();
|
||||
|
||||
app.insert_resource(xr_instance.clone())
|
||||
.insert_resource(session.clone())
|
||||
.insert_resource(blend_mode.clone())
|
||||
.insert_resource(resolution.clone())
|
||||
.insert_resource(format.clone())
|
||||
.insert_resource(session_running.clone())
|
||||
.insert_resource(frame_waiter.clone())
|
||||
.insert_resource(swapchain.clone())
|
||||
.insert_resource(input.clone())
|
||||
.insert_resource(views.clone())
|
||||
.insert_resource(frame_state.clone())
|
||||
.insert_resource(action_sets.clone());
|
||||
let hands = xr_instance.exts().ext_hand_tracking.is_some()
|
||||
&& xr_instance
|
||||
// TODO: Split this up into the indevidual resources
|
||||
if let Some(data) = app.world.get_resource::<XrRenderData>().cloned() {
|
||||
// just calling this stuff because I already had the code, so...
|
||||
app.insert_resource(XrEnableStatus::Enabled);
|
||||
app.world.send_event(XrEnableRequest::TryEnable);
|
||||
app.world.run_system_once(update_xr_stuff);
|
||||
app.insert_resource(XrEnableStatus::Enabled);
|
||||
//
|
||||
let hands = data.xr_instance.exts().ext_hand_tracking.is_some()
|
||||
&& data
|
||||
.xr_instance
|
||||
.supports_hand_tracking(
|
||||
xr_instance
|
||||
data.xr_instance
|
||||
.system(FormFactor::HEAD_MOUNTED_DISPLAY)
|
||||
.unwrap(),
|
||||
)
|
||||
.is_ok_and(|v| v);
|
||||
if hands {
|
||||
app.insert_resource(HandTrackingData::new(&session).unwrap());
|
||||
app.insert_resource(HandTrackingData::new(&data.xr_session).unwrap());
|
||||
} else {
|
||||
app.insert_resource(DisableHandTracking::Both);
|
||||
}
|
||||
|
||||
let (left, right) = swapchain.get_render_views();
|
||||
let (left, right) = data.xr_swapchain.get_render_views();
|
||||
let left = ManualTextureView {
|
||||
texture_view: left.into(),
|
||||
size: *resolution,
|
||||
format: *format,
|
||||
size: *data.xr_resolution,
|
||||
format: *data.xr_format,
|
||||
};
|
||||
let right = ManualTextureView {
|
||||
texture_view: right.into(),
|
||||
size: *resolution,
|
||||
format: *format,
|
||||
size: *data.xr_resolution,
|
||||
format: *data.xr_format,
|
||||
};
|
||||
app.add_systems(PreUpdate, xr_begin_frame);
|
||||
app.add_systems(PreUpdate, xr_begin_frame.run_if(xr_only()));
|
||||
let mut manual_texture_views = app.world.resource_mut::<ManualTextureViews>();
|
||||
manual_texture_views.insert(LEFT_XR_TEXTURE_HANDLE, left);
|
||||
manual_texture_views.insert(RIGHT_XR_TEXTURE_HANDLE, right);
|
||||
drop(manual_texture_views);
|
||||
let render_app = app.sub_app_mut(RenderApp);
|
||||
|
||||
render_app
|
||||
.insert_resource(xr_instance)
|
||||
.insert_resource(session)
|
||||
.insert_resource(blend_mode)
|
||||
.insert_resource(resolution)
|
||||
.insert_resource(format)
|
||||
.insert_resource(session_running)
|
||||
.insert_resource(frame_waiter)
|
||||
.insert_resource(swapchain)
|
||||
.insert_resource(input)
|
||||
.insert_resource(views)
|
||||
.insert_resource(frame_state)
|
||||
.insert_resource(action_sets);
|
||||
|
||||
render_app.insert_resource(data.xr_instance.clone());
|
||||
render_app.insert_resource(data.xr_session.clone());
|
||||
render_app.insert_resource(data.xr_blend_mode.clone());
|
||||
render_app.insert_resource(data.xr_resolution.clone());
|
||||
render_app.insert_resource(data.xr_format.clone());
|
||||
render_app.insert_resource(data.xr_session_running.clone());
|
||||
render_app.insert_resource(data.xr_frame_waiter.clone());
|
||||
render_app.insert_resource(data.xr_swapchain.clone());
|
||||
render_app.insert_resource(data.xr_input.clone());
|
||||
render_app.insert_resource(data.xr_views.clone());
|
||||
render_app.insert_resource(data.xr_frame_state.clone());
|
||||
render_app.insert_resource(XrEnableStatus::Enabled);
|
||||
render_app.add_systems(
|
||||
Render,
|
||||
(
|
||||
post_frame
|
||||
.run_if(xr_only())
|
||||
.before(render_system)
|
||||
.after(RenderSet::ExtractCommands),
|
||||
end_frame.after(render_system),
|
||||
end_frame.run_if(xr_only()).after(render_system),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -221,8 +228,9 @@ impl PluginGroup for DefaultXrPlugins {
|
||||
.build()
|
||||
.disable::<RenderPlugin>()
|
||||
.disable::<PipelinedRenderingPlugin>()
|
||||
.add_before::<RenderPlugin, _>(OpenXrPlugin::default())
|
||||
.add_before::<RenderPlugin, _>(OpenXrPlugin)
|
||||
.add_after::<OpenXrPlugin, _>(OpenXrInput::new(XrControllerType::OculusTouch))
|
||||
.add_before::<OpenXrPlugin, _>(RenderRestartPlugin)
|
||||
.add(HandEmulationPlugin)
|
||||
.add(HandTrackingPlugin)
|
||||
.set(WindowPlugin {
|
||||
|
||||
354
src/xr_init.rs
Normal file
354
src/xr_init.rs
Normal file
@@ -0,0 +1,354 @@
|
||||
// Just a lot of code that is meant for something way more complex but hey.
|
||||
// maybe will work on that soon
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use bevy::{
|
||||
ecs::schedule::{ExecutorKind, ScheduleLabel},
|
||||
prelude::*,
|
||||
render::{
|
||||
extract_resource::{ExtractResource, ExtractResourcePlugin},
|
||||
renderer::{
|
||||
self, RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue,
|
||||
},
|
||||
settings::WgpuSettings,
|
||||
},
|
||||
window::{PrimaryWindow, RawHandleWrapper},
|
||||
};
|
||||
use wgpu::Instance;
|
||||
|
||||
use crate::{
|
||||
graphics,
|
||||
input::XrInput,
|
||||
resources::{
|
||||
XrEnvironmentBlendMode, XrFormat, XrFrameState, XrFrameWaiter, XrInstance, XrResolution,
|
||||
XrSession, XrSessionRunning, XrSwapchain, XrViews,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Resource, Clone)]
|
||||
pub struct RenderCreationData {
|
||||
pub device: RenderDevice,
|
||||
pub queue: RenderQueue,
|
||||
pub adapter_info: RenderAdapterInfo,
|
||||
pub render_adapter: RenderAdapter,
|
||||
pub instance: Arc<Instance>,
|
||||
}
|
||||
|
||||
#[derive(Resource, Clone, ExtractResource)]
|
||||
pub struct XrRenderData {
|
||||
pub xr_instance: XrInstance,
|
||||
pub xr_session: XrSession,
|
||||
pub xr_blend_mode: XrEnvironmentBlendMode,
|
||||
pub xr_resolution: XrResolution,
|
||||
pub xr_format: XrFormat,
|
||||
pub xr_session_running: XrSessionRunning,
|
||||
pub xr_frame_waiter: XrFrameWaiter,
|
||||
pub xr_swapchain: XrSwapchain,
|
||||
pub xr_input: XrInput,
|
||||
pub xr_views: XrViews,
|
||||
pub xr_frame_state: XrFrameState,
|
||||
}
|
||||
|
||||
#[derive(Event, Clone, Copy, Debug)]
|
||||
pub enum XrEnableRequest {
|
||||
TryEnable,
|
||||
TryDisable,
|
||||
}
|
||||
#[derive(Resource, Event, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum XrEnableStatus {
|
||||
Enabled,
|
||||
Disabled,
|
||||
Waiting,
|
||||
}
|
||||
|
||||
#[derive(Resource, Event, Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub enum XrNextEnabledState {
|
||||
Enabled,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
pub struct RenderRestartPlugin;
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct ForceMain;
|
||||
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPreSetup;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrSetup;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPrePostSetup;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPostSetup;
|
||||
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPreCleanup;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrCleanup;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPostCleanup;
|
||||
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPreRenderUpdate;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrRenderUpdate;
|
||||
#[derive(Debug, ScheduleLabel, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct XrPostRenderUpdate;
|
||||
|
||||
pub fn xr_only() -> impl FnMut(Option<Res<'_, XrEnableStatus>>) -> bool {
|
||||
resource_exists_and_equals(XrEnableStatus::Enabled)
|
||||
}
|
||||
|
||||
impl Plugin for RenderRestartPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
add_schedules(app);
|
||||
app.add_plugins(ExtractResourcePlugin::<XrRenderData>::default())
|
||||
.insert_resource(ForceMain)
|
||||
.add_event::<XrEnableRequest>()
|
||||
.add_event::<XrEnableStatus>()
|
||||
.add_systems(
|
||||
PostUpdate,
|
||||
update_xr_stuff.run_if(on_event::<XrEnableRequest>()),
|
||||
)
|
||||
.add_systems(XrPreRenderUpdate, decide_next_xr_state)
|
||||
.add_systems(XrPostRenderUpdate, clear_events)
|
||||
.add_systems(
|
||||
XrRenderUpdate,
|
||||
(
|
||||
cleanup_xr.run_if(resource_exists_and_equals(XrNextEnabledState::Disabled)),
|
||||
// handle_xr_enable_requests,
|
||||
apply_deferred,
|
||||
setup_xr/* .run_if(resource_exists_and_equals(XrEnableStatus::Enabled)) */,
|
||||
)
|
||||
.chain(),
|
||||
)
|
||||
.add_systems(XrCleanup, cleanup_oxr_session);
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_events(mut events: ResMut<Events<XrEnableRequest>>) {
|
||||
events.clear();
|
||||
}
|
||||
|
||||
fn add_schedules(app: &mut App) {
|
||||
let schedules = [
|
||||
Schedule::new(XrPreSetup),
|
||||
Schedule::new(XrSetup),
|
||||
Schedule::new(XrPrePostSetup),
|
||||
Schedule::new(XrPostSetup),
|
||||
Schedule::new(XrPreRenderUpdate),
|
||||
Schedule::new(XrRenderUpdate),
|
||||
Schedule::new(XrPostRenderUpdate),
|
||||
Schedule::new(XrPreCleanup),
|
||||
Schedule::new(XrCleanup),
|
||||
Schedule::new(XrPostCleanup),
|
||||
];
|
||||
for mut schedule in schedules {
|
||||
schedule.set_executor_kind(ExecutorKind::SingleThreaded);
|
||||
schedule.set_apply_final_deferred(true);
|
||||
app.add_schedule(schedule);
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_xr(world: &mut World) {
|
||||
world.run_schedule(XrPreSetup);
|
||||
info!("PreSetup Done");
|
||||
world.run_schedule(XrSetup);
|
||||
info!("Setup Done");
|
||||
world.run_schedule(XrPrePostSetup);
|
||||
info!("PrePostSetup Done");
|
||||
world.run_schedule(XrPostSetup);
|
||||
info!("PostSetup Done");
|
||||
}
|
||||
fn cleanup_xr(world: &mut World) {
|
||||
world.run_schedule(XrPreCleanup);
|
||||
world.run_schedule(XrCleanup);
|
||||
world.run_schedule(XrPostCleanup);
|
||||
}
|
||||
|
||||
fn cleanup_oxr_session(xr_status: Option<Res<XrEnableStatus>>, session: Option<ResMut<XrSession>>) {
|
||||
if let (Some(XrEnableStatus::Disabled), Some(s)) = (xr_status.map(|v| v.into_inner()), session)
|
||||
{
|
||||
s.into_inner().request_exit().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_xr_stuff(world: &mut World) {
|
||||
world.run_schedule(XrPreRenderUpdate);
|
||||
world.run_schedule(XrRenderUpdate);
|
||||
world.run_schedule(XrPostRenderUpdate);
|
||||
}
|
||||
|
||||
// fn handle_xr_enable_requests(
|
||||
// primary_window: Query<&RawHandleWrapper, With<PrimaryWindow>>,
|
||||
// mut commands: Commands,
|
||||
// next_state: Res<XrNextEnabledState>,
|
||||
// on_main: Option<NonSend<ForceMain>>,
|
||||
// ) {
|
||||
// // Just to force this system onto the main thread because of unsafe code
|
||||
// let _ = on_main;
|
||||
//
|
||||
// commands.insert_resource(XrEnableStatus::Waiting);
|
||||
// let (creation_data, xr_data) = match next_state.into_inner() {
|
||||
// XrNextEnabledState::Enabled => {
|
||||
// let (
|
||||
// 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.get_single().ok().cloned())
|
||||
// .unwrap();
|
||||
//
|
||||
// commands.insert_resource(XrEnableStatus::Enabled);
|
||||
// (
|
||||
// RenderCreationData {
|
||||
// device,
|
||||
// queue,
|
||||
// adapter_info,
|
||||
// render_adapter,
|
||||
// instance: Arc::new(instance),
|
||||
// },
|
||||
// Some(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,
|
||||
// }),
|
||||
// )
|
||||
// }
|
||||
// XrNextEnabledState::Disabled => (
|
||||
// init_non_xr_graphics(primary_window.get_single().ok().cloned()),
|
||||
// None,
|
||||
// ),
|
||||
// };
|
||||
//
|
||||
// commands.insert_resource(creation_data.device);
|
||||
// commands.insert_resource(creation_data.queue);
|
||||
// commands.insert_resource(RenderInstance(creation_data.instance));
|
||||
// commands.insert_resource(creation_data.adapter_info);
|
||||
// commands.insert_resource(creation_data.render_adapter);
|
||||
//
|
||||
// if let Some(xr_data) = xr_data {
|
||||
// // TODO: Remove this lib.rs:144
|
||||
// commands.insert_resource(xr_data.clone());
|
||||
//
|
||||
// commands.insert_resource(xr_data.xr_instance);
|
||||
// commands.insert_resource(xr_data.xr_session);
|
||||
// commands.insert_resource(xr_data.xr_blend_mode);
|
||||
// commands.insert_resource(xr_data.xr_resolution);
|
||||
// commands.insert_resource(xr_data.xr_format);
|
||||
// commands.insert_resource(xr_data.xr_session_running);
|
||||
// commands.insert_resource(xr_data.xr_frame_waiter);
|
||||
// commands.insert_resource(xr_data.xr_input);
|
||||
// commands.insert_resource(xr_data.xr_views);
|
||||
// commands.insert_resource(xr_data.xr_frame_state);
|
||||
// commands.insert_resource(xr_data.xr_swapchain);
|
||||
// } else {
|
||||
// commands.remove_resource::<XrRenderData>();
|
||||
//
|
||||
// commands.remove_resource::<XrInstance>();
|
||||
// commands.remove_resource::<XrSession>();
|
||||
// commands.remove_resource::<XrEnvironmentBlendMode>();
|
||||
// commands.remove_resource::<XrResolution>();
|
||||
// commands.remove_resource::<XrFormat>();
|
||||
// commands.remove_resource::<XrSessionRunning>();
|
||||
// commands.remove_resource::<XrFrameWaiter>();
|
||||
// commands.remove_resource::<XrSwapchain>();
|
||||
// commands.remove_resource::<XrInput>();
|
||||
// commands.remove_resource::<XrViews>();
|
||||
// commands.remove_resource::<XrFrameState>();
|
||||
// }
|
||||
// }
|
||||
|
||||
fn decide_next_xr_state(
|
||||
mut commands: Commands,
|
||||
mut events: EventReader<XrEnableRequest>,
|
||||
xr_status: Option<Res<XrEnableStatus>>,
|
||||
) {
|
||||
info!("hm");
|
||||
let request = match events.read().next() {
|
||||
Some(v) => v,
|
||||
None => return,
|
||||
};
|
||||
info!("ok");
|
||||
match (request, xr_status.as_deref()) {
|
||||
(XrEnableRequest::TryEnable, Some(XrEnableStatus::Enabled)) => {
|
||||
info!("Xr Already Enabled! ignoring request");
|
||||
return;
|
||||
}
|
||||
(XrEnableRequest::TryDisable, Some(XrEnableStatus::Disabled)) => {
|
||||
info!("Xr Already Disabled! ignoring request");
|
||||
return;
|
||||
}
|
||||
(_, Some(XrEnableStatus::Waiting)) => {
|
||||
info!("Already Handling Request! ignoring request");
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let r = match request {
|
||||
XrEnableRequest::TryEnable => XrNextEnabledState::Enabled,
|
||||
XrEnableRequest::TryDisable => XrNextEnabledState::Disabled,
|
||||
};
|
||||
info!("{:#?}", r);
|
||||
commands.insert_resource(r);
|
||||
}
|
||||
|
||||
pub fn init_non_xr_graphics(primary_window: Option<RawHandleWrapper>) -> RenderCreationData {
|
||||
let settings = WgpuSettings::default();
|
||||
|
||||
let async_renderer = async move {
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||
// Probably a bad idea unwraping here but on the other hand no backends
|
||||
backends: settings.backends.unwrap(),
|
||||
dx12_shader_compiler: settings.dx12_shader_compiler.clone(),
|
||||
});
|
||||
let surface = primary_window.map(|wrapper| unsafe {
|
||||
// SAFETY: Plugins should be set up on the main thread.
|
||||
let handle = wrapper.get_handle();
|
||||
instance
|
||||
.create_surface(&handle)
|
||||
.expect("Failed to create wgpu surface")
|
||||
});
|
||||
|
||||
let request_adapter_options = wgpu::RequestAdapterOptions {
|
||||
power_preference: settings.power_preference,
|
||||
compatible_surface: surface.as_ref(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (device, queue, adapter_info, render_adapter) =
|
||||
renderer::initialize_renderer(&instance, &settings, &request_adapter_options).await;
|
||||
debug!("Configured wgpu adapter Limits: {:#?}", device.limits());
|
||||
debug!("Configured wgpu adapter Features: {:#?}", device.features());
|
||||
RenderCreationData {
|
||||
device,
|
||||
queue,
|
||||
adapter_info,
|
||||
render_adapter,
|
||||
instance: Arc::new(instance),
|
||||
}
|
||||
};
|
||||
// No need for wasm in bevy_oxr web xr would be a different crate
|
||||
futures_lite::future::block_on(async_renderer)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use bevy::{prelude::*, utils::HashMap};
|
||||
use openxr as xr;
|
||||
use xr::{Action, Binding, Haptic, Posef};
|
||||
|
||||
use crate::resources::{XrInstance, XrSession};
|
||||
use crate::{resources::{XrInstance, XrSession}, xr_init::XrPrePostSetup};
|
||||
|
||||
use super::oculus_touch::ActionSets;
|
||||
|
||||
@@ -12,11 +12,12 @@ impl Plugin for OpenXrActionsPlugin {
|
||||
app.insert_resource(SetupActionSets {
|
||||
sets: HashMap::new(),
|
||||
});
|
||||
app.add_systems(PostStartup, setup_oxr_actions);
|
||||
app.add_systems(XrPrePostSetup, setup_oxr_actions);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup_oxr_actions(world: &mut World) {
|
||||
info!("huh?!");
|
||||
let actions = world.remove_resource::<SetupActionSets>().unwrap();
|
||||
let instance = world.get_resource::<XrInstance>().unwrap();
|
||||
let session = world.get_resource::<XrSession>().unwrap();
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use bevy::ecs::schedule::IntoSystemConfigs;
|
||||
use bevy::log::{debug, info};
|
||||
use bevy::prelude::{
|
||||
Color, Gizmos, GlobalTransform, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3, With,
|
||||
Without,
|
||||
};
|
||||
|
||||
use crate::xr_init::xr_only;
|
||||
use crate::{
|
||||
input::XrInput,
|
||||
resources::{XrFrameState, XrSession},
|
||||
@@ -25,34 +27,41 @@ pub struct OpenXrDebugRenderer;
|
||||
|
||||
impl Plugin for OpenXrDebugRenderer {
|
||||
fn build(&self, app: &mut bevy::prelude::App) {
|
||||
app.add_systems(Update, draw_gizmos);
|
||||
app.add_systems(Update, draw_gizmos.run_if(xr_only()));
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments, clippy::complexity)]
|
||||
pub fn draw_gizmos(
|
||||
mut gizmos: Gizmos,
|
||||
oculus_controller: Res<OculusController>,
|
||||
frame_state: Res<XrFrameState>,
|
||||
xr_input: Res<XrInput>,
|
||||
session: Res<XrSession>,
|
||||
tracking_root_query: Query<(
|
||||
tracking_root_query: Query<
|
||||
&mut Transform,
|
||||
With<OpenXRTrackingRoot>,
|
||||
Without<OpenXRLeftController>,
|
||||
Without<OpenXRRightController>,
|
||||
)>,
|
||||
left_controller_query: Query<(
|
||||
(
|
||||
With<OpenXRTrackingRoot>,
|
||||
Without<OpenXRLeftController>,
|
||||
Without<OpenXRRightController>,
|
||||
),
|
||||
>,
|
||||
left_controller_query: Query<
|
||||
&GlobalTransform,
|
||||
With<OpenXRLeftController>,
|
||||
Without<OpenXRRightController>,
|
||||
Without<OpenXRTrackingRoot>,
|
||||
)>,
|
||||
right_controller_query: Query<(
|
||||
(
|
||||
With<OpenXRLeftController>,
|
||||
Without<OpenXRRightController>,
|
||||
Without<OpenXRTrackingRoot>,
|
||||
),
|
||||
>,
|
||||
right_controller_query: Query<
|
||||
&GlobalTransform,
|
||||
With<OpenXRRightController>,
|
||||
Without<OpenXRLeftController>,
|
||||
Without<OpenXRTrackingRoot>,
|
||||
)>,
|
||||
(
|
||||
With<OpenXRRightController>,
|
||||
Without<OpenXRLeftController>,
|
||||
Without<OpenXRTrackingRoot>,
|
||||
),
|
||||
>,
|
||||
action_sets: Res<XrActionSets>,
|
||||
) {
|
||||
// if let Some(hand_tracking) = hand_tracking {
|
||||
@@ -89,7 +98,7 @@ pub fn draw_gizmos(
|
||||
match root {
|
||||
Ok(position) => {
|
||||
gizmos.circle(
|
||||
position.0.translation
|
||||
position.translation
|
||||
+ Vec3 {
|
||||
x: 0.0,
|
||||
y: 0.01,
|
||||
@@ -107,7 +116,7 @@ pub fn draw_gizmos(
|
||||
let left_transform = left_controller_query.get_single();
|
||||
match left_transform {
|
||||
Ok(left_entity) => {
|
||||
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, left_entity.0);
|
||||
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, left_entity);
|
||||
}
|
||||
Err(_) => debug!("no left controller entity for debug gizmos"),
|
||||
}
|
||||
@@ -115,7 +124,7 @@ pub fn draw_gizmos(
|
||||
let right_transform = right_controller_query.get_single();
|
||||
match right_transform {
|
||||
Ok(right_entity) => {
|
||||
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, right_entity.0);
|
||||
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, right_entity);
|
||||
}
|
||||
Err(_) => debug!("no right controller entity for debug gizmos"),
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ use openxr::{ActionTy, HandJoint};
|
||||
|
||||
use super::common::{get_bone_gizmo_style, HandBoneRadius};
|
||||
use crate::{
|
||||
xr_init::{xr_only, XrSetup},
|
||||
resources::{XrInstance, XrSession},
|
||||
xr_input::{
|
||||
actions::{
|
||||
@@ -27,8 +28,11 @@ pub struct HandEmulationPlugin;
|
||||
|
||||
impl Plugin for HandEmulationPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PreUpdate, update_hand_skeleton_from_emulated);
|
||||
app.add_systems(Startup, setup_hand_emulation_action_set);
|
||||
app.add_systems(
|
||||
Update,
|
||||
update_hand_skeleton_from_emulated.run_if(xr_only()),
|
||||
);
|
||||
app.add_systems(XrSetup, setup_hand_emulation_action_set);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +91,7 @@ fn setup_hand_emulation_action_set(mut action_sets: ResMut<SetupActionSets>) {
|
||||
ActionHandednes::Double,
|
||||
);
|
||||
|
||||
suggest_oculus_touch_profile(&mut action_set);
|
||||
suggest_oculus_touch_profile(action_set);
|
||||
}
|
||||
|
||||
pub struct EmulatedHandPoseData {}
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::{
|
||||
xr_input::{
|
||||
hands::HandBone, trackers::OpenXRTrackingRoot, Hand, QuatConv,
|
||||
Vec3Conv,
|
||||
},
|
||||
}, xr_init::xr_only,
|
||||
};
|
||||
use super::common::HandBoneRadius;
|
||||
|
||||
@@ -128,7 +128,7 @@ impl Plugin for HandTrackingPlugin {
|
||||
(
|
||||
update_hand_bones.run_if(|dh: Option<Res<DisableHandTracking>>| {
|
||||
!dh.is_some_and(|v| *v == DisableHandTracking::Both)
|
||||
}),
|
||||
}).run_if(xr_only()),
|
||||
update_tracking_state_on_disable,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -9,13 +9,14 @@ pub mod prototype_locomotion;
|
||||
pub mod trackers;
|
||||
pub mod xr_camera;
|
||||
|
||||
use crate::xr_init::{XrPostSetup, XrSetup, xr_only};
|
||||
use crate::resources::{XrInstance, XrSession};
|
||||
use crate::xr_begin_frame;
|
||||
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::log::warn;
|
||||
use bevy::log::{warn, info};
|
||||
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;
|
||||
@@ -52,27 +53,27 @@ impl Plugin for OpenXrInput {
|
||||
app.add_plugins(CameraProjectionPlugin::<XRProjection>::default());
|
||||
app.add_plugins(OpenXrActionsPlugin);
|
||||
app.add_systems(
|
||||
PreUpdate,
|
||||
(post_action_setup_oculus_controller.after(setup_oxr_actions),),
|
||||
XrPostSetup,
|
||||
post_action_setup_oculus_controller,
|
||||
);
|
||||
match self.controller_type {
|
||||
XrControllerType::OculusTouch => {
|
||||
app.add_systems(Startup, setup_oculus_controller);
|
||||
app.add_systems(XrSetup, setup_oculus_controller);
|
||||
}
|
||||
}
|
||||
//adopt any new trackers
|
||||
app.add_systems(PreUpdate, adopt_open_xr_trackers);
|
||||
app.add_systems(PreUpdate, action_set_system);
|
||||
app.add_systems(PreUpdate, xr_camera_head_sync.after(xr_begin_frame));
|
||||
app.add_systems(PreUpdate, adopt_open_xr_trackers.run_if(xr_only()));
|
||||
// app.add_systems(PreUpdate, action_set_system);
|
||||
app.add_systems(PreUpdate, xr_camera_head_sync.run_if(xr_only()).after(xr_begin_frame));
|
||||
//update controller trackers
|
||||
app.add_systems(Update, update_open_xr_controllers);
|
||||
app.add_systems(Update, update_open_xr_controllers.run_if(xr_only()));
|
||||
app.add_systems(
|
||||
PostUpdate,
|
||||
update_frusta::<XRProjection>
|
||||
.after(TransformSystem::TransformPropagate)
|
||||
.before(VisibilitySystems::UpdatePerspectiveFrusta),
|
||||
);
|
||||
app.add_systems(Startup, setup_xr_cameras);
|
||||
app.add_systems(XrSetup, setup_xr_cameras);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +89,7 @@ fn setup_binding_recommendations(
|
||||
}
|
||||
|
||||
fn setup_xr_cameras(mut commands: Commands) {
|
||||
info!("WTF?!");
|
||||
//this needs to do the whole xr tracking volume not just cameras
|
||||
//get the root?
|
||||
let tracking_root = commands
|
||||
|
||||
Reference in New Issue
Block a user