refactor cameras. add defaults

This commit is contained in:
Carter Anderson 2020-03-21 21:55:33 -07:00
parent fb496a6172
commit d2e160d44a
14 changed files with 129 additions and 166 deletions

View file

@ -107,18 +107,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
.build(); .build();
} }

View file

@ -53,12 +53,7 @@ fn create_entities_insert_vec(
world.insert( world.insert(
(), (),
vec![( vec![(
Camera::new(CameraType::Projection { Camera::default(),
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
ActiveCamera, ActiveCamera,
LocalToWorld(Mat4::look_at_rh( LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
@ -103,12 +98,7 @@ fn create_entities_builder_add_component(
.add(Rotation::from_euler_angles(0.0, 0.0, 0.0)) .add(Rotation::from_euler_angles(0.0, 0.0, 0.0))
// camera // camera
.build_entity() .build_entity()
.add(Camera::new(CameraType::Projection { .add(Camera::default())
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}))
.add(ActiveCamera) .add(ActiveCamera)
.add(LocalToWorld(Mat4::look_at_rh( .add(LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
@ -137,6 +127,7 @@ fn create_entities_builder_archetype(
.add_entity(MeshEntity { .add_entity(MeshEntity {
mesh: cube_handle, mesh: cube_handle,
material: cube_material_handle, material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 1.0),
..Default::default() ..Default::default()
}) })
// light // light
@ -146,18 +137,12 @@ fn create_entities_builder_archetype(
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
.build(); .build();
} }

View file

@ -63,18 +63,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}); });
let mut rng = StdRng::from_entropy(); let mut rng = StdRng::from_entropy();

View file

@ -50,12 +50,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
vec![ vec![
// camera // camera
( (
Camera::new(CameraType::Projection { Camera::default(),
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
ActiveCamera, ActiveCamera,
LocalToWorld(Mat4::look_at_rh( LocalToWorld(Mat4::look_at_rh(
Vec3::new(6.0, -40.0, 20.0), Vec3::new(6.0, -40.0, 20.0),

View file

@ -56,23 +56,16 @@ fn setup(world: &mut World, resources: &mut Resources) {
// light // light
.add_entity(LightEntity { .add_entity(LightEntity {
translation: Translation::new(4.0, -4.0, 5.0), translation: Translation::new(4.0, -4.0, 5.0),
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
..Default::default() ..Default::default()
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(5.0, 10.0, 10.0), Vec3::new(5.0, 10.0, 10.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
.build(); .build();
} }

View file

@ -49,18 +49,12 @@ pub fn setup(world: &mut World, resources: &mut Resources) {
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
.build(); .build();
} }

View file

@ -53,18 +53,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
.build(); .build();
} }

View file

@ -63,18 +63,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}); });
let mut rng = StdRng::from_entropy(); let mut rng = StdRng::from_entropy();

View file

@ -10,7 +10,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
let mut texture_storage = resources.get_mut::<AssetStorage<Texture>>().unwrap(); let mut texture_storage = resources.get_mut::<AssetStorage<Texture>>().unwrap();
let texture = Texture::load(TextureType::Png( let texture = Texture::load(TextureType::Png(
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/temp_bevy_logo.png").to_string(), concat!(env!("CARGO_MANIFEST_DIR"), "/assets/bevy_logo_dark.png").to_string(),
)); ));
let texture_handle = texture_storage.add(texture); let texture_handle = texture_storage.add(texture);
@ -52,18 +52,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
}) })
// camera // camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
.build(); .build();
} }

View file

@ -31,30 +31,16 @@ fn setup(world: &mut World, resources: &mut Resources) {
}) })
// 3d camera // 3d camera
.add_entity(CameraEntity { .add_entity(CameraEntity {
camera: Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh( local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, 8.0, 5.0), Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0),
)), )),
..Default::default()
}) })
// 2d camera // 2d camera
.add_entity(Camera2dEntity { .add_entity(Camera2dEntity {
camera: Camera::new(CameraType::Orthographic { ..Default::default()
left: 0.0,
right: 0.0,
bottom: 0.0,
top: 0.0,
near: 0.0,
far: 1.0,
}),
active_camera_2d: ActiveCamera2d,
}) })
// bottom left anchor with vertical fill // bottom left anchor with vertical fill
.add_entity(UiEntity { .add_entity(UiEntity {

View file

@ -25,15 +25,8 @@ fn build_move_system() -> Box<dyn Schedulable> {
fn setup(world: &mut World, _resources: &mut Resources) { fn setup(world: &mut World, _resources: &mut Resources) {
let mut builder = world.build().add_entity(Camera2dEntity { let mut builder = world.build().add_entity(Camera2dEntity {
camera: Camera::new(CameraType::Orthographic { camera: Camera::new(CameraType::default_orthographic()),
left: 0.0, ..Default::default()
right: 0.0,
bottom: 0.0,
top: 0.0,
near: 0.0,
far: 1.0,
}),
active_camera_2d: ActiveCamera2d,
}); });
let mut prev = Vec2::default(); let mut prev = Vec2::default();

View file

@ -35,7 +35,7 @@ pub struct LightEntity {
pub rotation: Rotation, pub rotation: Rotation,
} }
#[derive(EntityArchetype)] #[derive(EntityArchetype, Default)]
pub struct CameraEntity { pub struct CameraEntity {
pub camera: Camera, pub camera: Camera,
pub active_camera: ActiveCamera, pub active_camera: ActiveCamera,
@ -48,6 +48,15 @@ pub struct Camera2dEntity {
pub active_camera_2d: ActiveCamera2d, pub active_camera_2d: ActiveCamera2d,
} }
impl Default for Camera2dEntity {
fn default() -> Self {
Camera2dEntity {
camera: Camera::new(CameraType::default_orthographic()),
active_camera_2d: ActiveCamera2d,
}
}
}
#[derive(EntityArchetype)] #[derive(EntityArchetype)]
pub struct UiEntity { pub struct UiEntity {
pub node: Node, pub node: Node,

View file

@ -1,25 +1,94 @@
use crate::math::Mat4; use crate::math::Mat4;
#[derive(Default)]
pub struct ActiveCamera; pub struct ActiveCamera;
#[derive(Default)]
pub struct ActiveCamera2d; pub struct ActiveCamera2d;
pub enum CameraType { pub struct OrthographicCamera {
Projection { pub left: f32,
fov: f32, pub right: f32,
aspect_ratio: f32, pub bottom: f32,
near: f32, pub top: f32,
far: f32, pub near: f32,
}, pub far: f32,
Orthographic {
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
},
} }
impl OrthographicCamera {
fn get_view_matrix(&self) -> Mat4 {
let projection = Mat4::orthographic_rh_gl(
self.left,
self.right,
self.bottom,
self.top,
self.near,
self.far,
);
opengl_to_wgpu_matrix() * projection
}
}
impl Default for OrthographicCamera {
fn default() -> Self {
OrthographicCamera {
left: 0.0,
right: 0.0,
bottom: 0.0,
top: 0.0,
near: 0.0,
far: 1.0,
}
}
}
pub struct PerspectiveCamera {
pub fov: f32,
pub aspect_ratio: f32,
pub near: f32,
pub far: f32,
}
impl PerspectiveCamera {
pub fn get_view_matrix(&self) -> Mat4 {
let projection = Mat4::perspective_rh_gl(self.fov, self.aspect_ratio, self.near, self.far);
opengl_to_wgpu_matrix() * projection
}
}
impl Default for PerspectiveCamera {
fn default() -> Self {
PerspectiveCamera {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}
}
}
pub enum CameraType {
Perspective(PerspectiveCamera),
Orthographic(OrthographicCamera),
}
impl CameraType {
pub fn default_perspective() -> CameraType {
CameraType::Perspective(PerspectiveCamera::default())
}
pub fn default_orthographic() -> CameraType {
CameraType::Orthographic(OrthographicCamera::default())
}
}
impl Default for CameraType {
fn default() -> Self {
CameraType::default_perspective()
}
}
#[derive(Default)]
pub struct Camera { pub struct Camera {
pub view_matrix: Mat4, pub view_matrix: Mat4,
pub camera_type: CameraType, pub camera_type: CameraType,
@ -34,53 +103,20 @@ impl Camera {
} }
pub fn update(&mut self, width: u32, height: u32) { pub fn update(&mut self, width: u32, height: u32) {
match &mut self.camera_type { self.view_matrix = match &mut self.camera_type {
CameraType::Projection { CameraType::Perspective(projection) => {
aspect_ratio, projection.aspect_ratio = width as f32 / height as f32;
fov, projection.get_view_matrix()
near,
far,
} => {
*aspect_ratio = width as f32 / height as f32;
self.view_matrix =
get_perspective_projection_matrix(*fov, *aspect_ratio, *near, *far)
} }
CameraType::Orthographic { CameraType::Orthographic(orthographic) => {
left, orthographic.right = width as f32;
right, orthographic.top = height as f32;
bottom, orthographic.get_view_matrix()
top,
near,
far,
} => {
*right = width as f32;
*top = height as f32;
self.view_matrix =
get_orthographic_projection_matrix(*left, *right, *bottom, *top, *near, *far)
} }
} }
} }
} }
pub fn get_perspective_projection_matrix(fov: f32, aspect_ratio: f32, near: f32, far: f32) -> Mat4 {
let projection = Mat4::perspective_rh_gl(fov, aspect_ratio, near, far);
opengl_to_wgpu_matrix() * projection
}
pub fn get_orthographic_projection_matrix(
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> Mat4 {
let projection = Mat4::orthographic_rh_gl(left, right, bottom, top, near, far);
opengl_to_wgpu_matrix() * projection
}
pub fn opengl_to_wgpu_matrix() -> Mat4 { pub fn opengl_to_wgpu_matrix() -> Mat4 {
Mat4::from_cols_array(&[ Mat4::from_cols_array(&[
1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0,

View file

@ -1,5 +1,5 @@
use super::Color; use super::{PerspectiveCamera, Color};
use crate::{math, prelude::Translation, render::camera}; use crate::{math, prelude::Translation};
use std::ops::Range; use std::ops::Range;
use zerocopy::{AsBytes, FromBytes}; use zerocopy::{AsBytes, FromBytes};
@ -29,12 +29,14 @@ pub struct LightRaw {
impl LightRaw { impl LightRaw {
pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw { pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw {
let proj = camera::get_perspective_projection_matrix( let perspective = PerspectiveCamera {
light.fov, fov: light.fov,
1.0, aspect_ratio: 1.0,
light.depth.start, near: light.depth.start,
light.depth.end, far: light.depth.end,
) * *transform; };
let proj = perspective.get_view_matrix() * *transform;
let (x, y, z) = translation.0.into(); let (x, y, z) = translation.0.into();
LightRaw { LightRaw {
proj: proj.to_cols_array_2d(), proj: proj.to_cols_array_2d(),