three_d/renderer/viewer/
camera.rs

1use crate::*;
2
3///
4/// Represents a camera used for viewing 2D and 3D objects.
5///
6#[derive(Clone, Debug)]
7pub struct Camera {
8    camera: three_d_asset::Camera,
9    /// This tone mapping is applied to the final color of renders using this camera.
10    pub tone_mapping: ToneMapping,
11    /// This color mapping is applied to the final color of renders using this camera.
12    pub color_mapping: ColorMapping,
13}
14
15impl Viewer for Camera {
16    fn position(&self) -> Vec3 {
17        self.camera.position()
18    }
19
20    fn view(&self) -> Mat4 {
21        self.camera.view()
22    }
23
24    fn projection(&self) -> Mat4 {
25        self.camera.projection()
26    }
27
28    fn viewport(&self) -> Viewport {
29        self.camera.viewport()
30    }
31
32    fn z_near(&self) -> f32 {
33        self.camera.z_near()
34    }
35
36    fn z_far(&self) -> f32 {
37        self.camera.z_far()
38    }
39
40    fn color_mapping(&self) -> ColorMapping {
41        self.color_mapping
42    }
43
44    fn tone_mapping(&self) -> ToneMapping {
45        self.tone_mapping
46    }
47}
48
49impl Camera {
50    ///
51    /// New camera which projects the world with an orthographic projection.
52    ///
53    pub fn new_orthographic(
54        viewport: Viewport,
55        position: Vec3,
56        target: Vec3,
57        up: Vec3,
58        height: f32,
59        z_near: f32,
60        z_far: f32,
61    ) -> Self {
62        Self {
63            camera: three_d_asset::Camera::new_orthographic(
64                viewport, position, target, up, height, z_near, z_far,
65            ),
66            tone_mapping: ToneMapping::default(),
67            color_mapping: ColorMapping::default(),
68        }
69    }
70
71    ///
72    /// New camera which projects the world with a perspective projection.
73    ///
74    pub fn new_perspective(
75        viewport: Viewport,
76        position: Vec3,
77        target: Vec3,
78        up: Vec3,
79        field_of_view_y: impl Into<Radians>,
80        z_near: f32,
81        z_far: f32,
82    ) -> Self {
83        Self {
84            camera: three_d_asset::Camera::new_perspective(
85                viewport,
86                position,
87                target,
88                up,
89                field_of_view_y,
90                z_near,
91                z_far,
92            ),
93            tone_mapping: ToneMapping::default(),
94            color_mapping: ColorMapping::default(),
95        }
96    }
97
98    ///
99    /// Returns an orthographic camera for viewing 2D content.
100    /// The camera is placed at the center of the given viewport.
101    /// The (0, 0) position is at the bottom left corner and the
102    /// (`viewport.width`, `viewport.height`) position is at the top right corner.
103    ///
104    pub fn new_2d(viewport: Viewport) -> Self {
105        Self::new_orthographic(
106            viewport,
107            vec3(
108                viewport.width as f32 * 0.5,
109                viewport.height as f32 * 0.5,
110                1.0,
111            ),
112            vec3(
113                viewport.width as f32 * 0.5,
114                viewport.height as f32 * 0.5,
115                0.0,
116            ),
117            vec3(0.0, 1.0, 0.0),
118            viewport.height as f32,
119            0.0,
120            10.0,
121        )
122    }
123
124    ///
125    /// Disables the tone and color mapping so as to be ready for rendering into an intermediate render target with this camera.
126    ///
127    pub fn disable_tone_and_color_mapping(&mut self) {
128        self.tone_mapping = ToneMapping::None;
129        self.color_mapping = ColorMapping::None;
130    }
131
132    ///
133    /// Sets the tone and color mapping to default so as to be ready for rendering into the final render target (usually the screen) with this camera.
134    ///
135    pub fn set_default_tone_and_color_mapping(&mut self) {
136        self.tone_mapping = ToneMapping::default();
137        self.color_mapping = ColorMapping::default();
138    }
139}
140
141impl std::ops::Deref for Camera {
142    type Target = three_d_asset::Camera;
143    fn deref(&self) -> &Self::Target {
144        &self.camera
145    }
146}
147
148impl std::ops::DerefMut for Camera {
149    fn deref_mut(&mut self) -> &mut Self::Target {
150        &mut self.camera
151    }
152}