Skip to content

Commit 6a54683

Browse files
committed
Document the bevy_render::camera module tree (#3528)
# Objective Document most of the public items of the `bevy_render::camera` module and its sub-modules. ## Solution Add docs to most public items. Follow-up from #3447.
1 parent 0c98a2f commit 6a54683

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

crates/bevy_render/src/camera/camera.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,24 @@ pub struct ComputedCameraValues {
7070
target_info: Option<RenderTargetInfo>,
7171
}
7272

73+
/// The defining component for camera entities, storing information about how and what to render
74+
/// through this camera.
75+
///
76+
/// The [`Camera`] component is added to an entity to define the properties of the viewpoint from
77+
/// which rendering occurs. It defines the position of the view to render, the projection method
78+
/// to transform the 3D objects into a 2D image, as well as the render target into which that image
79+
/// is produced.
80+
///
81+
/// Adding a camera is typically done by adding a bundle, either the `Camera2dBundle` or the
82+
/// `Camera3dBundle`.
7383
#[derive(Component, Debug, Reflect, FromReflect, Clone)]
7484
#[reflect(Component)]
7585
pub struct Camera {
7686
/// If set, this camera will render to the given [`Viewport`] rectangle within the configured [`RenderTarget`].
7787
pub viewport: Option<Viewport>,
7888
/// Cameras with a lower priority will be rendered before cameras with a higher priority.
7989
pub priority: isize,
80-
/// If this is set to true, this camera will be rendered to its specified [`RenderTarget`]. If false, this
90+
/// If this is set to `true`, this camera will be rendered to its specified [`RenderTarget`]. If `false`, this
8191
/// camera will not be rendered.
8292
pub is_active: bool,
8393
/// Computed values for this camera, such as the projection matrix and the render target size.
@@ -305,6 +315,21 @@ impl RenderTarget {
305315
}
306316
}
307317

318+
/// System in charge of updating a [`Camera`] when its window or projection changes.
319+
///
320+
/// The system detects window creation and resize events to update the camera projection if
321+
/// needed. It also queries any [`CameraProjection`] component associated with the same entity
322+
/// as the [`Camera`] one, to automatically update the camera projection matrix.
323+
///
324+
/// The system function is generic over the camera projection type, and only instances of
325+
/// [`OrthographicProjection`] and [`PerspectiveProjection`] are automatically added to
326+
/// the app, as well as the runtime-selected [`Projection`]. The system runs during the
327+
/// [`CoreStage::PostUpdate`] stage.
328+
///
329+
/// [`OrthographicProjection`]: crate::camera::OrthographicProjection
330+
/// [`PerspectiveProjection`]: crate::camera::PerspectiveProjection
331+
/// [`Projection`]: crate::camera::Projection
332+
/// [`CoreStage::PostUpdate`]: bevy_app::CoreStage::PostUpdate
308333
pub fn camera_system<T: CameraProjection + Component>(
309334
mut window_resized_events: EventReader<WindowResized>,
310335
mut window_created_events: EventReader<WindowCreated>,
@@ -317,19 +342,18 @@ pub fn camera_system<T: CameraProjection + Component>(
317342
)>,
318343
) {
319344
let mut changed_window_ids = Vec::new();
320-
// handle resize events. latest events are handled first because we only want to resize each
321-
// window once
322-
for event in window_resized_events.iter().rev() {
345+
346+
// Collect all unique window IDs of changed windows by inspecting created windows
347+
for event in window_created_events.iter() {
323348
if changed_window_ids.contains(&event.id) {
324349
continue;
325350
}
326351

327352
changed_window_ids.push(event.id);
328353
}
329354

330-
// handle resize events. latest events are handled first because we only want to resize each
331-
// window once
332-
for event in window_created_events.iter().rev() {
355+
// Collect all unique window IDs of changed windows by inspecting resized windows
356+
for event in window_resized_events.iter() {
333357
if changed_window_ids.contains(&event.id) {
334358
continue;
335359
}

crates/bevy_render/src/camera/projection.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ impl<T: CameraProjection + Component + GetTypeRegistration> Plugin for CameraPro
3838
}
3939
}
4040

41+
/// Trait to control the projection matrix of a camera.
42+
///
43+
/// Components implementing this trait are automatically polled for changes, and used
44+
/// to recompute the camera projection matrix of the [`Camera`] component attached to
45+
/// the same entity as the component implementing this trait.
46+
///
47+
/// [`Camera`]: crate::camera::Camera
4148
pub trait CameraProjection {
4249
fn get_projection_matrix(&self) -> Mat4;
4350
fn update(&mut self, width: f32, height: f32);

0 commit comments

Comments
 (0)