gltf_v1/
texture.rs

1use json::texture::{
2    SamplerMagFilter, SamplerMinFilter, SamplerWrap, TextureFormat, TextureTarget, TextureType,
3};
4
5use crate::{document::Document, image::Image};
6
7#[derive(Clone, Debug)]
8pub struct Sampler<'a> {
9    #[allow(dead_code)]
10    document: &'a Document,
11
12    index: &'a String,
13
14    json: &'a json::texture::Sampler,
15}
16
17#[derive(Clone, Debug)]
18pub struct Texture<'a> {
19    #[allow(dead_code)]
20    document: &'a Document,
21
22    index: &'a String,
23
24    json: &'a json::texture::Texture,
25
26    sampler: Sampler<'a>,
27
28    source: Image<'a>,
29}
30
31impl<'a> Sampler<'a> {
32    /// Constructs a `Sampler`.
33    pub(crate) fn new(
34        document: &'a Document,
35        index: &'a String,
36        json: &'a json::texture::Sampler,
37    ) -> Self {
38        Self {
39            document,
40            index,
41            json,
42        }
43    }
44    pub fn index(&self) -> &str {
45        self.index
46    }
47    pub fn name(&self) -> Option<&'a str> {
48        self.json.name.as_deref()
49    }
50    pub fn wrap_s(&self) -> SamplerWrap {
51        self.json.wrap_s.unwrap()
52    }
53    pub fn wrap_t(&self) -> SamplerWrap {
54        self.json.wrap_t.unwrap()
55    }
56    pub fn mag_filter(&self) -> SamplerMagFilter {
57        self.json.mag_filter.unwrap()
58    }
59    pub fn min_filter(&self) -> SamplerMinFilter {
60        self.json.min_filter.unwrap()
61    }
62}
63
64impl<'a> Texture<'a> {
65    /// Constructs a `Texture`.
66    pub(crate) fn new(
67        document: &'a Document,
68        index: &'a String,
69        json: &'a json::texture::Texture,
70    ) -> Self {
71        let source = document
72            .images()
73            .find(|x| x.index() == json.source.value())
74            .unwrap();
75        let sampler = document
76            .samplers()
77            .find(|x| x.index() == json.sampler.value())
78            .unwrap();
79        Self {
80            document,
81            index,
82            json,
83            sampler,
84            source,
85        }
86    }
87    pub fn index(&self) -> &str {
88        self.index
89    }
90    pub fn name(&self) -> Option<&'a str> {
91        self.json.name.as_deref()
92    }
93    pub fn source(&self) -> &Image<'a> {
94        &self.source
95    }
96    pub fn sampler(&self) -> &Sampler<'a> {
97        &self.sampler
98    }
99    pub fn format(&self) -> TextureFormat {
100        self.json.format.unwrap()
101    }
102    pub fn internal_format(&self) -> TextureFormat {
103        self.json.internal_format.unwrap()
104    }
105    pub fn target(&self) -> TextureTarget {
106        self.json.target.unwrap()
107    }
108    pub fn texture_type(&self) -> TextureType {
109        self.json.type_.unwrap()
110    }
111}
112
113/// An `Iterator` that visits every accessor in a glTF asset.
114#[derive(Clone, Debug)]
115pub struct Textures<'a> {
116    /// Internal accessor iterator.
117    pub(crate) iter: indexmap::map::Iter<'a, String, gltf_v1_json::Texture>,
118
119    /// The internal root glTF object.
120    pub(crate) document: &'a Document,
121}
122
123impl ExactSizeIterator for Textures<'_> {}
124impl<'a> Iterator for Textures<'a> {
125    type Item = Texture<'a>;
126
127    fn next(&mut self) -> Option<Self::Item> {
128        self.iter
129            .next()
130            .map(|(index, json)| Texture::new(self.document, index, json))
131    }
132    fn size_hint(&self) -> (usize, Option<usize>) {
133        self.iter.size_hint()
134    }
135    fn count(self) -> usize {
136        self.iter.count()
137    }
138    fn last(self) -> Option<Self::Item> {
139        let document = self.document;
140        self.iter
141            .last()
142            .map(|(index, json)| Texture::new(document, index, json))
143    }
144    fn nth(&mut self, n: usize) -> Option<Self::Item> {
145        self.iter
146            .nth(n)
147            .map(|(index, json)| Texture::new(self.document, index, json))
148    }
149}
150
151#[derive(Clone, Debug)]
152pub struct Samplers<'a> {
153    /// Internal accessor iterator.
154    pub(crate) iter: indexmap::map::Iter<'a, String, gltf_v1_json::Sampler>,
155
156    /// The internal root glTF object.
157    pub(crate) document: &'a Document,
158}
159
160impl ExactSizeIterator for Samplers<'_> {}
161impl<'a> Iterator for Samplers<'a> {
162    type Item = Sampler<'a>;
163
164    fn next(&mut self) -> Option<Self::Item> {
165        self.iter
166            .next()
167            .map(|(index, json)| Sampler::new(self.document, index, json))
168    }
169    fn size_hint(&self) -> (usize, Option<usize>) {
170        self.iter.size_hint()
171    }
172    fn count(self) -> usize {
173        self.iter.count()
174    }
175    fn last(self) -> Option<Self::Item> {
176        let document = self.document;
177        self.iter
178            .last()
179            .map(|(index, json)| Sampler::new(document, index, json))
180    }
181    fn nth(&mut self, n: usize) -> Option<Self::Item> {
182        self.iter
183            .nth(n)
184            .map(|(index, json)| Sampler::new(self.document, index, json))
185    }
186}