add documentation and satisfy clippy
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
//! A simple 3D scene with light shining over a cube sitting on a plane.
|
//! A simple 3D scene with light shining over a cube sitting on a plane.
|
||||||
|
|
||||||
use bevy::{
|
use bevy::prelude::*;
|
||||||
prelude::*,
|
use bevy_openxr::add_xr_plugins;
|
||||||
render::camera::{ManualTextureViewHandle, RenderTarget},
|
|
||||||
};
|
|
||||||
use bevy_openxr::{add_xr_plugins, render::XR_TEXTURE_INDEX};
|
|
||||||
use bevy_xr::camera::XrCameraBundle;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use bevy::app::{App, First, Plugin, PreUpdate};
|
use bevy::app::{App, First, Plugin};
|
||||||
use bevy::ecs::event::EventWriter;
|
use bevy::ecs::event::EventWriter;
|
||||||
use bevy::ecs::schedule::common_conditions::{not, on_event};
|
use bevy::ecs::schedule::common_conditions::{not, on_event};
|
||||||
use bevy::ecs::schedule::IntoSystemConfigs;
|
use bevy::ecs::schedule::IntoSystemConfigs;
|
||||||
@@ -6,7 +6,6 @@ use bevy::ecs::system::{Commands, Res, ResMut, Resource};
|
|||||||
use bevy::ecs::world::World;
|
use bevy::ecs::world::World;
|
||||||
use bevy::log::{error, info};
|
use bevy::log::{error, info};
|
||||||
use bevy::math::{uvec2, UVec2};
|
use bevy::math::{uvec2, UVec2};
|
||||||
use bevy::render::camera::ManualTextureViews;
|
|
||||||
use bevy::render::extract_resource::ExtractResourcePlugin;
|
use bevy::render::extract_resource::ExtractResourcePlugin;
|
||||||
use bevy::render::renderer::{
|
use bevy::render::renderer::{
|
||||||
RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue,
|
RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue,
|
||||||
@@ -48,14 +47,14 @@ pub fn session_created(status: Option<Res<XrStatus>>) -> bool {
|
|||||||
status.is_some_and(|status| status.session_created)
|
status.is_some_and(|status| status.session_created)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn session_running(status: Option<Res<XrStatus>>) -> bool {
|
|
||||||
status.is_some_and(|status| status.session_running)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn session_ready(status: Option<Res<XrStatus>>) -> bool {
|
pub fn session_ready(status: Option<Res<XrStatus>>) -> bool {
|
||||||
status.is_some_and(|status| status.session_ready)
|
status.is_some_and(|status| status.session_ready)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn session_running(status: Option<Res<XrStatus>>) -> bool {
|
||||||
|
status.is_some_and(|status| status.session_running)
|
||||||
|
}
|
||||||
|
|
||||||
impl Plugin for XrInitPlugin {
|
impl Plugin for XrInitPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
if let Err(e) = init_xr(&self, app) {
|
if let Err(e) = init_xr(&self, app) {
|
||||||
@@ -74,6 +73,7 @@ fn xr_entry() -> Result<XrEntry> {
|
|||||||
Ok(XrEntry(entry))
|
Ok(XrEntry(entry))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is called from [`XrInitPlugin::build()`]. Its a separate function so that we can return a [Result] and control flow is cleaner.
|
||||||
fn init_xr(config: &XrInitPlugin, app: &mut App) -> Result<()> {
|
fn init_xr(config: &XrInitPlugin, app: &mut App) -> Result<()> {
|
||||||
let entry = xr_entry()?;
|
let entry = xr_entry()?;
|
||||||
|
|
||||||
@@ -165,7 +165,8 @@ fn init_xr(config: &XrInitPlugin, app: &mut App) -> Result<()> {
|
|||||||
begin_xr_session
|
begin_xr_session
|
||||||
.run_if(session_ready)
|
.run_if(session_ready)
|
||||||
.run_if(on_event::<BeginXrSession>()),
|
.run_if(on_event::<BeginXrSession>()),
|
||||||
),
|
)
|
||||||
|
.chain(),
|
||||||
)
|
)
|
||||||
.sub_app_mut(RenderApp)
|
.sub_app_mut(RenderApp)
|
||||||
.insert_resource(instance)
|
.insert_resource(instance)
|
||||||
@@ -208,12 +209,13 @@ pub fn create_xr_session(world: &mut World) {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = init_xr_session(world, instance, *system_id, create_info) {
|
if let Err(e) = create_xr_session_inner(world, instance, *system_id, create_info) {
|
||||||
error!("Failed to initialize XrSession: {e}");
|
error!("Failed to initialize XrSession: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_xr_session(
|
/// This is called from [create_xr_session]. It is a separate function to allow us to return a [Result] and make control flow cleaner.
|
||||||
|
fn create_xr_session_inner(
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
instance: XrInstance,
|
instance: XrInstance,
|
||||||
system_id: openxr::SystemId,
|
system_id: openxr::SystemId,
|
||||||
@@ -378,6 +380,7 @@ struct XrRenderResources {
|
|||||||
stage: XrStage,
|
stage: XrStage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This system transfers important render resources from the main world to the render world when a session is created.
|
||||||
pub fn transfer_xr_resources(mut commands: Commands, mut world: ResMut<MainWorld>) {
|
pub fn transfer_xr_resources(mut commands: Commands, mut world: ResMut<MainWorld>) {
|
||||||
let Some(XrRenderResources {
|
let Some(XrRenderResources {
|
||||||
session,
|
session,
|
||||||
@@ -399,6 +402,7 @@ pub fn transfer_xr_resources(mut commands: Commands, mut world: ResMut<MainWorld
|
|||||||
commands.insert_resource(stage);
|
commands.insert_resource(stage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Poll any OpenXR events and handle them accordingly
|
||||||
pub fn poll_events(
|
pub fn poll_events(
|
||||||
instance: Res<XrInstance>,
|
instance: Res<XrInstance>,
|
||||||
session: Option<Res<XrSession>>,
|
session: Option<Res<XrSession>>,
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ use bevy::{
|
|||||||
use bevy_xr::camera::{XrCameraBundle, XrProjection, XrView};
|
use bevy_xr::camera::{XrCameraBundle, XrProjection, XrView};
|
||||||
use openxr::CompositionLayerFlags;
|
use openxr::CompositionLayerFlags;
|
||||||
|
|
||||||
use crate::layer_builder::*;
|
|
||||||
use crate::resources::*;
|
use crate::resources::*;
|
||||||
|
use crate::{init::begin_xr_session, layer_builder::*};
|
||||||
|
|
||||||
use crate::init::session_running;
|
use crate::init::session_running;
|
||||||
|
|
||||||
@@ -19,11 +19,12 @@ pub struct XrRenderPlugin;
|
|||||||
impl Plugin for XrRenderPlugin {
|
impl Plugin for XrRenderPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
PreUpdate,
|
First,
|
||||||
(
|
(
|
||||||
init_views.run_if(resource_added::<XrGraphicsInfo>),
|
init_views.run_if(resource_added::<XrGraphicsInfo>),
|
||||||
wait_frame.run_if(session_running),
|
wait_frame.run_if(session_running),
|
||||||
),
|
)
|
||||||
|
.after(begin_xr_session),
|
||||||
);
|
);
|
||||||
// .add_systems(Startup, init_views);
|
// .add_systems(Startup, init_views);
|
||||||
app.sub_app_mut(RenderApp).add_systems(
|
app.sub_app_mut(RenderApp).add_systems(
|
||||||
@@ -42,6 +43,7 @@ impl Plugin for XrRenderPlugin {
|
|||||||
|
|
||||||
pub const XR_TEXTURE_INDEX: u32 = 3383858418;
|
pub const XR_TEXTURE_INDEX: u32 = 3383858418;
|
||||||
|
|
||||||
|
// TODO: have cameras initialized externally and then recieved by this function.
|
||||||
/// This is needed to properly initialize the texture views so that bevy will set them to the correct resolution despite them being updated in the render world.
|
/// This is needed to properly initialize the texture views so that bevy will set them to the correct resolution despite them being updated in the render world.
|
||||||
pub fn init_views(
|
pub fn init_views(
|
||||||
graphics_info: Res<XrGraphicsInfo>,
|
graphics_info: Res<XrGraphicsInfo>,
|
||||||
@@ -73,6 +75,8 @@ pub fn init_views(
|
|||||||
|
|
||||||
pub fn wait_frame(mut frame_waiter: ResMut<XrFrameWaiter>, mut commands: Commands) {
|
pub fn wait_frame(mut frame_waiter: ResMut<XrFrameWaiter>, mut commands: Commands) {
|
||||||
let state = frame_waiter.wait().expect("Failed to wait frame");
|
let state = frame_waiter.wait().expect("Failed to wait frame");
|
||||||
|
// Here we insert the predicted display time for when this frame will be displayed.
|
||||||
|
// TODO: don't add predicted_display_period if pipelined rendering plugin not enabled
|
||||||
commands.insert_resource(XrTime(openxr::Time::from_nanos(
|
commands.insert_resource(XrTime(openxr::Time::from_nanos(
|
||||||
state.predicted_display_time.as_nanos() + state.predicted_display_period.as_nanos(),
|
state.predicted_display_time.as_nanos() + state.predicted_display_period.as_nanos(),
|
||||||
)));
|
)));
|
||||||
@@ -227,31 +231,6 @@ pub fn insert_texture_views(
|
|||||||
for i in 0..2 {
|
for i in 0..2 {
|
||||||
add_texture_view(&mut manual_texture_views, image, &graphics_info, i);
|
add_texture_view(&mut manual_texture_views, image, &graphics_info, i);
|
||||||
}
|
}
|
||||||
// let left = image.create_view(&wgpu::TextureViewDescriptor {
|
|
||||||
// dimension: Some(wgpu::TextureViewDimension::D2),
|
|
||||||
// array_layer_count: Some(1),
|
|
||||||
// ..Default::default()
|
|
||||||
// });
|
|
||||||
// let right = image.create_view(&wgpu::TextureViewDescriptor {
|
|
||||||
// dimension: Some(wgpu::TextureViewDimension::D2),
|
|
||||||
// array_layer_count: Some(1),
|
|
||||||
// base_array_layer: 1,
|
|
||||||
// ..Default::default()
|
|
||||||
// });
|
|
||||||
// let resolution = graphics_info.resolution;
|
|
||||||
// let format = graphics_info.format;
|
|
||||||
// let left = ManualTextureView {
|
|
||||||
// texture_view: left.into(),
|
|
||||||
// size: resolution,
|
|
||||||
// format: format,
|
|
||||||
// };
|
|
||||||
// let right = ManualTextureView {
|
|
||||||
// texture_view: right.into(),
|
|
||||||
// size: resolution,
|
|
||||||
// format: format,
|
|
||||||
// };
|
|
||||||
// manual_texture_views.insert(LEFT_XR_TEXTURE_HANDLE, left);
|
|
||||||
// manual_texture_views.insert(RIGHT_XR_TEXTURE_HANDLE, right);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_texture_view(
|
pub fn add_texture_view(
|
||||||
|
|||||||
@@ -49,15 +49,15 @@ pub fn handle_xr_events(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Event sent to backends to create an XR session
|
/// Event sent to backends to create an XR session. Should only be called after [`XrInstanceCreated`] is recieved.
|
||||||
#[derive(Event, Clone, Copy, Default)]
|
#[derive(Event, Clone, Copy, Default)]
|
||||||
pub struct CreateXrSession;
|
pub struct CreateXrSession;
|
||||||
|
|
||||||
/// Event sent to backends to begin an XR session
|
/// Event sent to backends to begin an XR session. Should only be called after [`XrSessionState::Ready`] is recieved.
|
||||||
#[derive(Event, Clone, Copy, Default)]
|
#[derive(Event, Clone, Copy, Default)]
|
||||||
pub struct BeginXrSession;
|
pub struct BeginXrSession;
|
||||||
|
|
||||||
/// Event sent to backends to end an XR session.
|
/// Event sent to backends to end an XR session. Should only be called after [`XrSessionState::Running`] is recieved.
|
||||||
#[derive(Event, Clone, Copy, Default)]
|
#[derive(Event, Clone, Copy, Default)]
|
||||||
pub struct EndXrSession;
|
pub struct EndXrSession;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user