2022-07-17 23:18:33 +02:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2022-11-16 23:07:49 +01:00
|
|
|
use bevy::render::camera::{Camera, CameraProjection};
|
2022-11-17 20:10:49 +01:00
|
|
|
use bevy::render::primitives::Frustum;
|
2022-11-16 23:07:49 +01:00
|
|
|
use bevy::render::view::VisibleEntities;
|
2022-12-06 21:12:15 +01:00
|
|
|
use bevy::math::Mat4;
|
2022-07-17 23:18:33 +02:00
|
|
|
|
2022-12-06 15:44:16 +01:00
|
|
|
use crate::screeninfo::ScreenInfo;
|
2022-12-06 21:12:15 +01:00
|
|
|
use crate::viewer::*;
|
2022-12-05 23:01:16 +01:00
|
|
|
|
|
|
|
#[derive(Component, Debug, Clone, Reflect)]
|
|
|
|
#[reflect(Component, Default)]
|
2022-07-17 23:18:33 +02:00
|
|
|
pub struct OffAxisProjection {
|
|
|
|
near: f32,
|
2022-12-05 23:01:16 +01:00
|
|
|
pub far: f32,
|
2022-07-17 23:18:33 +02:00
|
|
|
aspect: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CameraProjection for OffAxisProjection {
|
|
|
|
fn get_projection_matrix(&self) -> Mat4 {
|
2022-12-06 15:44:16 +01:00
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
println!("Here we go! {:?}",self);
|
2022-12-06 15:44:16 +01:00
|
|
|
|
2022-12-07 14:05:26 +01:00
|
|
|
Mat4::perspective_rh_gl(45.0_f32.to_radians(),
|
|
|
|
self.aspect,
|
|
|
|
self.near,
|
|
|
|
self.far)
|
2022-12-06 21:12:15 +01:00
|
|
|
|
2022-12-07 14:05:26 +01:00
|
|
|
//Mat4::orthographic_rh(-self.aspect, self.aspect, -1.0, 1.0, self.near, self.far)
|
2022-07-17 23:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// what to do on window resize
|
|
|
|
fn update(&mut self, width: f32, height: f32) {
|
|
|
|
self.aspect = width / height;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn far(&self) -> f32 {
|
|
|
|
self.far
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for OffAxisProjection {
|
|
|
|
fn default() -> Self {
|
2022-11-17 20:10:49 +01:00
|
|
|
Self {
|
|
|
|
near: 0.0,
|
|
|
|
far: 1000.0,
|
|
|
|
aspect: 1.0,
|
|
|
|
}
|
2022-07-17 23:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 21:12:15 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
let a = (right + left) / (right - left);
|
|
|
|
let b: f32 = (top + bottom) / (top - bottom);
|
|
|
|
let c= if far.abs() > f32::MAX { -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(
|
|
|
|
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(a, b, c, -1.0),
|
|
|
|
Vec4::new(0.0, 0.0, d, 0.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-05 23:01:16 +01:00
|
|
|
pub fn offaxis_camera_setup(mut commands: Commands) {
|
2022-07-17 23:18:33 +02:00
|
|
|
|
|
|
|
let projection = OffAxisProjection::default();
|
2022-11-16 23:07:49 +01:00
|
|
|
|
2022-07-17 23:18:33 +02:00
|
|
|
// position the camera like bevy would do by default for 2D:
|
|
|
|
let transform = Transform::from_xyz(0.0, 0.0, projection.far - 0.1);
|
2022-12-05 23:01:16 +01:00
|
|
|
|
2022-07-17 23:18:33 +02:00
|
|
|
// frustum construction code copied from Bevy
|
2022-11-17 20:10:49 +01:00
|
|
|
let view_projection = projection.get_projection_matrix() * transform.compute_matrix().inverse();
|
|
|
|
|
|
|
|
let frustum = Frustum::from_view_projection(
|
2022-07-17 23:18:33 +02:00
|
|
|
&view_projection,
|
|
|
|
&transform.translation,
|
|
|
|
&transform.back(),
|
|
|
|
projection.far,
|
2022-11-17 20:10:49 +01:00
|
|
|
);
|
2022-12-06 15:44:16 +01:00
|
|
|
|
2022-11-17 20:10:49 +01:00
|
|
|
commands.spawn((
|
2022-12-05 23:01:16 +01:00
|
|
|
bevy::render::camera::CameraRenderGraph::new(bevy::core_pipeline::core_3d::graph::NAME),
|
2022-11-17 20:10:49 +01:00
|
|
|
projection,
|
|
|
|
frustum,
|
|
|
|
transform,
|
|
|
|
GlobalTransform::default(),
|
2022-12-05 23:01:16 +01:00
|
|
|
VisibleEntities::default(),
|
|
|
|
Camera::default(),
|
2022-12-06 15:44:16 +01:00
|
|
|
Camera3d::default(),
|
2022-12-06 21:12:15 +01:00
|
|
|
ScreenInfo::new("Test"),
|
|
|
|
Viewer::new(transform.translation)
|
2022-11-17 20:10:49 +01:00
|
|
|
));
|
2022-07-17 23:18:33 +02:00
|
|
|
}
|
2022-12-06 21:12:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use bevy::prelude::Mat4;
|
|
|
|
|
|
|
|
use super::make_projection_rh_from_frustum;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compare_projections() {
|
|
|
|
|
|
|
|
// build an on-axis frustum
|
|
|
|
let fovy = 33.0_f32;
|
|
|
|
let aspect_ratio = 1.6666_f32;
|
|
|
|
let z_near = 1.0_f32;
|
|
|
|
let z_far = 500.0_f32;
|
|
|
|
|
|
|
|
let tan_fovy = (fovy * 0.5).to_radians().tan(); // use half angle beta
|
|
|
|
let right = tan_fovy * aspect_ratio * z_near;
|
|
|
|
let left = -right;
|
|
|
|
let top = tan_fovy * z_near;
|
|
|
|
let bottom = -top;
|
|
|
|
|
|
|
|
let mat_frust = make_projection_rh_from_frustum(left, right, bottom, top, z_near, z_far);
|
|
|
|
|
|
|
|
println!("mat 1 {:?}",mat_frust);
|
|
|
|
|
|
|
|
let mat_pers = Mat4::perspective_rh_gl(fovy.to_radians(),aspect_ratio,z_near,z_far);
|
|
|
|
|
|
|
|
println!("mat 2 {:?}",mat_pers);
|
|
|
|
|
|
|
|
assert!(mat_frust.abs_diff_eq(mat_pers, f32::EPSILON));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|