In bevy(0.10) I am discovering the transvoxel module, and in its current state* won't let the bevy_mesh
feature to be enabled because of some dependency problems; So I'm manually converting its output to a bevy::Mesh
.
One of the tasks this requires is to convert from a Vec to Vec<[f32; 3]>, so that each (xyz) coordinate is in one place.
Since I'm new to Rust I was wondering if there are best practices to achieve this.
So far what I tried is the following:
let mut pos2: Vec<[f32; 3]> = Vec::new(); // A vector to contain VertexFormat::Float32x3
let i = 0;
extracted_mesh.positions.into_iter().map(|u|{
if i % 3 == 0 {
pos2.push([0.,0.,0.]);
}
if let Some(pos3) = pos2.last_mut() {pos3[i % 3] = u};
});
Which I would say is really ugly, but seems to do the job.
I'm interested in if there's a map(...).collect()
way to do this, as this itself produces a warning:
warning: src/meshgen.rs:28: unused `std::iter::Map` that must be used
note: src/meshgen.rs:28: iterators are lazy and do nothing unless consumed
*as of 2023. March
If positions
is a Vec
or something along those lines there is slice::chunks
and slice::chunks_exact
, although they yield slices so you need to try_into
them into fixed-size arrays:
v.chunks(3).map(|c| <[_;3]>::try_from(c).unwrap()).collect::<Vec<_>>()
If you can swing it itertools has the even better Itertools::tuples
, which collects items into the a tuple the exact size you want, you just need to move them over to an array, with no panics to be seen:
v.into_iter().tuples().map(|(a, b, c)| [a, b, c]).collect::<Vec<_>>()