blender_mesh/vertex_attributes/single_indexed.rs
1mod interleave;
2
3pub use self::interleave::*;
4
5/// Most 3D model file formats export vertex data with multiple indices.
6///
7/// There might be indices for the positions, normals and uvs.
8///
9/// The `SingleIndexVertexData` is vertex data that only has one index.
10///
11/// When we've run [`BlenderMesh.combine_vertex_indices`] we'll end up generating
12/// `SingleIndexVertexData`
13///
14/// [`BlenderMesh.combine_vertex_indices`]: ../struct.BlenderMesh.html#method.combine_vertex_indices
15#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
16pub struct SingleIndexedVertexAttributes {
17 pub(crate) indices: Vec<u16>,
18 pub(crate) vertices: Vec<Vertex>,
19}
20
21/// A vertex within a mesh.
22///
23/// You'll typically buffer the Vertex's data onto the GPU interleaved into a single buffer, and
24/// then index into that buffer using the indices from [`SingleIndexedVertexAttributes`].
25#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Default)]
26pub struct Vertex {
27 pub(crate) position: [f32; 3],
28 pub(crate) normal: Option<[f32; 3]>,
29 pub(crate) face_tangent: Option<[f32; 3]>,
30 pub(crate) uv: Option<[f32; 2]>,
31 pub(crate) bones: Option<[BoneInfluence; 4]>,
32}
33
34impl Vertex {
35 /// The model space position of this Vertex
36 pub fn position(&self) -> [f32; 3] {
37 self.position
38 }
39
40 /// The surface normal for the face that this Vertex belongs to
41 pub fn normal(&self) -> Option<[f32; 3]> {
42 self.normal
43 }
44
45 /// The face tangent for the face that this Vertex belongs to
46 pub fn face_tangent(&self) -> Option<[f32; 3]> {
47 self.face_tangent
48 }
49
50 /// The UV coordinates for this Vertex
51 pub fn uv(&self) -> Option<[f32; 2]> {
52 self.uv
53 }
54
55 /// The bones that influence this Vertex.
56 ///
57 /// Currently a maximum of 4 bones is supported for no other reason than it being uncommon to
58 /// need more than that.
59 ///
60 /// If this doesn't meet your needs pleas open an issue.
61 ///
62 /// If there are fewer than 4 influencing bones then the extra fake bones in this array will
63 /// have weights of zero.
64 pub fn bones(&self) -> Option<[BoneInfluence; 4]> {
65 self.bones
66 }
67}
68
69/// The index of a bone that influences the vertex along with the weighting of that influence
70#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
71pub struct BoneInfluence {
72 pub(crate) bone_idx: u8,
73 pub(crate) weight: f32,
74}
75
76impl BoneInfluence {
77 /// The index of this bone within the mesh's parent armature's bones.
78 pub fn bone_idx(&self) -> u8 {
79 self.bone_idx
80 }
81
82 /// The amount between \[0.0, 1.0\] that this bone should influence the Vertex
83 pub fn weight(&self) -> f32 {
84 self.weight
85 }
86}
87
88impl SingleIndexedVertexAttributes {
89 /// For `SingleIndexVertexData` every 3 indices corresponds to one triangle.
90 ///
91 /// There can not be any other faces (quads, ngons) - only triangles.
92 pub fn indices(&self) -> &Vec<u16> {
93 &self.indices
94 }
95
96 /// All of the vertex data for the mesh.
97 ///
98 /// You can index into this data using [`SingleIndexedVertexAttributes#method.indices`]
99 pub fn vertices(&self) -> &Vec<Vertex> {
100 &self.vertices
101 }
102
103 pub(crate) fn vertices_mut(&mut self) -> &mut Vec<Vertex> {
104 &mut self.vertices
105 }
106}