1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use flatbox_core::math::transform::Transform;
use serde::{
Serialize,
Deserialize,
Serializer,
Deserializer,
de::*,
de::Error as DeError,
ser::SerializeStruct,
};
use crate::pbr::{
mesh::{MeshType, Mesh},
material::Material,
};
#[derive(Debug, Clone)]
#[readonly::make]
pub struct Model {
/// Model mesh type. It can be selected manually and is
/// readonly during future use
#[readonly]
pub mesh_type: MeshType,
pub mesh: Option<Mesh>,
}
impl Model {
pub fn new(mesh_type: MeshType, mesh: Mesh) -> Model {
Model {
mesh_type,
mesh: Some(mesh),
}
}
pub fn cube() -> Model {
Model {
mesh_type: MeshType::Cube,
mesh: Some(Mesh::cube()),
}
}
pub fn plane() -> Model {
Model {
mesh_type: MeshType::Plane,
mesh: Some(Mesh::plane()),
}
}
}
impl Default for Model {
fn default() -> Self {
Model {
mesh_type: MeshType::default(),
mesh: Some(Mesh::default()),
}
}
}
impl Serialize for Model {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut model = serializer.serialize_struct("Model", 2)?;
model.serialize_field("mesh_type", &self.mesh_type)?;
match self.mesh_type {
MeshType::Generic => {
model.serialize_field("mesh", &self.mesh)?;
},
_ => {
model.serialize_field("mesh", &Option::<Mesh>::None)?;
}
}
model.end()
}
}
impl<'de> Deserialize<'de> for Model {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>
{
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "snake_case")]
enum ModelField {
MeshType,
Mesh,
}
struct ModelVisitor;
impl<'de> Visitor<'de> for ModelVisitor {
type Value = Model;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("struct Model")
}
fn visit_seq<V>(self, mut seq: V) -> Result<Model, V::Error>
where
V: SeqAccess<'de>,
{
let mesh_type: MeshType = seq.next_element()?.ok_or_else(|| DeError::invalid_length(0, &self))?;
let mesh = match mesh_type {
MeshType::Cube => { Some(Mesh::cube()) },
// MeshType::Icosahedron => { Some(Mesh::icosahedron()) },
// MeshType::Sphere => { Some(Mesh::sphere()) },
// MeshType::Plane => { Some(Mesh::plane()) },
// MeshType::Loaded(path) => {
// return Ok(Model::load_obj(path)
// .expect("Cannot load deserialized model from path"));
// },
MeshType::Generic => {
seq.next_element()?.ok_or_else(|| DeError::invalid_length(1, &self))?
},
_ => todo!("Mesh types: `icosahedron`, `sphere`, `plane` etc."),
};
Ok(Model {
mesh_type,
mesh,
})
}
fn visit_map<V>(self, mut map: V) -> Result<Model, V::Error>
where
V: MapAccess<'de>,
{
let mut mesh_type: Option<MeshType> = None;
let mut mesh: Option<Option<Mesh>> = None;
while let Some(key) = map.next_key()? {
match key {
ModelField::MeshType => {
if mesh_type.is_some() {
return Err(DeError::duplicate_field("mesh_type"));
}
mesh_type = Some(map.next_value()?);
},
ModelField::Mesh => {
if mesh.is_some() {
return Err(DeError::duplicate_field("mesh"));
}
mesh = Some(map.next_value()?);
},
}
}
let mesh_type = mesh_type.ok_or_else(|| DeError::missing_field("mesh_type"))?;
let mesh = match mesh_type {
MeshType::Cube => { Some(Mesh::cube()) },
// MeshType::Icosahedron => { Some(Mesh::icosahedron()) },
// MeshType::Sphere => { Some(Mesh::sphere()) },
// MeshType::Plane => { Some(Mesh::plane()) },
// MeshType::Loaded(path) => {
// return Ok(Model::load_obj(path)
// .expect("Cannot load deserialized model from path"));
// },
MeshType::Generic => {
mesh.ok_or_else(|| DeError::missing_field("mesh"))?
},
_ => todo!("Mesh types: `icosahedron`, `sphere`, `plane` etc."),
};
Ok(Model {
mesh_type,
mesh,
})
}
}
const FIELDS: &[&str] = &[
"mesh_type",
"mesh"
];
deserializer.deserialize_struct("Model", FIELDS, ModelVisitor)
}
}
pub struct ModelBundle<M: Material> {
pub model: Model,
pub material: M,
pub transform: Transform,
}