From 9e8f09fe97896a3fe66475d29998b07d060c018b Mon Sep 17 00:00:00 2001 From: Schmarni Date: Sat, 6 Sep 2025 23:36:12 +0200 Subject: [PATCH] feat: add spectator camera flag Signed-off-by: Schmarni --- src/main.rs | 15 +++++++++++---- src/spectator_cam.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/spectator_cam.rs diff --git a/src/main.rs b/src/main.rs index 6c625bf..e96e082 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,14 @@ mod core; mod nodes; mod objects; mod session; +mod spectator_cam; pub mod tracking_offset; #[cfg(feature = "wayland")] mod wayland; use crate::nodes::drawable::sky::SkyPlugin; use crate::nodes::input; +use crate::spectator_cam::SpectatorCameraPlugin; use bevy::MinimalPlugins; use bevy::a11y::AccessibilityPlugin; @@ -89,6 +91,10 @@ struct CliArgs { #[clap(short, long, action)] flatscreen: bool, + /// Replaces the flatscreen mode with a first person spectator camera + #[clap(short, long, action)] + spectator: bool, + /// Creates a transparent window fot the flatscreen mode #[clap(short, long, action)] transparent_flatscreen: bool, @@ -326,10 +332,11 @@ fn bevy_loop( { let mut plugin = WinitPlugin::::default(); plugin.run_on_any_thread = true; - plugins = plugins - .add(plugin) - .disable::() - .add(FlatscreenInputPlugin); + plugins = plugins.add(plugin).disable::(); + plugins = match args.spectator { + true => plugins.add(SpectatorCameraPlugin), + false => plugins.add(FlatscreenInputPlugin), + }; } app.add_plugins( if !args.flatscreen { diff --git a/src/spectator_cam.rs b/src/spectator_cam.rs new file mode 100644 index 0000000..0739425 --- /dev/null +++ b/src/spectator_cam.rs @@ -0,0 +1,39 @@ +use bevy::{ + app::Plugin, + core_pipeline::core_3d::Camera3d, + ecs::{ + component::Component, + system::{Commands, Res}, + }, + render::camera::{PerspectiveProjection, Projection}, + transform::components::Transform, +}; +use bevy_mod_openxr::session::OxrSession; +use bevy_mod_xr::session::XrSessionCreated; +use openxr::ReferenceSpaceType; + +pub struct SpectatorCameraPlugin; + +impl Plugin for SpectatorCameraPlugin { + fn build(&self, app: &mut bevy::app::App) { + app.add_systems(XrSessionCreated, create); + } +} + +#[derive(Component)] +#[require(Camera3d)] +struct SpectatorCam; + +fn create(session: Res, mut cmds: Commands) { + cmds.spawn(( + SpectatorCam, + session + .create_reference_space(ReferenceSpaceType::VIEW, Transform::IDENTITY) + .unwrap() + .0, + Projection::Perspective(PerspectiveProjection { + fov: 100f32.to_radians(), + ..Default::default() + }), + )); +}