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