Skip to main content

gltf_reader/
accessor.rs

1use alloc::borrow::Cow;
2use alloc::vec::Vec;
3use ownable::IntoOwned;
4use serde::Deserialize;
5use serde_json::Number;
6
7use crate::buffer::BufferView;
8use crate::{Extensions, Extras, Idx};
9
10/// glTF known data types of an accessor's components.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ComponentTypeEnum {
13    I8,
14    U8,
15    I16,
16    U16,
17    U32,
18    F32,
19}
20
21/// Data type of an accessor's components.
22#[derive(Clone, Copy, PartialEq, Eq, Deserialize, IntoOwned)]
23#[serde(transparent)]
24pub struct ComponentType(pub u64);
25
26impl core::fmt::Debug for ComponentType {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        if let Some(e) = self.to_enum() {
29            e.fmt(f)
30        } else {
31            self.0.fmt(f)
32        }
33    }
34}
35
36impl ComponentType {
37    pub const I8: Self = Self(5120);
38    pub const U8: Self = Self(5121);
39    pub const I16: Self = Self(5122);
40    pub const U16: Self = Self(5123);
41    pub const U32: Self = Self(5125);
42    pub const F32: Self = Self(5126);
43
44    pub fn to_enum(self) -> Option<ComponentTypeEnum> {
45        Some(match self {
46            Self::I8 => ComponentTypeEnum::I8,
47            Self::U8 => ComponentTypeEnum::U8,
48            Self::I16 => ComponentTypeEnum::I16,
49            Self::U16 => ComponentTypeEnum::U16,
50            Self::U32 => ComponentTypeEnum::U32,
51            Self::F32 => ComponentTypeEnum::F32,
52            _ => return None,
53        })
54    }
55}
56
57/// glTF known data types of an accessor's elements.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum ElementTypeEnum {
60    Scalar,
61    Vec2,
62    Vec3,
63    Vec4,
64    Mat2,
65    Mat3,
66    Mat4,
67}
68
69/// Data type of an accessor's elements.
70#[derive(Clone, PartialEq, Eq, Deserialize, IntoOwned)]
71#[serde(transparent)]
72pub struct ElementType<'a>(#[serde(borrow)] pub Cow<'a, str>);
73
74impl core::fmt::Debug for ElementType<'_> {
75    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76        if let Some(e) = self.to_enum() {
77            e.fmt(f)
78        } else {
79            self.0.fmt(f)
80        }
81    }
82}
83
84impl ElementType<'_> {
85    pub const SCALAR: Self = Self(Cow::Borrowed("SCALAR"));
86    pub const VEC2: Self = Self(Cow::Borrowed("VEC2"));
87    pub const VEC3: Self = Self(Cow::Borrowed("VEC3"));
88    pub const VEC4: Self = Self(Cow::Borrowed("VEC4"));
89    pub const MAT2: Self = Self(Cow::Borrowed("MAT2"));
90    pub const MAT3: Self = Self(Cow::Borrowed("MAT3"));
91    pub const MAT4: Self = Self(Cow::Borrowed("MAT4"));
92
93    pub fn to_enum(&self) -> Option<ElementTypeEnum> {
94        if *self == Self::SCALAR {
95            return Some(ElementTypeEnum::Scalar);
96        } else if *self == Self::VEC2 {
97            return Some(ElementTypeEnum::Vec2);
98        } else if *self == Self::VEC3 {
99            return Some(ElementTypeEnum::Vec3);
100        } else if *self == Self::VEC4 {
101            return Some(ElementTypeEnum::Vec4);
102        } else if *self == Self::MAT2 {
103            return Some(ElementTypeEnum::Mat2);
104        } else if *self == Self::MAT3 {
105            return Some(ElementTypeEnum::Mat3);
106        } else if *self == Self::MAT4 {
107            return Some(ElementTypeEnum::Mat4);
108        }
109
110        None
111    }
112}
113
114/// Sparse storage of accessor values that deviate from their initialization value.
115#[derive(Debug, Clone, Deserialize, IntoOwned)]
116pub struct SparseIndices<'a> {
117    /// The index of the buffer view with sparse indices.
118    #[serde(rename = "bufferView")]
119    pub buffer_view: Idx<BufferView<'static>>,
120    /// The offset relative to the start of the buffer view in bytes.
121    #[serde(rename = "byteOffset")]
122    #[serde(default)]
123    pub byte_offset: u64,
124    /// The indices data type.
125    #[serde(rename = "componentType")]
126    pub component_type: ComponentType,
127
128    #[serde(borrow)]
129    pub extensions: Option<Extensions<'a>>,
130    #[serde(borrow)]
131    pub extras: Option<Extras<'a>>,
132}
133
134/// An object pointing to a buffer view containing the deviating accessor values.
135#[derive(Debug, Clone, Deserialize, IntoOwned)]
136pub struct SparseValues<'a> {
137    /// The index of the buffer view with sparse indices.
138    #[serde(rename = "bufferView")]
139    pub buffer_view: Idx<BufferView<'static>>,
140    /// The offset relative to the start of the buffer view in bytes.
141    #[serde(rename = "byteOffset")]
142    #[serde(default)]
143    pub byte_offset: u64,
144
145    #[serde(borrow)]
146    pub extensions: Option<Extensions<'a>>,
147    #[serde(borrow)]
148    pub extras: Option<Extras<'a>>,
149}
150
151/// Sparse storage of accessor values that deviate from their initialization value.
152#[derive(Debug, Clone, Deserialize, IntoOwned)]
153pub struct Sparse<'a> {
154    /// Number of deviating accessor values stored in the sparse array.
155    pub count: u64,
156    /// An object pointing to a buffer view containing the indices of deviating accessor values.
157    pub indices: SparseIndices<'a>,
158    /// An object pointing to a buffer view containing the deviating accessor values.
159    pub values: SparseValues<'a>,
160
161    #[serde(borrow)]
162    pub extensions: Option<Extensions<'a>>,
163    #[serde(borrow)]
164    pub extras: Option<Extras<'a>>,
165}
166
167/// A typed view into a buffer view that contains raw binary data.
168#[derive(Debug, Clone, Deserialize, IntoOwned)]
169pub struct Accessor<'a> {
170    /// The user-defined name of this object.
171    #[serde(borrow)]
172    pub name: Option<Cow<'a, str>>,
173
174    /// The index of the buffer view.
175    #[serde(rename = "bufferView")]
176    pub buffer_view: Idx<BufferView<'static>>,
177    /// The offset relative to the start of the buffer view in bytes.
178    #[serde(rename = "byteOffset")]
179    #[serde(default)]
180    pub byte_offset: u64,
181
182    /// Specifies if the accessor's elements are scalars, vectors, or matrices.
183    #[serde(rename = "type")]
184    #[serde(borrow)]
185    pub ty: ElementType<'a>,
186    /// The data type of the accessor's components.
187    #[serde(rename = "componentType")]
188    pub component_type: ComponentType,
189    /// Specifies whether integer data values are normalized to [0, 1] (for unsigned types) or to
190    /// [-1, 1] (for signed types) when they are accessed.
191    #[serde(default)]
192    pub normalized: bool,
193    /// The number of elements referenced by this accessor.
194    pub count: u64,
195    /// Maximum value of each component in this accessor.
196    #[ownable(clone)]
197    pub max: Option<Vec<Number>>,
198    /// Minimum value of each component in this accessor.
199    #[ownable(clone)]
200    pub min: Option<Vec<Number>>,
201
202    /// Sparse storage of elements that deviate from their initialization value.
203    #[serde(borrow)]
204    pub sparse: Option<Sparse<'a>>,
205
206    #[serde(borrow)]
207    pub extensions: Option<Extensions<'a>>,
208    #[serde(borrow)]
209    pub extras: Option<Extras<'a>>,
210}