wde_camera/lib.rs
1//! Camera plugin for WaterDropEngine.
2//!
3//! This plugin provides a camera component and a camera view component, as well as a system to extract the active camera and update the camera uniform each frame.
4//!
5//! # Example
6//! To spawn the default active camera, add a [`ActiveCamera`](crate::camera::ActiveCamera) component.
7//! This will automatically spawn a [`Camera`](crate::camera::Camera) component and a [`CameraView`](crate::view::CameraView) component with default parameters.
8//! For example:
9//! ```rust
10//! commands.spawn((
11//! Transform::from_xyz(0.0, 2.0, 6.0).looking_at(Vec3::ZERO, Vec3::Y),
12//! ActiveCamera
13//! ));
14//! ```
15//!
16//! If you want to customize the camera view parameters, you can spawn the components separately and add the `ActiveCamera` marker:
17//! ```rust
18//! commands.spawn((
19//! Transform::from_xyz(0.0, 2.0, 6.0).looking_at(Vec3::ZERO, Vec3::Y),
20//! Camera,
21//! CameraView::default(),
22//! ActiveCamera
23//! ));
24//! ```
25//!
26//! # Roles
27//! Several cameras can coexist, e.g. a game one and an editor one. Each role is held by a single
28//! camera, and moved with the [`SetCameraRole`](crate::roles::SetCameraRole) command (or from the
29//! `Camera/Cameras` editor window):
30//! - [`ActiveCamera`](crate::camera::ActiveCamera): rendered on screen, and used for picking.
31//! - [`CullingCamera`](crate::roles::CullingCamera): the view visibility is computed from.
32//! - [`ControlledCamera`](crate::roles::ControlledCamera): moved by the camera controllers.
33//!
34//! Roles no camera holds are given to the `ActiveCamera`.
35//!
36//! The camera uniform will then be automatically updated each frame with the active camera's transform and view parameters.
37//! To consumes the camera uniform in a shader, you can use the [`CameraRender`](crate::camera::CameraRender) render binding.
38//! It is accessible by the resource `Res<RenderAssets<GpuRenderBinding<CameraRender>>>` in render systems, or `SBindings<CameraRender>` in shader code.
39//! See [`GpuRenderBinding`](wde_renderer::prelude::GpuRenderBinding) for more details on how to use render bindings in shaders.
40use bevy::prelude::*;
41
42use crate::camera::CameraFeature;
43
44#[doc(hidden)]
45pub mod prelude {
46 pub use crate::camera::{ActiveCamera, Camera, CameraBinding, CameraRender, ProjectionJitter};
47 pub use crate::frustum::{Aabb, Frustum, FrustumTest};
48 pub use crate::roles::{CameraRole, ControlledCamera, CullingCamera, SetCameraRole};
49 pub use crate::transform::TransformUniform;
50 pub use crate::view::CameraView;
51}
52
53mod camera;
54#[cfg(debug_assertions)]
55mod editor;
56mod frustum;
57mod roles;
58mod transform;
59mod view;
60
61pub struct CameraPlugin;
62impl Plugin for CameraPlugin {
63 fn build(&self, app: &mut App) {
64 app.add_plugins(CameraFeature)
65 .register_type::<roles::CullingCamera>()
66 .register_type::<roles::ControlledCamera>()
67 .add_systems(First, roles::assign_default_roles);
68
69 #[cfg(debug_assertions)]
70 app.add_plugins(editor::CameraPropertiesEditor);
71 }
72}