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
use crate::renderer::*;
pub use three_d_asset::Model as CpuModel;
pub struct Model<M: Material>(Vec<Gm<Mesh, M>>);
impl<'a, M: Material> IntoIterator for &'a Model<M> {
type Item = &'a dyn Object;
type IntoIter = std::vec::IntoIter<&'a dyn Object>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
.map(|m| m as &dyn Object)
.collect::<Vec<_>>()
.into_iter()
}
}
impl<M: Material + FromCpuMaterial + Clone + Default> Model<M> {
pub fn new(context: &Context, cpu_model: &CpuModel) -> Result<Self, RendererError> {
let mut materials = std::collections::HashMap::new();
for m in cpu_model.materials.iter() {
materials.insert(m.name.clone(), M::from_cpu_material(context, m));
}
let mut gms = Vec::new();
for g in cpu_model.geometries.iter() {
gms.push(if let Some(material_name) = &g.material_name {
Gm {
geometry: Mesh::new(context, g),
material: materials
.get(material_name)
.ok_or(RendererError::MissingMaterial(
material_name.clone(),
g.name.clone(),
))?
.clone(),
}
} else {
Gm {
geometry: Mesh::new(context, g),
material: M::default(),
}
});
}
Ok(Self(gms))
}
}
impl<M: Material> std::ops::Deref for Model<M> {
type Target = Vec<Gm<Mesh, M>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<M: Material> std::ops::DerefMut for Model<M> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}