Skip to content

Commit

Permalink
session restarting workscargo run --release --example xr! views fixed…
Browse files Browse the repository at this point in the history
… and late latching for views
  • Loading branch information
Schmarni-Dev committed Feb 22, 2024
1 parent 3f27c8d commit 889ee3c
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 62 deletions.
67 changes: 34 additions & 33 deletions examples/xr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_oxr::graphics::XrAppInfo;
use bevy_oxr::input::XrInput;
use bevy_oxr::resources::{XrFrameState, XrSession};

use bevy_oxr::xr_init::{xr_only, EndXrSession, StartXrSession};
use bevy_oxr::xr_init::{xr_only, EndXrSession, StartXrSession, XrSetup};
use bevy_oxr::xr_input::actions::XrActionSets;
use bevy_oxr::xr_input::hands::common::HandInputDebugRenderer;
use bevy_oxr::xr_input::interactions::{
Expand All @@ -33,39 +33,39 @@ fn main() {
},
..default()
})
//.add_plugins(OpenXrDebugRenderer) //new debug renderer adds gizmos to
// .add_plugins(OpenXrDebugRenderer) //new debug renderer adds gizmos to
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_systems(Startup, setup)
// .add_systems(Update, proto_locomotion.run_if(xr_only()))
// .insert_resource(PrototypeLocomotionConfig::default())
.add_systems(Startup, spawn_controllers_example)
// .add_plugins(HandInputDebugRenderer)
// .add_systems(
// Update,
// draw_interaction_gizmos
// .after(update_interactable_states)
// .run_if(xr_only()),
// )
// .add_systems(
// Update,
// draw_socket_gizmos
// .after(update_interactable_states)
// .run_if(xr_only()),
// )
// .add_systems(
// Update,
// interactions
// .before(update_interactable_states)
// .run_if(xr_only()),
// )
// .add_systems(
// Update,
// socket_interactions.before(update_interactable_states),
// )
// .add_systems(Update, prototype_interaction_input.run_if(xr_only()))
// .add_systems(Update, update_interactable_states)
// .add_systems(Update, update_grabbables.after(update_interactable_states))
.add_systems(Update, proto_locomotion.run_if(xr_only()))
.insert_resource(PrototypeLocomotionConfig::default())
.add_systems(XrSetup, spawn_controllers_example)
.add_plugins(HandInputDebugRenderer)
.add_systems(
Update,
draw_interaction_gizmos
.after(update_interactable_states)
.run_if(xr_only()),
)
.add_systems(
Update,
draw_socket_gizmos
.after(update_interactable_states)
.run_if(xr_only()),
)
.add_systems(
Update,
interactions
.before(update_interactable_states)
.run_if(xr_only()),
)
.add_systems(
Update,
socket_interactions.before(update_interactable_states),
)
.add_systems(Update, prototype_interaction_input.run_if(xr_only()))
.add_systems(Update, update_interactable_states)
.add_systems(Update, update_grabbables.after(update_interactable_states))
.add_systems(Update, start_stop_session)
.add_event::<InteractionEvent>()
.run();
Expand Down Expand Up @@ -170,21 +170,22 @@ fn spawn_controllers_example(mut commands: Commands) {
));
}

