mod3d_base/
instance.rs

1//a Imports
2use crate::{Instantiable, Mat4, Renderable, SkeletonPose, Transformation};
3
4//a Instance
5//tp Instance
6/// A drawable::Instance contains the instance data for an instance of
7/// a drawable::Instantiable
8///
9/// It requires a base transformation, an array of BonePose (which
10/// matches the Instantiable's BoneSet array), and an array of Mat4
11/// for each bone in the BonePose array.
12pub struct Instance<'a, R>
13where
14    R: Renderable,
15{
16    /// Reference to the [Instantiable] this is based on
17    ///
18    /// This is provided as the instance *depends* on the
19    /// [Instantiable] although it does not use the data here
20    ///
21    /// The [Skeleton] of the [Instantiable] *is* borrowed by the
22    /// [SkeletonPose]
23    pub instantiable: &'a Instantiable<R>,
24    /// The transformation to apply to this model instance
25    pub transformation: Transformation,
26    /// Matrix for the transformation (must be updated after updating Transformation),
27    pub trans_mat: Mat4,
28    /// The sets of BonePose corresponding to the BoneSet array in the Instantiable
29    pub bone_poses: Vec<SkeletonPose<'a>>,
30    /// Transformation matrices for the bones
31    pub bone_matrices: Vec<Mat4>,
32}
33
34impl<'a, R> Instance<'a, R>
35where
36    R: Renderable,
37{
38    //fp new
39    /// Create a new [Instance] from an [Instantiable]
40    ///
41    /// This contains an array of [SkeletonPose]s to allow elements of
42    /// the [Instantiable] to be posed, and respective matrices for
43    /// drawing the meshes within the [Instantiable]
44    ///
45    /// It should contain appropriate Materials too
46    pub fn new(instantiable: &'a Instantiable<R>, num_bone_matrices: usize) -> Self {
47        let transformation = Transformation::new();
48        let trans_mat = [0.; 16];
49        let bone_poses = Vec::new();
50        let mut bone_matrices = Vec::with_capacity(num_bone_matrices);
51        for _ in 0..num_bone_matrices {
52            bone_matrices.push([0.; 16]);
53        }
54        Self {
55            instantiable,
56            transformation,
57            trans_mat,
58            bone_poses,
59            bone_matrices,
60        }
61    }
62}