first step done: we have a custom and updatable matrix

This commit is contained in:
Hartmut Seichter 2022-12-05 23:01:16 +01:00
parent 8234a61364
commit 4a8c9a298a
7 changed files with 360 additions and 180 deletions

View file

@ -1,8 +1,10 @@
use crate::scene::*;
use bevy::{prelude::*, window::PresentMode};
use bevy::{prelude::*, window::PresentMode, render::camera::CameraProjectionPlugin};
use offaxis::{offaxis_camera_setup, OffAxisProjection};
mod offaxis;
mod scene;
mod screeninfo;
fn main() {
App::new()
@ -35,7 +37,14 @@ fn main() {
// )
// .add_system(hello_world)
.add_startup_system(build_scene)
.add_system(print_positions)
.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();
}
@ -50,3 +59,14 @@ fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
}
}
}
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,16 +4,21 @@ use bevy::render::camera::{Camera, CameraProjection};
use bevy::render::primitives::Frustum;
use bevy::render::view::VisibleEntities;
#[derive(Component)]
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Default)]
pub struct OffAxisProjection {
near: f32,
far: f32,
pub far: f32,
aspect: f32,
}
impl CameraProjection for OffAxisProjection {
fn get_projection_matrix(&self) -> Mat4 {
println!("Here we go!");
println!("Here we go! {:?}",self);
Mat4::orthographic_rh(-self.aspect, self.aspect, -1.0, 1.0, self.near, self.far)
}
@ -45,23 +50,20 @@ impl Default for OffAxisProjection {
}
}
pub fn camera_setup(mut commands: Commands) {
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();
let camera = Camera {
// near: projection.near,
// far: projection.far,
..default()
};
print!("Setup {0}", line!());
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);
// frustum construction code copied from Bevy
let view_projection = projection.get_projection_matrix() * transform.compute_matrix().inverse();
@ -75,15 +77,17 @@ pub fn camera_setup(mut commands: Commands) {
print!("Setup {0}\n", line!());
commands.spawn((
camera,
bevy::render::camera::CameraRenderGraph::new(bevy::core_pipeline::core_3d::graph::NAME),
projection,
frustum,
VisibleEntities::default(),
transform,
GlobalTransform::default(),
VisibleEntities::default(),
Camera::default(),
Camera3d::default()
));
print!("Setup {0}\t{1}\n", line!(), file!());
println!("Setup {0}\t{1}\n", line!(), file!());
}
// fn main() {

View file

@ -1,4 +1,7 @@
use bevy::{prelude::*, math::bool};
use bevy::{prelude::*, math::bool, render::primitives::Frustum};
use rand::prelude::*;
use bevy::render::primitives::Plane;
pub struct BuildScenePlugin;
@ -33,7 +36,15 @@ pub struct ProjectionScreen {
}
fn compute_offaxis_frustum (
fn generalized_projection_stereo(eyePos : Vec3)
{
}
fn generalized_projection (
projectionScreen: ProjectionScreen,
eyePos: Vec3,
mut camera: Camera3dBundle,
@ -49,7 +60,9 @@ fn compute_offaxis_frustum (
let vr = projectionScreen.dir_right;
let vn = projectionScreen.dir_normal;
let m = projectionScreen.matrix;
// let m = projectionScreen.matrix;
//let m = Mat4::from_cols(vu., y_axis, z_axis, w_axis)
let va = pa - eyePos;
let vb = pb - eyePos;
@ -60,7 +73,9 @@ fn compute_offaxis_frustum (
let d = -va.dot(vn);
if (clampNearPlane) {
if clampNearPlane {
_ = 33;
// camera.frustum.planes[4]. // should be near
}
}
@ -99,12 +114,15 @@ pub fn build_scene(
let mut cam = Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
};
commands.spawn(cam);
commands.spawn((Person, Position { x: 10.0, y: 10.0 }));
// commands.spawn(cam);
// commands.spawn((Person, Position { x: 10.0, y: 10.0 }));
// commands.spawn()
// .insert(Person)
// .insert(Position { x: 10.0, y: 10.0 } );
@ -116,9 +134,52 @@ pub fn build_scene(
// });
}
pub fn modify_frustum(mut query: Query<&Frustum>)
{
let mut rng = rand::thread_rng();
for mut q in query.iter() {
for mut p in q.planes {
println!("{:?}",p);
let mut n = p.normal_d();
n.w += rng.gen::<f32>();
p = Plane::new(n);
println!("{:?}",p);
// p.normal_d() += rng.gen();
}
// println!("{:?}",q.frustum.planes.len());
}
}
pub fn modify_projection(mut query: Query<&Projection>)
{
let mut rng = rand::thread_rng();
for mut q in query.iter() {
print!("{:?}",q);
}
}
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

65
src/screeninfo.rs Normal file
View file

@ -0,0 +1,65 @@
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,
top : Vec3,
bottom : Vec3,
left : Vec3,
right : Vec3
}
impl ScreenInfo {
fn update(&mut self) {
// compute right as up ^ forward
self.horizontal = self.up.cross(self.normal);
// points of screen
self.top = self.center + self.up * (self.height * 0.5);
self.bottom = self.center - self.up * (self.height * 0.5);
self.left = self.center - self.horizontal * (self.height * 0.5);
self.right = self.center + self.horizontal * (self.height * 0.5);
}
fn intersection(
&self,
p1: Vec3,
p2: Vec3) -> (bool,Vec3)
{
let u = self.normal.dot(self.center - p1) / self.normal.dot(p2 - p1);
let intersection_point = p1 + ((p2 - p1) * u);
(u >= 0.0 && u <= 1.0,intersection_point)
}
fn normal_intersection(&self,
p1: Vec3) -> (bool,Vec3)
{
let p2 = p1 - self.normal * 10.0;
self.intersection(p1, p2)
}
}