mod3d_gltf/
primitives_meshes.rs1#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[cfg(feature = "serde")]
6use crate::{deserialize, serialize};
7
8use crate::{AccessorIndex, Indexable, MaterialIndex, PrimitiveIndex};
9
10#[derive(Debug, Default)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15pub struct GltfPrimitive {
16 #[cfg_attr(
21 feature = "serde",
22 serde(deserialize_with = "deserialize::attr_to_attr")
23 )]
24 #[cfg_attr(feature = "serde", serde(serialize_with = "serialize::attr_to_attr"))]
25 attributes: Vec<(mod3d_base::VertexAttr, AccessorIndex)>,
26
27 #[cfg_attr(feature = "serde", serde(default = "deserialize::pt_triangles"))]
31 #[cfg_attr(
32 feature = "serde",
33 serde(deserialize_with = "deserialize::primitive_type")
34 )]
35 #[cfg_attr(feature = "serde", serde(serialize_with = "serialize::primitive_type"))]
36 mode: mod3d_base::PrimitiveType,
37
38 #[cfg_attr(feature = "serde", serde(default))]
41 material: Option<MaterialIndex>,
42
43 #[cfg_attr(feature = "serde", serde(default))]
46 indices: Option<AccessorIndex>,
47 }
50
51impl GltfPrimitive {
53 pub fn new(
54 mode: mod3d_base::PrimitiveType,
55 indices: Option<AccessorIndex>,
56 material: Option<MaterialIndex>,
57 ) -> Self {
58 Self {
59 mode,
60 indices,
61 material,
62 ..Default::default()
63 }
64 }
65 pub fn indices(&self) -> Option<AccessorIndex> {
70 self.indices
71 }
72
73 pub fn primitive_type(&self) -> mod3d_base::PrimitiveType {
77 self.mode
78 }
79
80 pub fn attributes(&self) -> &[(mod3d_base::VertexAttr, AccessorIndex)] {
84 &self.attributes
85 }
86
87 pub fn material(&self) -> Option<MaterialIndex> {
91 self.material
92 }
93 pub fn add_attribute(&mut self, attr: mod3d_base::VertexAttr, accessor: AccessorIndex) {
94 self.attributes.push((attr, accessor))
95 }
96}
97
98#[derive(Debug, Default)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101#[cfg_attr(feature = "serde", serde(default))]
102pub struct GltfMesh {
103 #[cfg_attr(feature = "serde", serde(default))]
105 name: String,
106 primitives: Vec<GltfPrimitive>,
108 }
111
112impl GltfMesh {
113 pub fn add_primitive(
114 &mut self,
115 mode: mod3d_base::PrimitiveType,
116 indices: Option<AccessorIndex>,
117 material: Option<MaterialIndex>,
118 ) -> PrimitiveIndex {
119 let p = GltfPrimitive::new(mode, indices, material);
120 let n = self.primitives.len();
121 self.primitives.push(p);
122 n.into()
123 }
124 pub fn name(&self) -> &str {
125 &self.name
126 }
127 pub fn primitives(&self) -> &[GltfPrimitive] {
128 &self.primitives
129 }
130}
131
132impl std::ops::Index<PrimitiveIndex> for GltfMesh {
134 type Output = GltfPrimitive;
135 fn index(&self, index: PrimitiveIndex) -> &Self::Output {
136 &self.primitives[index.as_usize()]
137 }
138}
139
140impl std::ops::IndexMut<PrimitiveIndex> for GltfMesh {
142 fn index_mut(&mut self, index: PrimitiveIndex) -> &mut Self::Output {
143 &mut self.primitives[index.as_usize()]
144 }
145}