Skip to main content

gltf_reader/
camera.rs

1use alloc::borrow::Cow;
2use ownable::IntoOwned;
3use serde::Deserialize;
4
5use crate::{Extensions, Extras};
6
7/// glTF known types of camera projection.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum CameraTypeEnum {
10    Perspective,
11    Orthographic,
12}
13
14/// Type of camera projection.
15#[derive(Clone, PartialEq, Eq, Deserialize, IntoOwned)]
16#[serde(transparent)]
17pub struct CameraType<'a>(#[serde(borrow)] pub Cow<'a, str>);
18
19impl core::fmt::Debug for CameraType<'_> {
20    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21        if let Some(e) = self.to_enum() {
22            e.fmt(f)
23        } else {
24            self.0.fmt(f)
25        }
26    }
27}
28
29impl CameraType<'_> {
30    pub const PERSPECTIVE: Self = Self(Cow::Borrowed("perspective"));
31    pub const ORTHOGRAPHIC: Self = Self(Cow::Borrowed("orthographic"));
32
33    pub fn to_enum(&self) -> Option<CameraTypeEnum> {
34        if *self == Self::PERSPECTIVE {
35            return Some(CameraTypeEnum::Perspective);
36        } else if *self == Self::ORTHOGRAPHIC {
37            return Some(CameraTypeEnum::Orthographic);
38        }
39
40        None
41    }
42}
43
44/// A perspective camera containing properties to create a perspective projection matrix.
45#[derive(Debug, Clone, Deserialize, IntoOwned)]
46pub struct Perspective<'a> {
47    /// The floating-point aspect ratio of the field of view.
48    #[serde(rename = "aspectRatio")]
49    pub aspect_ratio: Option<f32>,
50    /// The floating-point vertical field of view in radians.
51    pub yfov: f32,
52    /// The floating-point distance to the far clipping plane.
53    pub zfar: Option<f32>,
54    /// The floating-point distance to the near clipping plane.
55    pub znear: f32,
56
57    #[serde(borrow)]
58    pub extensions: Option<Extensions<'a>>,
59    #[serde(borrow)]
60    pub extras: Option<Extras<'a>>,
61}
62
63/// An orthographic camera containing properties to create an orthographic projection matrix.
64#[derive(Debug, Clone, Deserialize, IntoOwned)]
65pub struct Orthographic<'a> {
66    /// The floating-point horizontal magnification of the view.
67    pub xmag: f32,
68    /// The floating-point vertical magnification of the view.
69    pub ymag: f32,
70    /// The floating-point distance to the far clipping plane.
71    pub zfar: f32,
72    /// The floating-point distance to the near clipping plane.
73    pub znear: f32,
74
75    #[serde(borrow)]
76    pub extensions: Option<Extensions<'a>>,
77    #[serde(borrow)]
78    pub extras: Option<Extras<'a>>,
79}
80
81/// A camera's projection.
82#[derive(Debug, Clone, Deserialize, IntoOwned)]
83pub struct Camera<'a> {
84    /// The user-defined name of this object.
85    #[serde(borrow)]
86    pub name: Option<Cow<'a, str>>,
87
88    /// Specifies if the camera uses a perspective or orthographic projection.
89    #[serde(borrow)]
90    pub ty: CameraType<'a>,
91    /// A perspective camera containing properties to create a perspective projection matrix.
92    #[serde(borrow)]
93    pub perspective: Option<Perspective<'a>>,
94    /// An orthographic camera containing properties to create an orthographic projection matrix.
95    #[serde(borrow)]
96    pub orthographic: Option<Orthographic<'a>>,
97
98    #[serde(borrow)]
99    pub extensions: Option<Extensions<'a>>,
100    #[serde(borrow)]
101    pub extras: Option<Extras<'a>>,
102}