-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - use marker components for cameras instead of name strings #3635
Changes from all commits
c95f4a4
fde7ff4
00da121
03cf004
124138d
b6cd8c9
979cdb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,48 @@ | ||
use crate::{ | ||
camera::{ | ||
Camera, CameraPlugin, DepthCalculation, OrthographicProjection, PerspectiveProjection, | ||
ScalingMode, | ||
}, | ||
camera::{Camera, DepthCalculation, OrthographicProjection, PerspectiveProjection}, | ||
primitives::Frustum, | ||
view::VisibleEntities, | ||
}; | ||
use bevy_ecs::bundle::Bundle; | ||
use bevy_ecs::{bundle::Bundle, prelude::Component}; | ||
use bevy_math::Vec3; | ||
use bevy_transform::components::{GlobalTransform, Transform}; | ||
|
||
use super::CameraProjection; | ||
use super::{CameraProjection, ScalingMode}; | ||
|
||
#[derive(Component, Default)] | ||
pub struct Camera3d; | ||
|
||
#[derive(Component, Default)] | ||
pub struct Camera2d; | ||
|
||
/// Component bundle for camera entities with perspective projection | ||
/// | ||
/// Use this for 3D rendering. | ||
#[derive(Bundle)] | ||
pub struct PerspectiveCameraBundle { | ||
pub struct PerspectiveCameraBundle<M: Component> { | ||
pub camera: Camera, | ||
pub perspective_projection: PerspectiveProjection, | ||
pub visible_entities: VisibleEntities, | ||
pub frustum: Frustum, | ||
pub transform: Transform, | ||
pub global_transform: GlobalTransform, | ||
pub marker: M, | ||
} | ||
|
||
impl Default for PerspectiveCameraBundle<Camera3d> { | ||
fn default() -> Self { | ||
PerspectiveCameraBundle::new_3d() | ||
} | ||
} | ||
|
||
impl PerspectiveCameraBundle { | ||
impl PerspectiveCameraBundle<Camera3d> { | ||
pub fn new_3d() -> Self { | ||
Default::default() | ||
PerspectiveCameraBundle::new() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels a bit weird to me? Why not just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to have only one |
||
} | ||
} | ||
|
||
pub fn with_name(name: &str) -> Self { | ||
impl<M: Component + Default> PerspectiveCameraBundle<M> { | ||
pub fn new() -> Self { | ||
let perspective_projection = PerspectiveProjection::default(); | ||
let view_projection = perspective_projection.get_projection_matrix(); | ||
let frustum = Frustum::from_view_projection( | ||
|
@@ -41,7 +53,6 @@ impl PerspectiveCameraBundle { | |
); | ||
PerspectiveCameraBundle { | ||
camera: Camera { | ||
name: Some(name.to_string()), | ||
near: perspective_projection.near, | ||
far: perspective_projection.far, | ||
..Default::default() | ||
|
@@ -51,30 +62,56 @@ impl PerspectiveCameraBundle { | |
frustum, | ||
transform: Default::default(), | ||
global_transform: Default::default(), | ||
marker: M::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for PerspectiveCameraBundle { | ||
fn default() -> Self { | ||
PerspectiveCameraBundle::with_name(CameraPlugin::CAMERA_3D) | ||
} | ||
} | ||
|
||
/// Component bundle for camera entities with orthographic projection | ||
/// | ||
/// Use this for 2D games, isometric games, CAD-like 3D views. | ||
#[derive(Bundle)] | ||
pub struct OrthographicCameraBundle { | ||
pub struct OrthographicCameraBundle<M: Component> { | ||
pub camera: Camera, | ||
pub orthographic_projection: OrthographicProjection, | ||
pub visible_entities: VisibleEntities, | ||
pub frustum: Frustum, | ||
pub transform: Transform, | ||
pub global_transform: GlobalTransform, | ||
pub marker: M, | ||
} | ||
|
||
impl OrthographicCameraBundle<Camera3d> { | ||
pub fn new_3d() -> Self { | ||
let orthographic_projection = OrthographicProjection { | ||
scaling_mode: ScalingMode::FixedVertical, | ||
depth_calculation: DepthCalculation::Distance, | ||
..Default::default() | ||
}; | ||
let view_projection = orthographic_projection.get_projection_matrix(); | ||
let frustum = Frustum::from_view_projection( | ||
&view_projection, | ||
&Vec3::ZERO, | ||
&Vec3::Z, | ||
orthographic_projection.far(), | ||
); | ||
OrthographicCameraBundle { | ||
camera: Camera { | ||
near: orthographic_projection.near, | ||
far: orthographic_projection.far, | ||
..Default::default() | ||
}, | ||
orthographic_projection, | ||
visible_entities: VisibleEntities::default(), | ||
frustum, | ||
transform: Default::default(), | ||
global_transform: Default::default(), | ||
marker: Camera3d, | ||
} | ||
} | ||
} | ||
|
||
impl OrthographicCameraBundle { | ||
impl OrthographicCameraBundle<Camera2d> { | ||
/// Create an orthographic projection camera to render 2D content. | ||
/// | ||
/// The projection creates a camera space where X points to the right of the screen, | ||
|
@@ -112,7 +149,6 @@ impl OrthographicCameraBundle { | |
); | ||
OrthographicCameraBundle { | ||
camera: Camera { | ||
name: Some(CameraPlugin::CAMERA_2D.to_string()), | ||
near: orthographic_projection.near, | ||
far: orthographic_projection.far, | ||
..Default::default() | ||
|
@@ -122,58 +158,7 @@ impl OrthographicCameraBundle { | |
frustum, | ||
transform, | ||
global_transform: Default::default(), | ||
} | ||
} | ||
|
||
pub fn new_3d() -> Self { | ||
let orthographic_projection = OrthographicProjection { | ||
scaling_mode: ScalingMode::FixedVertical, | ||
depth_calculation: DepthCalculation::Distance, | ||
..Default::default() | ||
}; | ||
let view_projection = orthographic_projection.get_projection_matrix(); | ||
let frustum = Frustum::from_view_projection( | ||
&view_projection, | ||
&Vec3::ZERO, | ||
&Vec3::Z, | ||
orthographic_projection.far(), | ||
); | ||
OrthographicCameraBundle { | ||
camera: Camera { | ||
name: Some(CameraPlugin::CAMERA_3D.to_string()), | ||
near: orthographic_projection.near, | ||
far: orthographic_projection.far, | ||
..Default::default() | ||
}, | ||
orthographic_projection, | ||
visible_entities: VisibleEntities::default(), | ||
frustum, | ||
transform: Default::default(), | ||
global_transform: Default::default(), | ||
} | ||
} | ||
|
||
pub fn with_name(name: &str) -> Self { | ||
let orthographic_projection = OrthographicProjection::default(); | ||
let view_projection = orthographic_projection.get_projection_matrix(); | ||
let frustum = Frustum::from_view_projection( | ||
&view_projection, | ||
&Vec3::ZERO, | ||
&Vec3::Z, | ||
orthographic_projection.far(), | ||
); | ||
OrthographicCameraBundle { | ||
camera: Camera { | ||
name: Some(name.to_string()), | ||
near: orthographic_projection.near, | ||
far: orthographic_projection.far, | ||
..Default::default() | ||
}, | ||
orthographic_projection, | ||
visible_entities: VisibleEntities::default(), | ||
frustum, | ||
transform: Default::default(), | ||
global_transform: Default::default(), | ||
marker: Camera2d, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this style of API; I've used it before in my games and it's great.