load mesh faster
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::time::SystemTime;
|
||||
use std::{collections::HashMap, io::Read, time::SystemTime};
|
||||
|
||||
use gltf::{Document, Error};
|
||||
use gltf::{Document, Error, buffer::Source};
|
||||
use gltf::mesh::util::{ReadJoints, ReadNormals, ReadPositions, ReadTangents, ReadTexCoords, ReadWeights};
|
||||
|
||||
use crate::vulkan::mesh::LoadError::{GltfError, MeshDataMissing, NoIndices};
|
||||
@@ -34,6 +34,13 @@ pub struct CPUMesh {
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
fn read_file(path: &str) -> Vec<u8> {
|
||||
let mut glb_file = std::fs::File::open(path).unwrap();
|
||||
let mut glb_bytes = vec![];
|
||||
glb_file.read_to_end(&mut glb_bytes).unwrap();
|
||||
glb_bytes
|
||||
}
|
||||
|
||||
pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>, Document), LoadError> {
|
||||
let mut start_time = None;
|
||||
let mut total_vertices = 0;
|
||||
@@ -41,20 +48,30 @@ pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>, D
|
||||
|
||||
if print_status {
|
||||
start_time = Some(SystemTime::now());
|
||||
println!("Loading mesh file {}", mesh_path);
|
||||
}
|
||||
let (document, buffers, _textures) = gltf::import(mesh_path)?;
|
||||
let mut meshes = vec![];
|
||||
let document = gltf::Gltf::open(mesh_path)?.document;
|
||||
|
||||
if print_status { println!("Mesh file loaded after {} seconds, processing...", start_time.unwrap().elapsed().unwrap().as_secs()); }
|
||||
let mut meshes = vec![];
|
||||
let mut mesh_data: HashMap<&str, Vec<u8>> = HashMap::new();
|
||||
|
||||
for b in document.buffers() {
|
||||
if let Source::Uri(uri) = b.source() {
|
||||
mesh_data.insert(uri, read_file(&format!("models/{}", uri)));
|
||||
}
|
||||
}
|
||||
|
||||
for mesh in document.meshes() {
|
||||
for primitive in mesh.primitives() {
|
||||
let texture_index = primitive.material().pbr_metallic_roughness().base_color_texture().map(|tex_info| tex_info.texture().index());
|
||||
let normal_map_index = primitive.material().normal_texture().map(|tex_info| tex_info.texture().index());
|
||||
|
||||
|
||||
let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()]));
|
||||
let reader = primitive.reader(|buffer| {
|
||||
if let Source::Uri(uri) = buffer.source() {
|
||||
Some(&mesh_data[uri])
|
||||
} else {
|
||||
panic!("Loading embedded gltf binary is not supported!");
|
||||
}
|
||||
});
|
||||
let indices = reader.read_indices().ok_or(NoIndices)?;
|
||||
let vertices_result = create_vertices(
|
||||
reader.read_positions(),
|
||||
@@ -75,12 +92,6 @@ pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>, D
|
||||
let vert_count = cpu_mesh.vertices.len();
|
||||
let index_count = cpu_mesh.indices.len();
|
||||
|
||||
println!("Loaded mesh {} after {} seconds: {} Vertices, {} Indices",
|
||||
mesh.name().unwrap_or(""),
|
||||
start_time.unwrap().elapsed().unwrap().as_secs(),
|
||||
vert_count,
|
||||
index_count);
|
||||
|
||||
total_vertices += vert_count;
|
||||
total_indices += index_count;
|
||||
}
|
||||
@@ -89,7 +100,7 @@ pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>, D
|
||||
}
|
||||
|
||||
if print_status {
|
||||
println!("Finished loading file, total vertices: {}, total indices: {}", total_vertices, total_indices);
|
||||
println!("Finished loading {} after {}ms, total vertices: {}, total indices: {}", mesh_path, start_time.unwrap().elapsed().unwrap().as_millis(), total_vertices, total_indices);
|
||||
}
|
||||
|
||||
Ok((meshes, document))
|
||||
|
||||
Reference in New Issue
Block a user