use bevy::prelude::*; use bevy::math::Mat4; /// creates a conventional projection matrix from frustum planes /// /// 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 { // 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)) } /// 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 { assert!(z_near > 0.0 && z_far > 0.0); // // reversed z 0..1 projection based on https://thxforthefish.com/posts/reverse_z/ // let a = (right + left) / (right - left); let b: f32 = (top + bottom) / (top - bottom); let c = z_near / (z_far - z_near); let d = z_far * z_near / (z_far - z_near); Mat4::from_cols( 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(a, b, c, -1.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 { let tan_fovy = (fov_y * 0.5).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; //make_projection_rh_from_frustum(left, right, bottom, top, z_near, z_far) make_projection_rh_from_frustum_reversed(left, right, bottom, top, z_near, z_far) } fn create_offaxis_matrices( screen_lower_left: Vec3, screen_lower_right: Vec3, screen_upper_left: Vec3, pos_eye: Vec3, z_far: f32 ) -> (Mat4,Mat4) { // let vec_right = screen_lower_right - screen_lower_left; // vr let vec_up = screen_upper_left - screen_lower_left; // vu let frustum_left = screen_lower_left - pos_eye; // va let frustum_right = screen_lower_right - pos_eye; // vb let frustum_up = screen_upper_left - pos_eye; // vc let vec_right_normalized = vec_right.normalize(); let vec_up_normalized = vec_up.normalize(); let vec_normal = vec_right_normalized.cross(vec_up_normalized).normalize(); let dist = -vec_right.dot(vec_normal); let z_near = 0.001_f32.max(dist - 0.01); let left = vec_right.dot(frustum_left) * z_near / dist; let right = vec_right.dot(frustum_right) * z_near / dist; let bottom = vec_up.dot(frustum_left) * z_near / dist; let top = vec_up.dot(frustum_up) * z_near / dist; let projection_matrix = make_projection_rh_from_frustum_reversed(left,right,bottom,top,z_near,z_far); let view_matrix_rotation = Mat4::from_cols( Vec4::new(vec_right.x, vec_up.x, vec_normal.x ,0.0), Vec4::new(vec_right.y, vec_up.y, vec_normal.y ,0.0), Vec4::new(vec_right.z, vec_up.z, vec_normal.z ,0.0), Vec4::W ); let view_matrix_eye = Mat4::from_cols( Vec4::X, Vec4::Y, Vec4::Z, (-pos_eye).extend(1.0) ); let view_matrix = view_matrix_rotation * view_matrix_eye; (view_matrix,projection_matrix) } #[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 = 1000.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)); } }