minor update

This commit is contained in:
Hartmut Seichter 2022-12-06 15:44:16 +01:00
parent 4a8c9a298a
commit 3b18d1e032
7 changed files with 154 additions and 133 deletions

18
LICENSE.md Normal file
View file

@ -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.

View file

@ -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::<offaxis::OffAxisProjection>,
// )
// .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::<OffAxisProjection>::default())
.add_system(update_offaxis)
.run();
}
fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
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);
}
}

View file

@ -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);
@ -74,8 +59,6 @@ pub fn offaxis_camera_setup(mut commands: Commands) {
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::<SimpleOrthoProjection>,
// )
// .run();
// }

View file

@ -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::<f32>();
// n.w += rng.gen::<f32>();
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

View file

@ -2,20 +2,23 @@
use bevy::{prelude::*, math::bool, render::primitives::Frustum};
use rand::prelude::*;
use bevy::render::primitives::Plane;
enum EyePos {
Left,
Right
}
pub struct ScreenInfo {
name : String, // main
width : f32, // 3.08
height : f32, // 2.33
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,
#[derive(Default,Component)]
pub struct ScreenInfo {
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 (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)
}
@ -61,5 +81,4 @@ impl ScreenInfo {
self.intersection(p1, p2)
}
}

33
src/utils.rs Normal file
View file

@ -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<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
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());
}
}

22
src/viewer.rs Normal file
View file

@ -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,
}
}
}