From 3b18d1e032b008ea45da7106df08e0b944dd3e7f Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 6 Dec 2022 15:44:16 +0100 Subject: [PATCH] minor update --- LICENSE.md | 18 +++++++++++ src/main.rs | 54 ++++++++------------------------ src/offaxis.rs | 46 ++++----------------------- src/scene.rs | 79 +++++++++++++++++++++-------------------------- src/screeninfo.rs | 35 ++++++++++++++++----- src/utils.rs | 33 ++++++++++++++++++++ src/viewer.rs | 22 +++++++++++++ 7 files changed, 154 insertions(+), 133 deletions(-) create mode 100644 LICENSE.md create mode 100644 src/utils.rs create mode 100644 src/viewer.rs diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..a59733e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,18 @@ +Copyright 2022 Hartmut Seichter + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3f64228..47e9b81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,24 @@ +/** + * PixSpace is a Virtual Reality Toolkit to run projective VR setups + * + * © Copyright 2022 Hartmut Seichter + * + */ + use crate::scene::*; +use crate::utils::*; + use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin}; use offaxis::{offaxis_camera_setup, OffAxisProjection}; mod offaxis; mod scene; mod screeninfo; +mod utils; fn main() { App::new() .insert_resource(ClearColor(Color::rgb(0.0, 0.1, 0.9))) - // .insert_resource(WindowDescriptor{ - // title: "PixSpace".to_string(), - // width: 1280.0, - // height: 800.0, - // present_mode: PresentMode::AutoVsync, - // ..Default::default() - // }) .add_plugins(DefaultPlugins.set(WindowPlugin { window: WindowDescriptor { title: "PixSpace".to_string(), @@ -26,47 +29,16 @@ fn main() { }, ..default() })) - // .add_plugins(DefaultPlugins) - .add_plugin(scene::BuildScenePlugin) - // .add_startup_system(offaxis::camera_setup) + // .add_plugin(scene::BuildScenePlugin) .add_system(bevy::window::close_on_esc) .add_system(cycle_msaa) - // .add_system_to_stage( - // CoreStage::PostUpdate, - // camera_system::, - // ) - // .add_system(hello_world) .add_startup_system(build_scene) - .add_system(modify_projection) + // .add_system(modify_projection) + .add_startup_system(offaxis_camera_setup) - // .add_system(print_positions) - // .add_system(modify_frustum) - .add_plugin(CameraProjectionPlugin::::default()) - .add_system(update_offaxis) .run(); } -fn cycle_msaa(input: Res>, mut msaa: ResMut) { - if input.just_pressed(KeyCode::M) { - if msaa.samples == 4 { - info!("Not using MSAA"); - msaa.samples = 1; - } else { - info!("Using 4x MSAA"); - msaa.samples = 4; - } - } -} -fn update_offaxis(mut query : Query<&mut OffAxisProjection>) -{ - for mut q in query.iter_mut() { - - // we fake access to far for updating the matrix - (*q).far *= 1.0; - - println!("Update {:?}",q); - } -} diff --git a/src/offaxis.rs b/src/offaxis.rs index f926641..0ed0b3d 100644 --- a/src/offaxis.rs +++ b/src/offaxis.rs @@ -4,9 +4,7 @@ use bevy::render::camera::{Camera, CameraProjection}; use bevy::render::primitives::Frustum; use bevy::render::view::VisibleEntities; - - - +use crate::screeninfo::ScreenInfo; #[derive(Component, Debug, Clone, Reflect)] #[reflect(Component, Default)] @@ -18,7 +16,9 @@ pub struct OffAxisProjection { impl CameraProjection for OffAxisProjection { fn get_projection_matrix(&self) -> Mat4 { + println!("Here we go! {:?}",self); + Mat4::orthographic_rh(-self.aspect, self.aspect, -1.0, 1.0, self.near, self.far) } @@ -27,14 +27,6 @@ impl CameraProjection for OffAxisProjection { self.aspect = width / height; } - // fn depth_calculation(&self) -> DepthCalculation { - // // for 2D (camera doesn't rotate) - // DepthCalculation::ZDifference - - // // otherwise - // //DepthCalculation::Distance - // } - fn far(&self) -> f32 { self.far } @@ -51,16 +43,9 @@ impl Default for OffAxisProjection { } pub fn offaxis_camera_setup(mut commands: Commands) { - print!("Setup {0}", line!()); - - // We need all the components that Bevy's built-in camera bundles would add let projection = OffAxisProjection::default(); - - - println!("Setup {0}", line!()); - // position the camera like bevy would do by default for 2D: let transform = Transform::from_xyz(0.0, 0.0, projection.far - 0.1); @@ -73,9 +58,7 @@ pub fn offaxis_camera_setup(mut commands: Commands) { &transform.back(), projection.far, ); - - print!("Setup {0}\n", line!()); - + commands.spawn(( bevy::render::camera::CameraRenderGraph::new(bevy::core_pipeline::core_3d::graph::NAME), projection, @@ -84,24 +67,7 @@ pub fn offaxis_camera_setup(mut commands: Commands) { GlobalTransform::default(), VisibleEntities::default(), Camera::default(), - Camera3d::default() + Camera3d::default(), + ScreenInfo::default() )); - - println!("Setup {0}\t{1}\n", line!(), file!()); } - -// fn main() { -// // need to add a bevy-internal camera system to update -// // the projection on window resizing - -// use bevy::render::camera::camera_system; - -// App::new() -// .add_plugins(DefaultPlugins) -// .add_startup_system(setup) -// .add_system_to_stage( -// CoreStage::PostUpdate, -// camera_system::, -// ) -// .run(); -// } diff --git a/src/scene.rs b/src/scene.rs index 98ebf65..d9444c1 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -3,23 +3,14 @@ use rand::prelude::*; use bevy::render::primitives::Plane; -pub struct BuildScenePlugin; +// pub struct BuildScenePlugin; -impl Plugin for BuildScenePlugin { - fn build(&self, app: &mut App) { - app.add_startup_system(build_scene) // actual scene - .add_system(print_positions); // debugging - } -} - -#[derive(Component)] -struct Person; - -#[derive(Component)] -pub struct Position { - x: f32, - y: f32, -} +// impl Plugin for BuildScenePlugin { +// fn build(&self, app: &mut App) { +// app.add_startup_system(build_scene) // actual scene +// .add_system(print_positions); // debugging +// } +// } #[derive(Component)] pub struct ProjectionScreen { @@ -136,50 +127,50 @@ pub fn build_scene( -pub fn modify_frustum(mut query: Query<&Frustum>) -{ - let mut rng = rand::thread_rng(); +// pub fn modify_frustum(mut query: Query<&Frustum>) +// { +// let mut rng = rand::thread_rng(); - for mut q in query.iter() { +// for mut q in query.iter() { - for mut p in q.planes { +// for mut p in q.planes { - println!("{:?}",p); +// println!("{:?}",p); - let mut n = p.normal_d(); +// let mut n = p.normal_d(); - n.w += rng.gen::(); +// n.w += rng.gen::(); - p = Plane::new(n); +// p = Plane::new(n); - println!("{:?}",p); +// println!("{:?}",p); - // p.normal_d() += rng.gen(); - } +// // p.normal_d() += rng.gen(); +// } - // println!("{:?}",q.frustum.planes.len()); - } -} +// // println!("{:?}",q.frustum.planes.len()); +// } +// } -pub fn modify_projection(mut query: Query<&Projection>) -{ - let mut rng = rand::thread_rng(); +// pub fn modify_projection(mut query: Query<&Projection>) +// { +// let mut rng = rand::thread_rng(); - for mut q in query.iter() { +// for mut q in query.iter() { - print!("{:?}",q); - } -} +// print!("{:?}",q); +// } +// } -pub fn print_positions(query: Query<&Position>) { - for _position in query.iter() { - _ = 33; - // println!("position {:?} {:?}", position.x, position.y) - } -} +// pub fn print_positions(query: Query<&Position>) { +// for _position in query.iter() { +// _ = 33; +// // println!("position {:?} {:?}", position.x, position.y) +// } +// } // Problem with off-axis // projection 2401, 240 \ No newline at end of file diff --git a/src/screeninfo.rs b/src/screeninfo.rs index be7857a..956f750 100644 --- a/src/screeninfo.rs +++ b/src/screeninfo.rs @@ -2,20 +2,23 @@ use bevy::{prelude::*, math::bool, render::primitives::Frustum}; use rand::prelude::*; use bevy::render::primitives::Plane; + enum EyePos { Left, Right } + +#[derive(Default,Component)] pub struct ScreenInfo { - name : String, // main - width : f32, // 3.08 - height : f32, // 2.33 + pub name : String, // main (to identify the screen) + width : f32, // 3.08 (full width in m) + height : f32, // 2.33 (full height in m) - center : Vec3, // 0.0 -1.15 -3.08 - normal : Vec3, // 0.0 0.0 1.0 - up: Vec3, // 0.0 1.0 0.0 (vertical) - horizontal: Vec3, + center : Vec3, // 0.0 -1.15 -3.08 (mid point of screen in global coordinated - tracking!) + normal : Vec3, // 0.0 0.0 1.0 (orientation of front side) + up: Vec3, // 0.0 1.0 0.0 (vertical axis) + horizontal: Vec3, // right vector computed as orthonormal top : Vec3, bottom : Vec3, @@ -25,6 +28,22 @@ pub struct ScreenInfo { impl ScreenInfo { + fn default() -> Self { + Self { + name: String::from("main"), + width: 3.08, + height: 2.33, + center: Vec3 { x: 0.0, y: -1.15, z: -3.08 }, + normal: Vec3::Z, + up: Vec3::Y, + horizontal: Vec3::ZERO, + top: Vec3::ZERO, + bottom: Vec3::ZERO, + left: Vec3::ZERO, + right: Vec3::ZERO, + } + } + fn update(&mut self) { @@ -49,6 +68,7 @@ impl ScreenInfo { let intersection_point = p1 + ((p2 - p1) * u); + // returns a tuple if the intersection point is in front and the actual point (u >= 0.0 && u <= 1.0,intersection_point) } @@ -60,6 +80,5 @@ impl ScreenInfo { self.intersection(p1, p2) } - } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..548f31f --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,33 @@ +use crate::scene::*; +use crate::offaxis::*; +use crate::screeninfo; +use crate::screeninfo::ScreenInfo; + +use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin}; + +pub fn cycle_msaa(input: Res>, mut msaa: ResMut) { + if input.just_pressed(KeyCode::M) { + if msaa.samples == 4 { + info!("Not using MSAA"); + msaa.samples = 1; + } else { + info!("Using 4x MSAA"); + msaa.samples = 4; + } + } +} + +pub fn update_offaxis(mut query : Query<( + &mut OffAxisProjection, + &ScreenInfo +)> +) { + for (mut q,si) in query.iter_mut() { + + // we fake access to far for updating the matrix + (*q).far *= 1.0; + + println!("Update {:?}",q); + println!("Screeninfo {0}",si.name.len()); + } +} \ No newline at end of file diff --git a/src/viewer.rs b/src/viewer.rs new file mode 100644 index 0000000..635d167 --- /dev/null +++ b/src/viewer.rs @@ -0,0 +1,22 @@ +use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin}; +use offaxis::{offaxis_camera_setup, OffAxisProjection}; + + +#[derive(Component)] +struct Viewer; + +#[derive(Component)] +pub struct Viewer { + position: Vec3, + orientation: Quat, +} + + +impl Viewer { + + fn default() -> Self { + Self { + position: Vec3::ZERO, + } + } +} \ No newline at end of file