update with proper scene setup

This commit is contained in:
Hartmut Seichter 2022-12-17 20:57:35 +01:00
parent 714be06192
commit fda3502870
6 changed files with 111 additions and 88 deletions

View file

@ -55,8 +55,8 @@ fn main() {
.add_startup_system(setup_threaded_tracker) .add_startup_system(setup_threaded_tracker)
.add_plugin(CameraProjectionPlugin::<OffAxisProjection>::default()) .add_plugin(CameraProjectionPlugin::<OffAxisProjection>::default())
// .add_system(simulate_viewer) .add_system(simulate_viewer_with_circle)
.add_system(simulate_viewer_with_keyboard) // .add_system(simulate_viewer_with_keyboard)
.add_system(apply_viewer_to_projections) .add_system(apply_viewer_to_projections)
.add_system(toggle_fullscreeen) .add_system(toggle_fullscreeen)
.run(); .run();

View file

@ -1,6 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::camera::{Camera, CameraProjection}; use bevy::render::camera::{Camera, CameraProjection};
use bevy::render::view::visibility::*;
use bevy::render::primitives::Frustum; use bevy::render::primitives::Frustum;
use bevy::render::view::VisibleEntities; use bevy::render::view::VisibleEntities;
use bevy::math::Mat4; use bevy::math::Mat4;
@ -64,6 +65,7 @@ pub fn offaxis_camera_setup(mut commands: Commands) {
bevy::render::camera::CameraRenderGraph::new(bevy::core_pipeline::core_3d::graph::NAME), bevy::render::camera::CameraRenderGraph::new(bevy::core_pipeline::core_3d::graph::NAME),
projection, projection,
frustum, frustum,
NoFrustumCulling,
transform, transform,
GlobalTransform::default(), GlobalTransform::default(),
VisibleEntities::default(), VisibleEntities::default(),

View file

@ -1,27 +1,48 @@
use bevy::math::{Affine3A, Mat4, Quat};
use bevy::prelude::*; use bevy::prelude::*;
use bevy::math::{Mat4, Quat, Affine3A};
/// creates a conventional projection matrix from frustum planes /// creates a conventional projection matrix from frustum planes
/// ///
/// returns a potentially off-axis projection matrix /// returns a potentially off-axis projection matrix
pub fn make_projection_rh_from_frustum(left: f32, right: f32, bottom: f32, top: f32, near:f32, far:f32) -> Mat4 pub fn make_projection_rh_from_frustum(
{ left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> Mat4 {
// based on OpenSceneGraph / glFrustum implementation // based on OpenSceneGraph / glFrustum implementation
let a = (right + left) / (right - left); let a = (right + left) / (right - left);
let b: f32 = (top + bottom) / (top - bottom); let b: f32 = (top + bottom) / (top - bottom);
let c= if far.abs() > f32::MAX { -1.0 } else { -(far + near) / (far - near)}; let c = if far.abs() > f32::MAX {
let d = if far.abs() > f32::MAX { -2.0 * near } else { -2.0 * far * near / (far - near) }; -1.0
} else {
-(far + near) / (far - near)
};
let d = if far.abs() > f32::MAX {
-2.0 * near
} else {
-2.0 * far * near / (far - near)
};
Mat4::from_cols( Mat4::from_cols(
Vec4::new(2.0 * near / (right - left), 0.0, 0.0, 0.0), Vec4::new(2.0 * near / (right - left), 0.0, 0.0, 0.0),
Vec4::new(0.0, 2.0 * near / (top - bottom), 0.0, 0.0), Vec4::new(0.0, 2.0 * near / (top - bottom), 0.0, 0.0),
Vec4::new(a, b, c, -1.0), Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0)) Vec4::new(0.0, 0.0, d, 0.0),
)
} }
/// creates a projection from a frustum planes with a reversed depth mapped to [0..1] /// creates a projection from a frustum planes with a reversed depth mapped to [0..1]
pub fn make_projection_rh_from_frustum_reversed(left: f32, right: f32, bottom: f32, top: f32, z_near:f32, z_far:f32) -> Mat4 pub fn make_projection_rh_from_frustum_reversed(
{ left: f32,
right: f32,
bottom: f32,
top: f32,
z_near: f32,
z_far: f32,
) -> Mat4 {
assert!(z_near > 0.0 && z_far > 0.0); assert!(z_near > 0.0 && z_far > 0.0);
// //
@ -33,17 +54,15 @@ pub fn make_projection_rh_from_frustum_reversed(left: f32, right: f32, bottom: f
let c = z_near / (z_far - z_near); let c = z_near / (z_far - z_near);
let d = z_far * z_near / (z_far - z_near); let d = z_far * z_near / (z_far - z_near);
Mat4::from_cols( Mat4::from_cols(
Vec4::new(2.0 * z_near / (right - left), 0.0, 0.0, 0.0), Vec4::new(2.0 * z_near / (right - left), 0.0, 0.0, 0.0),
Vec4::new(0.0, 2.0 * z_near / (top - bottom), 0.0, 0.0), Vec4::new(0.0, 2.0 * z_near / (top - bottom), 0.0, 0.0),
Vec4::new(a, b, c, -1.0), Vec4::new(a, b, c, -1.0),
Vec4::new(0.0, 0.0, d, 0.0)) Vec4::new(0.0, 0.0, d, 0.0),
)
} }
pub fn make_projection_rh_custom(fov_y: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Mat4 {
pub fn make_projection_rh_custom(fov_y: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Mat4
{
let tan_fovy = (fov_y * 0.5).tan(); // use half angle beta let tan_fovy = (fov_y * 0.5).tan(); // use half angle beta
let right = tan_fovy * aspect_ratio * z_near; let right = tan_fovy * aspect_ratio * z_near;
let left = -right; let left = -right;
@ -55,16 +74,13 @@ pub fn make_projection_rh_custom(fov_y: f32, aspect_ratio: f32, z_near: f32, z_f
make_projection_rh_from_frustum_reversed(left, right, bottom, top, z_near, z_far) make_projection_rh_from_frustum_reversed(left, right, bottom, top, z_near, z_far)
} }
pub fn create_offaxis_matrices( pub fn create_offaxis_matrices(
screen_lower_left: Vec3, screen_lower_left: Vec3,
screen_lower_right: Vec3, screen_lower_right: Vec3,
screen_upper_left: Vec3, screen_upper_left: Vec3,
pos_eye: Vec3, pos_eye: Vec3,
z_far: f32 z_far: f32,
) -> (Mat4,Mat4) ) -> (Mat4, Mat4) {
{
// //
let vec_right = screen_lower_right - screen_lower_left; // vr let vec_right = screen_lower_right - screen_lower_left; // vr
let vec_up = screen_upper_left - screen_lower_left; // vu let vec_up = screen_upper_left - screen_lower_left; // vu
@ -104,13 +120,29 @@ pub fn create_offaxis_matrices(
// info!("l r b t {} {} {} {}",left,right,bottom,top); // info!("l r b t {} {} {} {}",left,right,bottom,top);
// create a view frustum // create a view frustum
let projection_matrix = make_projection_rh_from_frustum_reversed(left,right,bottom,top,z_near,z_far); let projection_matrix =
make_projection_rh_from_frustum_reversed(left, right, bottom, top, z_near, z_far);
let view_matrix_rotation = Mat4::from_cols( let view_matrix_rotation = Mat4::from_cols(
Vec4::new(vec_right_normalized.x, vec_up_normalized.x, vec_normal.x ,0.0), Vec4::new(
Vec4::new(vec_right_normalized.y, vec_up_normalized.y, vec_normal.y ,0.0), vec_right_normalized.x,
Vec4::new(vec_right_normalized.z, vec_up_normalized.z, vec_normal.z ,0.0), vec_up_normalized.x,
Vec4::W vec_normal.x,
0.0,
),
Vec4::new(
vec_right_normalized.y,
vec_up_normalized.y,
vec_normal.y,
0.0,
),
Vec4::new(
vec_right_normalized.z,
vec_up_normalized.z,
vec_normal.z,
0.0,
),
Vec4::W,
); );
let rotation_quat = Quat::from_mat4(&view_matrix_rotation); // Quat::from_mat4(view_matrix_rotation); let rotation_quat = Quat::from_mat4(&view_matrix_rotation); // Quat::from_mat4(view_matrix_rotation);
@ -118,12 +150,7 @@ pub fn create_offaxis_matrices(
// info!("Rotation Mat {:?}",view_matrix_rotation); // info!("Rotation Mat {:?}",view_matrix_rotation);
// info!("Viewer Rotation {:?}",rotation_quat); // info!("Viewer Rotation {:?}",rotation_quat);
let view_matrix_eye = Mat4::from_cols( let view_matrix_eye = Mat4::from_cols(Vec4::X, Vec4::Y, Vec4::Z, (-pos_eye).extend(1.0));
Vec4::X,
Vec4::Y,
Vec4::Z,
(-pos_eye).extend(1.0)
);
// create resulting view matrix (this should be much simpler using glam API) // create resulting view matrix (this should be much simpler using glam API)
let view_matrix = view_matrix_rotation * view_matrix_eye; let view_matrix = view_matrix_rotation * view_matrix_eye;
@ -132,20 +159,16 @@ pub fn create_offaxis_matrices(
(view_matrix, projection_matrix) (view_matrix, projection_matrix)
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bevy::prelude::*; use bevy::prelude::*;
use crate::{screeninfo, projection::create_offaxis_matrices}; use crate::{projection::create_offaxis_matrices, screeninfo};
use super::make_projection_rh_from_frustum; use super::make_projection_rh_from_frustum;
#[test] #[test]
fn compare_projections() { fn compare_projections() {
// build an on-axis frustum // build an on-axis frustum
let fovy = 33.0_f32; let fovy = 33.0_f32;
let aspect_ratio = 1.6666_f32; let aspect_ratio = 1.6666_f32;
@ -167,36 +190,33 @@ mod tests {
println!("mat 2 {:?}", mat_pers); println!("mat 2 {:?}", mat_pers);
assert!(mat_frust.abs_diff_eq(mat_pers, f32::EPSILON)); assert!(mat_frust.abs_diff_eq(mat_pers, f32::EPSILON));
} }
#[test] #[test]
fn create_view_matrices() { fn create_view_matrices() {
// Assumptions: Screensize 6.0m x 1.8m // Assumptions: Screensize 6.0m x 1.8m
// Viewer is 5m away from center // Viewer is 5m away from center
let screen_lower_left = Vec3::new(-3.0, -0.9, 0.0); let screen_lower_left = Vec3::new(-3.0, -0.9, 0.0);
let screen_lower_right = Vec3::new(3.0, -0.9, 0.0); let screen_lower_right = Vec3::new(3.0, -0.9, 0.0);
let screen_upper_left = Vec3::new(-3.0, 0.9, 0.0); let screen_upper_left = Vec3::new(-3.0, 0.9, 0.0);
let eye_pos = Vec3::Z * 5.0; let eye_pos = Vec3::Z * 5.0;
let z_far = 100.0_f32; let z_far = 100.0_f32;
let (view, projection) = create_offaxis_matrices(
let (view,projection) = create_offaxis_matrices(screen_lower_left, screen_lower_right, screen_upper_left, eye_pos, z_far); screen_lower_left,
screen_lower_right,
screen_upper_left,
eye_pos,
z_far,
);
println!("View {:?}", view); println!("View {:?}", view);
println!("Projection {:?}", projection); println!("Projection {:?}", projection);
// assert!(!view.is_nan()); // assert!(!view.is_nan());
// assert!(!projection.is_nan()); // assert!(!projection.is_nan());
} }
} }

View file

@ -9,13 +9,14 @@ pub fn build_scene(
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 4.0 })), mesh: meshes.add(Mesh::from(shape::Plane { size: 4.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
transform: Transform::from_xyz(0.0, -0.5, 0.0),
..default() ..default()
}); });
// cube // cube
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), mesh: meshes.add(Mesh::from(shape::Cube { size: 1.5 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0), transform: Transform::from_xyz(0.0, 0.5, -3.0),
..default() ..default()
}); });
// light // light

View file

@ -61,7 +61,6 @@ impl Tracker {
let _ = dispatcher.add_typed_handler(Box::new( Tracker {} ), None).unwrap(); let _ = dispatcher.add_typed_handler(Box::new( Tracker {} ), None).unwrap();
// setup a channel // setup a channel
// let (tx, rx) = mpsc::channel(); // let (tx, rx) = mpsc::channel();

View file

@ -56,20 +56,21 @@ pub fn apply_viewer_to_projections(
}); });
} }
pub fn simulate_viewer(mut query: Query<&mut Viewer>, time: Res<Time>) { pub fn simulate_viewer_with_circle(mut query: Query<&mut Viewer>, time: Res<Time>) {
for mut v in query.iter_mut() { for mut v in query.iter_mut() {
//v.position += Vec3::Y * 0.005;
v.alpha += 1.5 * time.delta_seconds();
let radius = 1.0; let radius = 1.3;
let offset = Vec3::new(0.0,0.0,8f32);
let speed = 1.0f32;
v.alpha += speed * time.delta_seconds();
let z_distance = 3.0_f32;
v.position = Vec3::new( v.position = Vec3::new(
v.alpha.sin() * radius, v.alpha.sin() * radius,
v.alpha.cos() * radius, v.alpha.cos() * radius,
z_distance + v.alpha.sin(), 0.0,
); ) + offset;
// info!("Viewer {:?}", v.position); // info!("Viewer {:?}", v.position);
} }