PixSpace/src/offaxis.rs

78 lines
1.9 KiB
Rust
Raw Normal View History

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;
use bevy::math::Mat4;
2022-07-17 23:18:33 +02:00
2022-12-06 15:44:16 +01:00
use crate::screeninfo::ScreenInfo;
use crate::viewer::*;
use crate::projection::*;
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Default)]
2022-07-17 23:18:33 +02:00
pub struct OffAxisProjection {
pub far: f32,
pub projection_matrix: Mat4
2022-07-17 23:18:33 +02:00
}
impl CameraProjection for OffAxisProjection {
fn get_projection_matrix(&self) -> Mat4 {
self.projection_matrix
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;
2022-07-17 23:18:33 +02:00
}
fn far(&self) -> f32 {
self.far
}
}
impl Default for OffAxisProjection {
fn default() -> Self {
2022-11-17 20:10:49 +01:00
Self {
far: 1000.0,
projection_matrix: make_projection_rh_custom(45.0f32.to_radians(),1.3f32, 1.0, 1000.0)
2022-11-17 20:10:49 +01:00
}
2022-07-17 23:18:33 +02: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
// let projection = PerspectiveProjection::default();
2022-07-17 23:18:33 +02:00
// position the camera like bevy would do by default for 2D:
2022-12-14 22:55:26 +01:00
let transform = Transform::from_xyz(0.0, 0.0, 0.1);
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((
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(),
VisibleEntities::default(),
Camera::default(),
2022-12-06 15:44:16 +01:00
Camera3d::default(),
ScreenInfo::new("Test"),
Viewer::new(transform.translation)
2022-11-17 20:10:49 +01:00
));
2022-07-17 23:18:33 +02:00
}