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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ComponentTypeEnum {
13 I8,
14 U8,
15 I16,
16 U16,
17 U32,
18 F32,
19}
20
21#[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#[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#[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#[derive(Debug, Clone, Deserialize, IntoOwned)]
116pub struct SparseIndices<'a> {
117 #[serde(rename = "bufferView")]
119 pub buffer_view: Idx<BufferView<'static>>,
120 #[serde(rename = "byteOffset")]
122 #[serde(default)]
123 pub byte_offset: u64,
124 #[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#[derive(Debug, Clone, Deserialize, IntoOwned)]
136pub struct SparseValues<'a> {
137 #[serde(rename = "bufferView")]
139 pub buffer_view: Idx<BufferView<'static>>,
140 #[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#[derive(Debug, Clone, Deserialize, IntoOwned)]
153pub struct Sparse<'a> {
154 pub count: u64,
156 pub indices: SparseIndices<'a>,
158 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#[derive(Debug, Clone, Deserialize, IntoOwned)]
169pub struct Accessor<'a> {
170 #[serde(borrow)]
172 pub name: Option<Cow<'a, str>>,
173
174 #[serde(rename = "bufferView")]
176 pub buffer_view: Idx<BufferView<'static>>,
177 #[serde(rename = "byteOffset")]
179 #[serde(default)]
180 pub byte_offset: u64,
181
182 #[serde(rename = "type")]
184 #[serde(borrow)]
185 pub ty: ElementType<'a>,
186 #[serde(rename = "componentType")]
188 pub component_type: ComponentType,
189 #[serde(default)]
192 pub normalized: bool,
193 pub count: u64,
195 #[ownable(clone)]
197 pub max: Option<Vec<Number>>,
198 #[ownable(clone)]
200 pub min: Option<Vec<Number>>,
201
202 #[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}