pull changes from webxr-refactor

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2024-06-05 00:32:38 +02:00
6 changed files with 60 additions and 33 deletions

View File

@@ -37,17 +37,21 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let mut white: StandardMaterial = Color::WHITE.into();
white.unlit = true;
// circular base // circular base
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Circle::new(4.0)), mesh: meshes.add(Circle::new(4.0)),
material: materials.add(Color::WHITE), material: materials.add(white),
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
..default() ..default()
}); });
let mut cube_mat: StandardMaterial = Color::rgb_u8(124, 144, 255).into();
cube_mat.unlit = true;
// cube // cube
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::rgb_u8(124, 144, 255)), material: materials.add(cube_mat),
transform: Transform::from_xyz(0.0, 0.5, 0.0), transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default() ..default()
}); });

View File

@@ -17,8 +17,8 @@ fn main() {
fn handle_input( fn handle_input(
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
mut end: EventWriter<bevy_xr::session::EndXrSession>, mut end: EventWriter<bevy_xr::session::EndXrSession>,
mut destroy: EventWriter<bevy_xr::session::DestroyXrSession>, mut _destroy: EventWriter<bevy_xr::session::DestroyXrSession>,
mut begin: EventWriter<bevy_xr::session::BeginXrSession>, mut _begin: EventWriter<bevy_xr::session::BeginXrSession>,
mut create: EventWriter<bevy_xr::session::CreateXrSession>, mut create: EventWriter<bevy_xr::session::CreateXrSession>,
state: Res<XrStatus>, state: Res<XrStatus>,
) { ) {
@@ -26,14 +26,6 @@ fn handle_input(
info!("sending end"); info!("sending end");
end.send_default(); end.send_default();
} }
if keys.just_pressed(KeyCode::KeyD) {
info!("sending destroy");
destroy.send_default();
}
if keys.just_pressed(KeyCode::KeyB) {
info!("sending begin");
begin.send_default();
}
if keys.just_pressed(KeyCode::KeyC) { if keys.just_pressed(KeyCode::KeyC) {
info!("sending create"); info!("sending create");
create.send_default(); create.send_default();

View File

@@ -40,7 +40,7 @@ fn spawn_default_hands(
session: Res<OxrSession>, session: Res<OxrSession>,
root: Query<Entity, With<OxrTrackingRoot>>, root: Query<Entity, With<OxrTrackingRoot>>,
) { ) {
dbg!("spawning default hands"); debug!("spawning default hands");
let Ok(root) = root.get_single() else { let Ok(root) = root.get_single() else {
error!("unable to get tracking root, skipping hand creation"); error!("unable to get tracking root, skipping hand creation");
return; return;
@@ -118,7 +118,7 @@ fn clean_up_default_hands(
query: Query<Entity, Or<(With<DefaultHandTracker>, With<DefaultHandBone>)>>, query: Query<Entity, Or<(With<DefaultHandTracker>, With<DefaultHandBone>)>>,
) { ) {
for e in &query { for e in &query {
dbg!("removing default hand entity"); debug!("removing default hand entity");
cmds.entity(e).despawn_recursive(); cmds.entity(e).despawn_recursive();
} }
} }

View File

@@ -511,7 +511,6 @@ pub fn poll_events(
SessionState::IDLE => { SessionState::IDLE => {
if *status == XrStatus::Available { if *status == XrStatus::Available {
session_status_events.send(OxrSessionStatusEvent::Created); session_status_events.send(OxrSessionStatusEvent::Created);
info!("sending create info");
} }
XrStatus::Idle XrStatus::Idle
} }
@@ -522,7 +521,6 @@ pub fn poll_events(
SessionState::STOPPING => XrStatus::Stopping, SessionState::STOPPING => XrStatus::Stopping,
SessionState::EXITING | SessionState::LOSS_PENDING => { SessionState::EXITING | SessionState::LOSS_PENDING => {
session_status_events.send(OxrSessionStatusEvent::AboutToBeDestroyed); session_status_events.send(OxrSessionStatusEvent::AboutToBeDestroyed);
info!("sending destroy info");
XrStatus::Exiting XrStatus::Exiting
} }
_ => unreachable!(), _ => unreachable!(),
@@ -557,5 +555,4 @@ pub fn destroy_xr_session_render(world: &mut World) {
world.run_system_once(apply_deferred); world.run_system_once(apply_deferred);
world.remove_resource::<OxrSession>(); world.remove_resource::<OxrSession>();
world.insert_resource(OxrSessionStarted(false)); world.insert_resource(OxrSessionStarted(false));
info!("Render App destroy");
} }

View File

@@ -1,13 +1,15 @@
use bevy::{ use bevy::{
ecs::query::QuerySingleError, app::{MainScheduleOrder, SubApp},
ecs::{query::QuerySingleError, schedule::MainThreadExecutor},
prelude::*, prelude::*,
render::{ render::{
camera::{ManualTextureView, ManualTextureViewHandle, ManualTextureViews, RenderTarget}, camera::{ManualTextureView, ManualTextureViewHandle, ManualTextureViews, RenderTarget},
extract_resource::ExtractResourcePlugin, extract_resource::ExtractResourcePlugin,
pipelined_rendering::PipelinedRenderingPlugin, pipelined_rendering::{PipelinedRenderingPlugin, RenderAppChannels, RenderExtractApp},
view::ExtractedView, view::ExtractedView,
Render, RenderApp, RenderSet, Render, RenderApp, RenderSet,
}, },
tasks::ComputeTaskPool,
transform::TransformSystem, transform::TransformSystem,
}; };
use bevy_xr::{ use bevy_xr::{
@@ -35,6 +37,20 @@ impl Plugin for OxrRenderPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if app.is_plugin_added::<PipelinedRenderingPlugin>() { if app.is_plugin_added::<PipelinedRenderingPlugin>() {
app.init_resource::<Pipelined>(); app.init_resource::<Pipelined>();
let mut schedule_order = app.world.resource_mut::<MainScheduleOrder>();
if let Some(pos) = schedule_order
.labels
.iter()
.position(|label| (**label).eq(&OxrLast))
{
schedule_order.labels.remove(pos);
}
if let Some(sub_app) = app.remove_sub_app(RenderExtractApp) {
app.insert_sub_app(RenderExtractApp, SubApp::new(sub_app.app, update_rendering));
}
} }
app.add_plugins(( app.add_plugins((
@@ -82,11 +98,6 @@ impl Plugin for OxrRenderPlugin {
) )
.add_systems(Last, wait_frame.run_if(session_started)); .add_systems(Last, wait_frame.run_if(session_started));
app.sub_app_mut(RenderApp) app.sub_app_mut(RenderApp)
.add_systems(
Render,
(|q: Query<&XrCamera>| info!("cams render: {}", q.iter().len()))
.in_set(OxrRenderBegin),
)
.add_systems( .add_systems(
Render, Render,
( (
@@ -158,6 +169,31 @@ impl Plugin for OxrRenderPlugin {
} }
} }
// This function waits for the rendering world to be received,
// runs extract, and then sends the rendering world back to the render thread.
//
// modified pipelined rendering extract function
fn update_rendering(app_world: &mut World, _sub_app: &mut App) {
app_world.resource_scope(|world, main_thread_executor: Mut<MainThreadExecutor>| {
world.resource_scope(|world, mut render_channels: Mut<RenderAppChannels>| {
// we use a scope here to run any main thread tasks that the render world still needs to run
// while we wait for the render world to be received.
let mut render_app = ComputeTaskPool::get()
.scope_with_executor(true, Some(&*main_thread_executor.0), |s| {
s.spawn(async { render_channels.recv().await });
})
.pop()
.unwrap();
world.run_schedule(OxrLast);
render_app.extract(world);
render_channels.send_blocking(render_app);
});
});
}
pub const XR_TEXTURE_INDEX: u32 = 3383858418; pub const XR_TEXTURE_INDEX: u32 = 3383858418;
pub fn clean_views( pub fn clean_views(
@@ -168,7 +204,6 @@ pub fn clean_views(
for (e, cam) in &cam_query { for (e, cam) in &cam_query {
manual_texture_views.remove(&ManualTextureViewHandle(XR_TEXTURE_INDEX + cam.0)); manual_texture_views.remove(&ManualTextureViewHandle(XR_TEXTURE_INDEX + cam.0));
commands.entity(e).despawn_recursive(); commands.entity(e).despawn_recursive();
info!("removing cam")
} }
} }

View File

@@ -74,7 +74,6 @@ fn run_session_status_schedules(world: &mut World) {
world.run_schedule(XrSessionExiting); world.run_schedule(XrSessionExiting);
world.run_system_once(apply_deferred); world.run_system_once(apply_deferred);
world.remove_resource::<OxrSession>(); world.remove_resource::<OxrSession>();
info!("Main App destroy");
} }
} }
} }