Add 2d camera
This commit is contained in:
parent
bd21e62cba
commit
2e08a5a784
|
@ -20,7 +20,13 @@ impl Default for CameraComponent {
|
|||
}
|
||||
|
||||
impl CameraComponent {
|
||||
pub fn new() -> Self {
|
||||
pub fn new_3d() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn new_2d() -> Self {
|
||||
let mut s = Self::default();
|
||||
s.mode = CameraProjectionMode::Orthographic;
|
||||
s
|
||||
}
|
||||
}
|
|
@ -40,6 +40,7 @@ impl Projection {
|
|||
pub struct RenderCamera {
|
||||
view_proj: glam::Mat4,
|
||||
|
||||
size: PhysicalSize<u32>,
|
||||
aspect: f32,
|
||||
znear: f32,
|
||||
zfar: f32,
|
||||
|
@ -50,6 +51,7 @@ impl RenderCamera {
|
|||
Self {
|
||||
view_proj: glam::Mat4::IDENTITY,
|
||||
|
||||
size,
|
||||
aspect: size.width as f32 / size.height as f32,
|
||||
znear: 0.1,
|
||||
zfar: 100.0,
|
||||
|
@ -61,27 +63,39 @@ impl RenderCamera {
|
|||
}
|
||||
|
||||
pub fn update_view_projection(&mut self, camera: &CameraComponent) -> &glam::Mat4 {
|
||||
let position = camera.transform.translation;
|
||||
let target = camera.transform.rotation * glam::Vec3::new(0.0, 0.0, -1.0);
|
||||
let target = target.normalize();
|
||||
match camera.mode {
|
||||
CameraProjectionMode::Perspective => {
|
||||
let position = camera.transform.translation;
|
||||
let target = camera.transform.rotation * glam::Vec3::new(0.0, 0.0, -1.0);
|
||||
let target = target.normalize();
|
||||
|
||||
/* debug!("Camera rotation: {:?}", rotation);
|
||||
debug!("Camera position: {:?}", position);
|
||||
debug!("Camera target: {:?}", target); */
|
||||
let view = glam::Mat4::look_to_rh(
|
||||
position,
|
||||
target,
|
||||
glam::Vec3::new(0.0, 1.0, 0.0)
|
||||
);
|
||||
|
||||
let view = glam::Mat4::look_to_rh(
|
||||
position,
|
||||
target,
|
||||
glam::Vec3::new(0.0, 1.0, 0.0)
|
||||
);
|
||||
let proj = glam::Mat4::perspective_rh_gl(camera.fov.to_radians(), self.aspect, self.znear, self.zfar);
|
||||
|
||||
let proj = glam::Mat4::perspective_rh_gl(camera.fov.to_radians(), self.aspect, self.znear, self.zfar);
|
||||
self.view_proj = OPENGL_TO_WGPU_MATRIX * proj * view;
|
||||
&self.view_proj
|
||||
},
|
||||
CameraProjectionMode::Orthographic => {
|
||||
let position = camera.transform.translation;
|
||||
let target = camera.transform.rotation * glam::Vec3::new(0.0, 0.0, -1.0);
|
||||
let target = target.normalize();
|
||||
|
||||
self.view_proj = OPENGL_TO_WGPU_MATRIX * proj * view;
|
||||
&self.view_proj
|
||||
}
|
||||
let ratio_size_per_depth = ((camera.fov.to_radians() / 2.0) * 2.0).atan();
|
||||
let distance = (target - position).length();
|
||||
|
||||
pub fn view_proj(&self) -> &glam::Mat4 {
|
||||
&self.view_proj
|
||||
let size_y = ratio_size_per_depth * distance;
|
||||
let size_x = ratio_size_per_depth * distance * self.aspect;
|
||||
|
||||
let proj = glam::Mat4::orthographic_rh_gl(-size_x, size_x, -size_y, size_y, self.znear, self.zfar);
|
||||
|
||||
self.view_proj = OPENGL_TO_WGPU_MATRIX * proj;
|
||||
&self.view_proj
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue