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
use crate::renderer::*;

///
/// Similar to [Model], except it is possible to render many instances of the same model efficiently.
///
pub struct InstancedModel<M: Material>(Vec<Gm<InstancedMesh, M>>);

impl<M: Material> InstancedModel<M> {
    ///
    /// Creates a new instanced 3D model with a triangle mesh as geometry and the given material.
    /// The model is rendered in as many instances as there are data in the [Instances] given as input.
    ///
    #[deprecated = "Use Gm::new(InstancedMesh::new(&context, &instances, &cpu_mesh)?, material);"]
    pub fn new_with_material(
        context: &Context,
        instances: &Instances,
        cpu_mesh: &CpuMesh,
        material: M,
    ) -> ThreeDResult<Gm<InstancedMesh, M>> {
        Ok(Gm {
            geometry: InstancedMesh::new(context, instances, cpu_mesh)?,
            material,
        })
    }

    ///
    /// Returns a list of references to the objects in this instanced model which can be used as input to a render function, for example [render_pass].
    ///
    pub fn to_objects(&self) -> Vec<&dyn Object> {
        self.0.iter().map(|m| m as &dyn Object).collect::<Vec<_>>()
    }

    ///
    /// Returns a list of references to the geometries in this instanced which can be used as input to for example [pick] or [DirectionalLight::generate_shadow_map].
    ///
    pub fn to_geometries(&self) -> Vec<&dyn Geometry> {
        self.0
            .iter()
            .map(|m| m as &dyn Geometry)
            .collect::<Vec<_>>()
    }
}

impl<T: Material + FromCpuMaterial + Clone + Default> InstancedModel<T> {
    ///
    /// Constructs an [InstancedModel] from a [CpuModel] and the given [Instances] attributes, ie. constructs a list of [Gm]s with a [InstancedMesh] as geometry (constructed from the [CpuMesh]es in the [CpuModel]) and
    /// a [material] type specified by the generic parameter which implement [FromCpuMaterial] (constructed from the [CpuMaterial]s in the [CpuModel]).
    ///
    pub fn new(
        context: &Context,
        instances: &Instances,
        cpu_model: &CpuModel,
    ) -> ThreeDResult<Self> {
        let mut materials = std::collections::HashMap::new();
        for m in cpu_model.materials.iter() {
            materials.insert(m.name.clone(), T::from_cpu_material(context, m)?);
        }
        let mut gms: Vec<Gm<InstancedMesh, T>> = Vec::new();
        for g in cpu_model.geometries.iter() {
            gms.push(if let Some(material_name) = &g.material_name {
                Gm {
                    geometry: InstancedMesh::new(context, instances, g)?,
                    material: materials
                        .get(material_name)
                        .ok_or(CoreError::MissingMaterial(
                            material_name.clone(),
                            g.name.clone(),
                        ))?
                        .clone(),
                }
            } else {
                Gm {
                    geometry: InstancedMesh::new(context, instances, g)?,
                    material: T::default(),
                }
            });
        }
        Ok(Self(gms))
    }
}

impl<M: Material> std::ops::Deref for InstancedModel<M> {
    type Target = Vec<Gm<InstancedMesh, M>>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<M: Material> std::ops::DerefMut for InstancedModel<M> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}