Skip to main content

bevy_camera/
components.rs

1use crate::{primitives::Frustum, Camera, CameraProjection, OrthographicProjection, Projection};
2use bevy_ecs::prelude::*;
3use bevy_reflect::{std_traits::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize};
4use bevy_transform::prelude::{GlobalTransform, Transform};
5use serde::{Deserialize, Serialize};
6use wgpu_types::{LoadOp, TextureUsages};
7
8/// A 2D camera component. Enables the 2D render graph for a [`Camera`].
9#[derive(Component, Default, Reflect, Clone)]
10#[reflect(Component, Default, Clone)]
11#[require(
12    Camera,
13    Projection::Orthographic(OrthographicProjection::default_2d()),
14    Frustum = OrthographicProjection::default_2d().compute_frustum(&GlobalTransform::from(Transform::default())),
15)]
16pub struct Camera2d;
17
18/// A 3D camera component. Enables the main 3D render graph for a [`Camera`].
19///
20/// The camera coordinate space is right-handed X-right, Y-up, Z-back.
21/// This means "forward" is -Z.
22#[derive(Component, Reflect, Clone)]
23#[reflect(Component, Default, Clone)]
24#[require(Camera, Projection)]
25pub struct Camera3d {
26    /// The depth clear operation to perform for the main 3d pass.
27    pub depth_load_op: Camera3dDepthLoadOp,
28    /// The texture usages for the depth texture created for the main 3d pass.
29    pub depth_texture_usages: Camera3dDepthTextureUsage,
30}
31
32impl Default for Camera3d {
33    fn default() -> Self {
34        Self {
35            depth_load_op: Default::default(),
36            depth_texture_usages: TextureUsages::RENDER_ATTACHMENT.into(),
37        }
38    }
39}
40
41#[derive(Clone, Copy, Reflect, Serialize, Deserialize)]
42#[reflect(Serialize, Deserialize, Clone)]
43pub struct Camera3dDepthTextureUsage(pub u32);
44
45impl From<TextureUsages> for Camera3dDepthTextureUsage {
46    fn from(value: TextureUsages) -> Self {
47        Self(value.bits())
48    }
49}
50
51impl From<Camera3dDepthTextureUsage> for TextureUsages {
52    fn from(value: Camera3dDepthTextureUsage) -> Self {
53        Self::from_bits_truncate(value.0)
54    }
55}
56
57/// The depth clear operation to perform for the main 3d pass.
58#[derive(Reflect, Serialize, Deserialize, Clone, Debug)]
59#[reflect(Serialize, Deserialize, Clone, Default)]
60pub enum Camera3dDepthLoadOp {
61    /// Clear with a specified value.
62    /// Note that 0.0 is the far plane due to bevy's use of reverse-z projections.
63    Clear(f32),
64    /// Load from memory.
65    Load,
66}
67
68impl Default for Camera3dDepthLoadOp {
69    fn default() -> Self {
70        Camera3dDepthLoadOp::Clear(0.0)
71    }
72}
73
74impl From<Camera3dDepthLoadOp> for LoadOp<f32> {
75    fn from(config: Camera3dDepthLoadOp) -> Self {
76        match config {
77            Camera3dDepthLoadOp::Clear(x) => LoadOp::Clear(x),
78            Camera3dDepthLoadOp::Load => LoadOp::Load,
79        }
80    }
81}
82
83/// If this component is added to a camera, the camera will use an intermediate "high dynamic range" render texture.
84/// This allows rendering with a wider range of lighting values. However, this does *not* affect
85/// whether the camera will render with hdr display output (which bevy does not support currently)
86/// and only affects the intermediate render texture.
87#[derive(Component, Default, Copy, Clone, Reflect, PartialEq, Eq, Hash, Debug)]
88#[reflect(Component, Default, PartialEq, Hash, Debug)]
89pub struct Hdr;
90
91/// Color space for alpha compositing. Affects how overlapping semi-transparent layers blend.
92#[derive(Component, Copy, Clone, Reflect, PartialEq, Eq, Hash, Debug, Default)]
93#[reflect(Component, PartialEq, Hash, Debug, Default)]
94pub enum CompositingSpace {
95    /// Gamma-encoded blending. Matches most image editors. Uses default sRGB target.
96    #[default]
97    Srgb,
98    /// Linear light blending. Physically correct.
99    Linear,
100    /// Perceptually uniform blending. Often smoother gradients. Requires [`Hdr`] because its value can be outside [0, 1].
101    Oklab,
102}