mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
refactor cameras. add defaults
This commit is contained in:
parent
fb496a6172
commit
d2e160d44a
14 changed files with 129 additions and 166 deletions
|
@ -107,18 +107,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -53,12 +53,7 @@ fn create_entities_insert_vec(
|
|||
world.insert(
|
||||
(),
|
||||
vec![(
|
||||
Camera::new(CameraType::Projection {
|
||||
fov: std::f32::consts::PI / 4.0,
|
||||
near: 1.0,
|
||||
far: 1000.0,
|
||||
aspect_ratio: 1.0,
|
||||
}),
|
||||
Camera::default(),
|
||||
ActiveCamera,
|
||||
LocalToWorld(Mat4::look_at_rh(
|
||||
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))
|
||||
// camera
|
||||
.build_entity()
|
||||
.add(Camera::new(CameraType::Projection {
|
||||
fov: std::f32::consts::PI / 4.0,
|
||||
near: 1.0,
|
||||
far: 1000.0,
|
||||
aspect_ratio: 1.0,
|
||||
}))
|
||||
.add(Camera::default())
|
||||
.add(ActiveCamera)
|
||||
.add(LocalToWorld(Mat4::look_at_rh(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
|
@ -137,6 +127,7 @@ fn create_entities_builder_archetype(
|
|||
.add_entity(MeshEntity {
|
||||
mesh: cube_handle,
|
||||
material: cube_material_handle,
|
||||
translation: Translation::new(0.0, 0.0, 1.0),
|
||||
..Default::default()
|
||||
})
|
||||
// light
|
||||
|
@ -146,18 +137,12 @@ fn create_entities_builder_archetype(
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -63,18 +63,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let mut rng = StdRng::from_entropy();
|
||||
|
|
|
@ -50,12 +50,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
vec![
|
||||
// camera
|
||||
(
|
||||
Camera::new(CameraType::Projection {
|
||||
fov: std::f32::consts::PI / 4.0,
|
||||
near: 1.0,
|
||||
far: 1000.0,
|
||||
aspect_ratio: 1.0,
|
||||
}),
|
||||
Camera::default(),
|
||||
ActiveCamera,
|
||||
LocalToWorld(Mat4::look_at_rh(
|
||||
Vec3::new(6.0, -40.0, 20.0),
|
||||
|
|
|
@ -56,23 +56,16 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
// light
|
||||
.add_entity(LightEntity {
|
||||
translation: Translation::new(4.0, -4.0, 5.0),
|
||||
rotation: Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||
..Default::default()
|
||||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(5.0, 10.0, 10.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -49,18 +49,12 @@ pub fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.build();
|
||||
}
|
|
@ -53,18 +53,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -63,18 +63,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let mut rng = StdRng::from_entropy();
|
||||
|
|
|
@ -10,7 +10,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
|
||||
let mut texture_storage = resources.get_mut::<AssetStorage<Texture>>().unwrap();
|
||||
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);
|
||||
|
||||
|
@ -52,18 +52,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -31,30 +31,16 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
})
|
||||
// 3d camera
|
||||
.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(
|
||||
Vec3::new(3.0, 8.0, 5.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, 0.0, 1.0),
|
||||
)),
|
||||
..Default::default()
|
||||
})
|
||||
// 2d camera
|
||||
.add_entity(Camera2dEntity {
|
||||
camera: Camera::new(CameraType::Orthographic {
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
bottom: 0.0,
|
||||
top: 0.0,
|
||||
near: 0.0,
|
||||
far: 1.0,
|
||||
}),
|
||||
active_camera_2d: ActiveCamera2d,
|
||||
..Default::default()
|
||||
})
|
||||
// bottom left anchor with vertical fill
|
||||
.add_entity(UiEntity {
|
||||
|
|
|
@ -25,15 +25,8 @@ fn build_move_system() -> Box<dyn Schedulable> {
|
|||
|
||||
fn setup(world: &mut World, _resources: &mut Resources) {
|
||||
let mut builder = world.build().add_entity(Camera2dEntity {
|
||||
camera: Camera::new(CameraType::Orthographic {
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
bottom: 0.0,
|
||||
top: 0.0,
|
||||
near: 0.0,
|
||||
far: 1.0,
|
||||
}),
|
||||
active_camera_2d: ActiveCamera2d,
|
||||
camera: Camera::new(CameraType::default_orthographic()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let mut prev = Vec2::default();
|
||||
|
|
|
@ -35,7 +35,7 @@ pub struct LightEntity {
|
|||
pub rotation: Rotation,
|
||||
}
|
||||
|
||||
#[derive(EntityArchetype)]
|
||||
#[derive(EntityArchetype, Default)]
|
||||
pub struct CameraEntity {
|
||||
pub camera: Camera,
|
||||
pub active_camera: ActiveCamera,
|
||||
|
@ -48,6 +48,15 @@ pub struct Camera2dEntity {
|
|||
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)]
|
||||
pub struct UiEntity {
|
||||
pub node: Node,
|
||||
|
|
|
@ -1,25 +1,94 @@
|
|||
use crate::math::Mat4;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ActiveCamera;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ActiveCamera2d;
|
||||
|
||||
pub enum CameraType {
|
||||
Projection {
|
||||
fov: f32,
|
||||
aspect_ratio: f32,
|
||||
near: f32,
|
||||
far: f32,
|
||||
},
|
||||
Orthographic {
|
||||
left: f32,
|
||||
right: f32,
|
||||
bottom: f32,
|
||||
top: f32,
|
||||
near: f32,
|
||||
far: f32,
|
||||
},
|
||||
pub struct OrthographicCamera {
|
||||
pub left: f32,
|
||||
pub right: f32,
|
||||
pub bottom: f32,
|
||||
pub top: f32,
|
||||
pub near: f32,
|
||||
pub 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 view_matrix: Mat4,
|
||||
pub camera_type: CameraType,
|
||||
|
@ -34,53 +103,20 @@ impl Camera {
|
|||
}
|
||||
|
||||
pub fn update(&mut self, width: u32, height: u32) {
|
||||
match &mut self.camera_type {
|
||||
CameraType::Projection {
|
||||
aspect_ratio,
|
||||
fov,
|
||||
near,
|
||||
far,
|
||||
} => {
|
||||
*aspect_ratio = width as f32 / height as f32;
|
||||
self.view_matrix =
|
||||
get_perspective_projection_matrix(*fov, *aspect_ratio, *near, *far)
|
||||
self.view_matrix = match &mut self.camera_type {
|
||||
CameraType::Perspective(projection) => {
|
||||
projection.aspect_ratio = width as f32 / height as f32;
|
||||
projection.get_view_matrix()
|
||||
}
|
||||
CameraType::Orthographic {
|
||||
left,
|
||||
right,
|
||||
bottom,
|
||||
top,
|
||||
near,
|
||||
far,
|
||||
} => {
|
||||
*right = width as f32;
|
||||
*top = height as f32;
|
||||
self.view_matrix =
|
||||
get_orthographic_projection_matrix(*left, *right, *bottom, *top, *near, *far)
|
||||
CameraType::Orthographic(orthographic) => {
|
||||
orthographic.right = width as f32;
|
||||
orthographic.top = height as f32;
|
||||
orthographic.get_view_matrix()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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,5 +1,5 @@
|
|||
use super::Color;
|
||||
use crate::{math, prelude::Translation, render::camera};
|
||||
use super::{PerspectiveCamera, Color};
|
||||
use crate::{math, prelude::Translation};
|
||||
use std::ops::Range;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
|
||||
|
@ -29,12 +29,14 @@ pub struct LightRaw {
|
|||
|
||||
impl LightRaw {
|
||||
pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw {
|
||||
let proj = camera::get_perspective_projection_matrix(
|
||||
light.fov,
|
||||
1.0,
|
||||
light.depth.start,
|
||||
light.depth.end,
|
||||
) * *transform;
|
||||
let perspective = PerspectiveCamera {
|
||||
fov: light.fov,
|
||||
aspect_ratio: 1.0,
|
||||
near: light.depth.start,
|
||||
far: light.depth.end,
|
||||
};
|
||||
|
||||
let proj = perspective.get_view_matrix() * *transform;
|
||||
let (x, y, z) = translation.0.into();
|
||||
LightRaw {
|
||||
proj: proj.to_cols_array_2d(),
|
||||
|
|
Loading…
Reference in a new issue