bones_render/camera.rs
1//! Camera components.
2
3use crate::prelude::*;
4
5/// Makes an entity behave like a camera.
6///
7/// The entity must also have a [`Transform`] component for the camera to render anything.
8#[derive(Clone, Copy, Debug, TypeUlid)]
9#[ulid = "01GNR2978NRN7PH5XWBXP3KMD7"]
10#[repr(C)]
11pub struct Camera {
12 /// The height of the camera in in-game pixels.
13 ///
14 /// The width of the camera will be determined from the window aspect ratio.
15 pub height: f32,
16 /// Whether or not the camera is enabled and rendering.
17 pub active: bool,
18 /// An optional viewport override, allowing you to specify that the camera should render to only
19 /// a portion of the window.
20 ///
21 /// This can be used, for example, for split screen functionality.
22 pub viewport: Option<Viewport>,
23}
24
25/// A custom viewport specification for a [`Camera`].
26#[derive(Clone, Copy, Debug)]
27#[repr(C)]
28pub struct Viewport {
29 /// The physical position to render this viewport to within the RenderTarget of this Camera.
30 /// (0,0) corresponds to the top-left corner.
31 pub position: UVec2,
32 /// The physical size of the viewport rectangle to render to within the RenderTarget of this
33 /// Camera. The origin of the rectangle is in the top-left corner.
34 pub size: UVec2,
35 /// The minimum depth to render (on a scale from 0.0 to 1.0).
36 pub depth_min: f32,
37 /// The maximum depth to render (on a scale from 0.0 to 1.0).
38 pub depth_max: f32,
39}
40
41impl Default for Camera {
42 fn default() -> Self {
43 Self {
44 height: 400.0,
45 active: true,
46 viewport: None,
47 }
48 }
49}
50
51/// Resource for controlling the clear color.
52#[derive(Deref, DerefMut, Clone, Copy, TypeUlid, Default)]
53#[ulid = "01GP4XRQYRPQNX4J22E513975M"]
54pub struct ClearColor(pub Color);