schematic_mesher/export/
raw.rs1use crate::mesher::MesherOutput;
4
5#[derive(Debug)]
7pub struct RawMeshData {
8 pub positions: Vec<[f32; 3]>,
10 pub normals: Vec<[f32; 3]>,
12 pub uvs: Vec<[f32; 2]>,
14 pub colors: Vec<[f32; 4]>,
16 pub indices: Vec<u32>,
18 pub texture_rgba: Vec<u8>,
20 pub texture_width: u32,
22 pub texture_height: u32,
24}
25
26pub fn export_raw(output: &MesherOutput) -> RawMeshData {
30 let mesh = output.mesh();
32 let atlas = &output.atlas;
33
34 RawMeshData {
35 positions: mesh.vertices.iter().map(|v| v.position).collect(),
36 normals: mesh.vertices.iter().map(|v| v.normal).collect(),
37 uvs: mesh.vertices.iter().map(|v| v.uv).collect(),
38 colors: mesh.vertices.iter().map(|v| v.color).collect(),
39 indices: mesh.indices.clone(),
40 texture_rgba: atlas.pixels.clone(),
41 texture_width: atlas.width,
42 texture_height: atlas.height,
43 }
44}
45
46impl RawMeshData {
47 pub fn positions_flat(&self) -> Vec<f32> {
49 self.positions.iter().flat_map(|p| p.iter().copied()).collect()
50 }
51
52 pub fn normals_flat(&self) -> Vec<f32> {
54 self.normals.iter().flat_map(|n| n.iter().copied()).collect()
55 }
56
57 pub fn uvs_flat(&self) -> Vec<f32> {
59 self.uvs.iter().flat_map(|uv| uv.iter().copied()).collect()
60 }
61
62 pub fn colors_flat(&self) -> Vec<f32> {
64 self.colors.iter().flat_map(|c| c.iter().copied()).collect()
65 }
66
67 pub fn vertex_count(&self) -> usize {
69 self.positions.len()
70 }
71
72 pub fn triangle_count(&self) -> usize {
74 self.indices.len() / 3
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81 use crate::atlas::TextureAtlas;
82 use crate::mesher::geometry::{Mesh, Vertex};
83 use crate::types::BoundingBox;
84
85 #[test]
86 fn test_export_raw() {
87 let mut mesh = Mesh::new();
88 mesh.add_vertex(Vertex::new([0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0]));
89 mesh.add_vertex(Vertex::new([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 0.0]));
90 mesh.add_vertex(Vertex::new([0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0]));
91 mesh.add_triangle(0, 1, 2);
92
93 let output = MesherOutput {
94 opaque_mesh: mesh,
95 transparent_mesh: Mesh::new(),
96 atlas: TextureAtlas::empty(),
97 bounds: BoundingBox::new([0.0, 0.0, 0.0], [1.0, 0.0, 1.0]),
98 };
99
100 let raw = export_raw(&output);
101
102 assert_eq!(raw.vertex_count(), 3);
103 assert_eq!(raw.triangle_count(), 1);
104 assert_eq!(raw.positions.len(), 3);
105 assert_eq!(raw.indices.len(), 3);
106 }
107}