Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_camera/src/components.rs
9353 views
1
use crate::{primitives::Frustum, Camera, CameraProjection, OrthographicProjection, Projection};
2
use bevy_ecs::prelude::*;
3
use bevy_reflect::{std_traits::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize};
4
use bevy_transform::prelude::{GlobalTransform, Transform};
5
use serde::{Deserialize, Serialize};
6
use 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
)]
16
pub 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)]
25
pub 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
32
impl 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)]
43
pub struct Camera3dDepthTextureUsage(pub u32);
44
45
impl From<TextureUsages> for Camera3dDepthTextureUsage {
46
fn from(value: TextureUsages) -> Self {
47
Self(value.bits())
48
}
49
}
50
51
impl 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)]
60
pub 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
68
impl Default for Camera3dDepthLoadOp {
69
fn default() -> Self {
70
Camera3dDepthLoadOp::Clear(0.0)
71
}
72
}
73
74
impl 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)]
89
pub struct Hdr;
90
91