gltf_v1/
camera.rs

1use json::camera::CameraType;
2
3use crate::Document;
4
5/// A camera's projection.
6#[derive(Clone, Debug)]
7pub enum Projection<'a> {
8    /// Describes an orthographic projection.
9    Orthographic(Orthographic<'a>),
10
11    /// Describes a perspective projection.
12    Perspective(Perspective<'a>),
13}
14
15///  Values for an orthographic camera projection.
16#[derive(Clone, Debug)]
17pub struct Orthographic<'a> {
18    /// The parent `Document` struct.
19    #[allow(dead_code)]
20    document: &'a Document,
21
22    /// The corresponding JSON struct.
23    json: &'a json::camera::Orthographic,
24}
25
26/// Values for a perspective camera projection.
27#[derive(Clone, Debug)]
28pub struct Perspective<'a> {
29    /// The parent `Document` struct.
30    #[allow(dead_code)]
31    document: &'a Document,
32
33    /// The corresponding JSON struct.
34    json: &'a json::camera::Perspective,
35}
36
37#[derive(Clone, Debug)]
38pub struct Camera<'a> {
39    /// The parent `Document` struct.
40    #[allow(dead_code)]
41    document: &'a Document,
42
43    /// The corresponding JSON index.
44    index: &'a String,
45
46    /// The corresponding JSON struct.
47    json: &'a json::Camera,
48}
49
50impl<'a> Camera<'a> {
51    /// Constructs a `Buffer`.
52    pub(crate) fn new(document: &'a Document, index: &'a String, json: &'a json::Camera) -> Self {
53        Self {
54            document,
55            index,
56            json,
57        }
58    }
59
60    /// Returns the internal JSON index.
61    pub fn index(&self) -> &str {
62        self.index
63    }
64    pub fn name(&self) -> Option<&'a str> {
65        self.json.name.as_deref()
66    }
67
68    /// Returns the camera's projection.
69    pub fn projection(&self) -> Projection {
70        match self.json.type_.unwrap() {
71            CameraType::Orthographic => {
72                let json = self.json.orthographic.as_ref().unwrap();
73                Projection::Orthographic(Orthographic::new(self.document, json))
74            }
75            CameraType::Perspective => {
76                let json = self.json.perspective.as_ref().unwrap();
77                Projection::Perspective(Perspective::new(self.document, json))
78            }
79        }
80    }
81}
82impl<'a> Orthographic<'a> {
83    /// Constructs a `Orthographic` camera projection.
84    pub(crate) fn new(document: &'a Document, json: &'a json::camera::Orthographic) -> Self {
85        Self { document, json }
86    }
87
88    ///  The horizontal magnification of the view.
89    pub fn xmag(&self) -> f32 {
90        self.json.xmag
91    }
92
93    ///  The vertical magnification of the view.
94    pub fn ymag(&self) -> f32 {
95        self.json.ymag
96    }
97
98    ///  The distance to the far clipping plane.
99    pub fn zfar(&self) -> f32 {
100        self.json.zfar
101    }
102
103    ///  The distance to the near clipping plane.
104    pub fn znear(&self) -> f32 {
105        self.json.znear
106    }
107}
108
109impl<'a> Perspective<'a> {
110    /// Constructs a `Perspective` camera projection.
111    pub(crate) fn new(document: &'a Document, json: &'a json::camera::Perspective) -> Self {
112        Self { document, json }
113    }
114
115    ///  Aspect ratio of the field of view.
116    pub fn aspect_ratio(&self) -> Option<f32> {
117        self.json.aspect_ratio
118    }
119
120    ///  The vertical field of view in radians.
121    pub fn yfov(&self) -> f32 {
122        self.json.yfov
123    }
124
125    ///  The distance to the far clipping plane.
126    pub fn zfar(&self) -> f32 {
127        self.json.zfar
128    }
129
130    ///  The distance to the near clipping plane.
131    pub fn znear(&self) -> f32 {
132        self.json.znear
133    }
134}
135
136/// An `Iterator` that visits every buffer in a glTF asset.
137#[derive(Clone, Debug)]
138pub struct Cameras<'a> {
139    /// Internal buffer iterator.
140    pub(crate) iter: indexmap::map::Iter<'a, String, gltf_v1_json::Camera>,
141
142    /// The internal root glTF object.
143    pub(crate) document: &'a Document,
144}
145
146impl ExactSizeIterator for Cameras<'_> {}
147impl<'a> Iterator for Cameras<'a> {
148    type Item = Camera<'a>;
149    fn next(&mut self) -> Option<Self::Item> {
150        self.iter
151            .next()
152            .map(|(index, json)| Camera::new(self.document, index, json))
153    }
154    fn size_hint(&self) -> (usize, Option<usize>) {
155        self.iter.size_hint()
156    }
157    fn count(self) -> usize {
158        self.iter.count()
159    }
160    fn last(self) -> Option<Self::Item> {
161        let document = self.document;
162        self.iter
163            .last()
164            .map(|(index, json)| Camera::new(document, index, json))
165    }
166    fn nth(&mut self, n: usize) -> Option<Self::Item> {
167        self.iter
168            .nth(n)
169            .map(|(index, json)| Camera::new(self.document, index, json))
170    }
171}