mesh_graph/integrations/
mod.rs

1use glam::Vec3;
2use slotmap::SecondaryMap;
3
4use crate::MeshGraph;
5
6#[cfg(feature = "bevy")]
7pub mod bevy;
8
9/// Classical indexed mesh representation
10#[derive(Clone, Debug)]
11pub struct VertexIndexBuffers {
12    /// Vertex positions, one per vertex.
13    pub positions: Vec<Vec3>,
14    /// Vertex normals, one per vertex.
15    pub normals: Vec<Vec3>,
16    /// Indices: 3*N where N is the number of triangles. Indices point to
17    /// elements of `positions` and `normals`.
18    pub indices: Vec<u32>,
19}
20
21impl From<MeshGraph> for VertexIndexBuffers {
22    fn from(mut mesh_graph: MeshGraph) -> VertexIndexBuffers {
23        if mesh_graph.vertex_normals.is_none() {
24            mesh_graph.compute_vertex_normals();
25        }
26        let vertex_normals = mesh_graph.vertex_normals.as_ref().unwrap();
27
28        let mut vertex_id_to_index = SecondaryMap::default();
29        let mut positions = vec![];
30        let mut normals = vec![];
31        let mut indices = vec![];
32
33        for (vertex_id, pos) in &mesh_graph.positions {
34            vertex_id_to_index.insert(vertex_id, positions.len() as u32);
35            positions.push(*pos);
36            normals.push(vertex_normals[vertex_id]);
37        }
38
39        for face in mesh_graph.faces.values() {
40            for vertex in face.vertices(&mesh_graph) {
41                let index = vertex_id_to_index[vertex];
42                indices.push(index);
43            }
44        }
45
46        VertexIndexBuffers {
47            indices,
48            positions,
49            normals,
50        }
51    }
52}