use crate::mesher::MesherOutput;
#[derive(Debug)]
pub struct RawMeshData {
pub positions: Vec<[f32; 3]>,
pub normals: Vec<[f32; 3]>,
pub uvs: Vec<[f32; 2]>,
pub colors: Vec<[f32; 4]>,
pub indices: Vec<u32>,
pub texture_rgba: Vec<u8>,
pub texture_width: u32,
pub texture_height: u32,
}
pub fn export_raw(output: &MesherOutput) -> RawMeshData {
let mesh = output.mesh();
let atlas = &output.atlas;
RawMeshData {
positions: mesh.vertices.iter().map(|v| v.position).collect(),
normals: mesh.vertices.iter().map(|v| v.normal).collect(),
uvs: mesh.vertices.iter().map(|v| v.uv).collect(),
colors: mesh.vertices.iter().map(|v| v.color).collect(),
indices: mesh.indices.clone(),
texture_rgba: atlas.pixels.clone(),
texture_width: atlas.width,
texture_height: atlas.height,
}
}
impl RawMeshData {
pub fn positions_flat(&self) -> Vec<f32> {
self.positions.iter().flat_map(|p| p.iter().copied()).collect()
}
pub fn normals_flat(&self) -> Vec<f32> {
self.normals.iter().flat_map(|n| n.iter().copied()).collect()
}
pub fn uvs_flat(&self) -> Vec<f32> {
self.uvs.iter().flat_map(|uv| uv.iter().copied()).collect()
}
pub fn colors_flat(&self) -> Vec<f32> {
self.colors.iter().flat_map(|c| c.iter().copied()).collect()
}
pub fn vertex_count(&self) -> usize {
self.positions.len()
}
pub fn triangle_count(&self) -> usize {
self.indices.len() / 3
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atlas::TextureAtlas;
use crate::mesher::geometry::{Mesh, Vertex};
use crate::types::BoundingBox;
#[test]
fn test_export_raw() {
let mut mesh = Mesh::new();
mesh.add_vertex(Vertex::new([0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0]));
mesh.add_vertex(Vertex::new([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 0.0]));
mesh.add_vertex(Vertex::new([0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0]));
mesh.add_triangle(0, 1, 2);
let output = MesherOutput {
opaque_mesh: mesh,
transparent_mesh: Mesh::new(),
atlas: TextureAtlas::empty(),
bounds: BoundingBox::new([0.0, 0.0, 0.0], [1.0, 0.0, 1.0]),
};
let raw = export_raw(&output);
assert_eq!(raw.vertex_count(), 3);
assert_eq!(raw.triangle_count(), 1);
assert_eq!(raw.positions.len(), 3);
assert_eq!(raw.indices.len(), 3);
}
}