#[allow(clippy::type_complexity)]
fn prototype_interaction_input(
oculus_controller: Res<OculusController>,
frame_state: Res<XrFrameState>,
xr_input: Res<XrInput>,
session: Res<XrSession>,
mut right_interactor_query: Query<
(&mut XRInteractorState),
&mut XRInteractorState,
(
With<XRDirectInteractor>,
With<OpenXRRightController>,
Without<OpenXRLeftController>,
),
>,
mut left_interactor_query: Query<
(&mut XRInteractorState),
&mut XRInteractorState,
(
With<XRRayInteractor>,
With<OpenXRLeftController>,
Expand Down
57 changes: 38 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::atomic::AtomicBool;

use crate::xr_init::{StartXrSession, XrInitPlugin};
use crate::xr_input::oculus_touch::ActionSets;
use crate::xr_input::trackers::verify_quat;
use bevy::app::{AppExit, PluginGroupBuilder};
use bevy::core::TaskPoolThreadAssignmentPolicy;
use bevy::ecs::system::SystemState;
Expand All @@ -36,6 +37,7 @@ use xr_input::hands::emulated::HandEmulationPlugin;
use xr_input::hands::hand_tracking::HandTrackingPlugin;
use xr_input::hands::HandPlugin;
use xr_input::xr_camera::XrCameraPlugin;
use xr_input::XrInputPlugin;

const VIEW_TYPE: xr::ViewConfigurationType = xr::ViewConfigurationType::PRIMARY_STEREO;

Expand Down Expand Up @@ -129,15 +131,15 @@ impl Plugin for OpenXrPlugin {
.after(xr_poll_events),
);
let render_app = app.sub_app_mut(RenderApp);
render_app.add_systems(
Render,
xr_begin_frame
.run_if(xr_only())
.run_if(xr_after_wait_only())
// .run_if(xr_render_only())
.after(RenderSet::ExtractCommands)
.before(xr_pre_frame),
);
// render_app.add_systems(
// Render,
// xr_begin_frame
// .run_if(xr_only())
// .run_if(xr_after_wait_only())
// // .run_if(xr_render_only())
// .after(RenderSet::ExtractCommands)
// .before(xr_pre_frame),
// );
render_app.add_systems(
Render,
xr_pre_frame
Expand Down Expand Up @@ -174,7 +176,8 @@ impl Plugin for OpenXrPlugin {
}

fn clean_resources_render(mut cmds: &mut World) {
let session = cmds.remove_resource::<XrSession>().unwrap();
// let session = cmds.remove_resource::<XrSession>().unwrap();
cmds.remove_resource::<XrSession>();
cmds.remove_resource::<XrResolution>();
cmds.remove_resource::<XrFormat>();
// cmds.remove_resource::<XrSessionRunning>();
Expand All @@ -190,7 +193,7 @@ fn clean_resources_render(mut cmds: &mut World) {
warn!("Cleanup Resources Render");
}
fn clean_resources(mut cmds: &mut World) {
let session = cmds.remove_resource::<XrSession>().unwrap();
cmds.remove_resource::<XrSession>();
cmds.remove_resource::<XrResolution>();
cmds.remove_resource::<XrFormat>();
// cmds.remove_resource::<XrSessionRunning>();
Expand Down Expand Up @@ -257,14 +260,14 @@ impl PluginGroup for DefaultXrPlugins {
app_info: self.app_info.clone(),
})
.add_after::<OpenXrPlugin, _>(XrInitPlugin)
// .add(XrInput)
// .add(XrActionsPlugin)
.add(XrInputPlugin)
.add(XrActionsPlugin)
.add(XrCameraPlugin)
.add_before::<OpenXrPlugin, _>(XrEarlyInitPlugin)
// .add(HandPlugin)
// .add(HandTrackingPlugin)
// .add(HandEmulationPlugin)
// .add(PassthroughPlugin)
.add(HandPlugin)
.add(HandTrackingPlugin)
.add(HandEmulationPlugin)
.add(PassthroughPlugin)
.add(XrResourcePlugin)
.set(WindowPlugin {
#[cfg(not(target_os = "android"))]
Expand Down Expand Up @@ -369,6 +372,7 @@ pub fn xr_wait_frame(
**world.get_resource_mut::<XrShouldRender>().unwrap() = should_render;
**world.get_resource_mut::<XrHasWaited>().unwrap() = true;
}
world.get_resource::<XrSwapchain>().unwrap().begin().unwrap();
}

pub fn xr_pre_frame(
Expand Down Expand Up @@ -458,11 +462,26 @@ pub fn locate_views(
xr_frame_state.predicted_display_time,
&input.stage,
) {
Ok(this) => this,
Ok(this) => this
.1
.into_iter()
.map(|mut view| {
use crate::prelude::*;
let quat = view.pose.orientation.to_quat();
let fixed_quat = verify_quat(quat);
let oxr_quat = xr::Quaternionf {
x: fixed_quat.x,
y: fixed_quat.y,
z: fixed_quat.z,
w: fixed_quat.w,
};
view.pose.orientation = oxr_quat;
view
})
.collect(),
Err(err) => {
warn!("error: {}", err);
return;
}
}
.1;
}
12 changes: 9 additions & 3 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ pub struct SwapchainInner<G: xr::Graphics> {
pub(crate) buffers: Vec<wgpu::Texture>,
pub(crate) image_index: Mutex<usize>,
}
impl<G: xr::Graphics> Drop for SwapchainInner<G> {
fn drop(&mut self) {
for _ in 0..self.buffers.len() {
let v = self.buffers.remove(0);
Box::leak(Box::new(v));
}
}
}

impl<G: xr::Graphics> SwapchainInner<G> {
fn begin(&self) -> xr::Result<()> {
Expand Down Expand Up @@ -204,9 +212,7 @@ impl<G: xr::Graphics> SwapchainInner<G> {
predicted_display_time,
environment_blend_mode,
&[
&CompositionLayerPassthrough::from_xr_passthrough_layer(
pass,
),
&CompositionLayerPassthrough::from_xr_passthrough_layer(pass),
&xr::CompositionLayerProjection::new()
.layer_flags(CompositionLayerFlags::BLEND_TEXTURE_SOURCE_ALPHA)
.space(stage)
Expand Down
3 changes: 2 additions & 1 deletion src/xr_init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl Plugin for XrInitPlugin {
);
app.add_systems(XrSetup, setup_manual_texture_views);
app.add_systems(XrCleanup, set_cleanup_res);
app.add_systems(PreUpdate, remove_cleanup_res.before(cleanup_xr));
let render_app = app.sub_app_mut(RenderApp);
render_app.add_systems(
Render,
Expand Down Expand Up @@ -220,5 +221,5 @@ fn stop_xr_session(session: ResMut<XrSession>, mut status: ResMut<XrStatus>) {
error!("Error while trying to request session exit: {}", err)
}
}
*status = XrStatus::Enabling;
*status = XrStatus::Disabling;
}
12 changes: 9 additions & 3 deletions src/xr_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,26 @@ use bevy::utils::HashMap;
use openxr::Binding;

use self::actions::{setup_oxr_actions, XrActionsPlugin};
use self::oculus_touch::{init_subaction_path, post_action_setup_oculus_controller, ActionSets};
use self::oculus_touch::{init_subaction_path, post_action_setup_oculus_controller, ActionSets, OculusController};
use self::trackers::{
adopt_open_xr_trackers, update_open_xr_controllers, OpenXRLeftEye, OpenXRRightEye,
OpenXRTrackingRoot,
};
use self::xr_camera::{/* GlobalTransformExtract, TransformExtract, */ XrCamera};

#[derive(Copy, Clone)]
pub struct XrInput;
pub struct XrInputPlugin;
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Component)]
pub enum Hand {
Left,
Right,
}

impl Plugin for XrInput {
impl Plugin for XrInputPlugin {
fn build(&self, app: &mut App) {
app.add_systems(XrPostSetup, post_action_setup_oculus_controller);
app.add_systems(XrSetup, setup_oculus_controller);
app.add_systems(XrCleanup, cleanup_oculus_controller);
//adopt any new trackers
app.add_systems(PreUpdate, adopt_open_xr_trackers.run_if(xr_only()));
// app.add_systems(PreUpdate, action_set_system.run_if(xr_only()));
Expand All @@ -61,6 +62,10 @@ impl Plugin for XrInput {
}
}

fn cleanup_oculus_controller(mut commands: Commands) {
commands.remove_resource::<OculusController>();
}

fn cleanup_xr_root(
mut commands: Commands,
tracking_root_query: Query<Entity, With<OpenXRTrackingRoot>>,
Expand All @@ -74,6 +79,7 @@ fn setup_xr_root(
tracking_root_query: Query<Entity, With<OpenXRTrackingRoot>>,
) {
if tracking_root_query.get_single().is_err() {
info!("Creating XrTrackingRoot!");
commands.spawn((SpatialBundle::default(), OpenXRTrackingRoot));
}
}
Expand Down
Loading

0 comments on commit 889ee3c

Please sign in to comment.