three_d/renderer/object/
gm.rs

1use crate::renderer::*;
2
3///
4/// A combination of a [Geometry] and a [Material] which implements [Object].
5/// Use this to combine any [geometry] and [material] into an object that can be used in a render function for example [RenderTarget::render].
6/// The only requirement is that the geometry provides all the per vertex information (normals, uv coordinates, etc.) that the material requires.
7///
8pub struct Gm<G: Geometry, M: Material> {
9    /// The geometry
10    pub geometry: G,
11    /// The material applied to the geometry
12    pub material: M,
13}
14
15impl<G: Geometry, M: Material> Gm<G, M> {
16    ///
17    /// Creates a new [Gm] from a geometry and material.
18    ///
19    pub fn new(geometry: G, material: M) -> Self {
20        Self { geometry, material }
21    }
22}
23
24impl<'a, G: Geometry, M: Material> IntoIterator for &'a Gm<G, M> {
25    type Item = &'a dyn Object;
26    type IntoIter = std::iter::Once<&'a dyn Object>;
27
28    fn into_iter(self) -> Self::IntoIter {
29        std::iter::once(self)
30    }
31}
32
33impl<G: Geometry + Clone, M: Material + Clone> Clone for Gm<G, M> {
34    fn clone(&self) -> Self {
35        Self {
36            geometry: self.geometry.clone(),
37            material: self.material.clone(),
38        }
39    }
40}
41
42use std::ops::Deref;
43impl<G: Geometry, M: Material> std::ops::Deref for Gm<G, M> {
44    type Target = G;
45    fn deref(&self) -> &Self::Target {
46        &self.geometry
47    }
48}
49
50impl<G: Geometry, M: Material> std::ops::DerefMut for Gm<G, M> {
51    fn deref_mut(&mut self) -> &mut Self::Target {
52        &mut self.geometry
53    }
54}
55
56impl<G: Geometry, M: Material> Geometry for Gm<G, M> {
57    impl_geometry_body!(deref);
58
59    fn animate(&mut self, time: f32) {
60        self.geometry.animate(time)
61    }
62}
63
64impl<G: Geometry, M: Material> Object for Gm<G, M> {
65    fn render(&self, viewer: &dyn Viewer, lights: &[&dyn Light]) {
66        self.render_with_material(&self.material, viewer, lights)
67    }
68
69    fn material_type(&self) -> MaterialType {
70        self.material.material_type()
71    }
72}