feat: switch to gltf crate and remove easy_gltf
This is because easy_gltf did not expose texture information, which the gltf crate did.
This commit is contained in:
1 parent
2d8b895841
commit
639875b603
6 files changed
+78
-68
No files matched your search
+33
-23
@@ -1,40 +1,50 @@
|
||||
use std::path::Path;
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::Result;
|
||||
use crate::loaders::mesh_loader::{MeshFileLoader, MeshInfo};
|
||||
|
||||
pub struct GltfLoader;
|
||||
|
||||
impl MeshFileLoader for GltfLoader {
|
||||
fn load_mesh(&self, path: &Path) -> Result<MeshInfo> {
|
||||
let scenes = easy_gltf::load(path)
|
||||
.map_err(|e| anyhow!("could not load gltf file: {e}"))?;
|
||||
|
||||
println!("Found {} scene(s) in gltf file", scenes.len());
|
||||
|
||||
let scene = scenes.first()
|
||||
.ok_or(anyhow!("no scenes in gltf file"))?;
|
||||
|
||||
println!("Found {} model(s) in gltf file", scene.models.len());
|
||||
let (document, buffers, _) = gltf::import(path)?;
|
||||
|
||||
let mut vertices = Vec::new();
|
||||
let mut normals = Vec::new();
|
||||
let mut uvs = Vec::new();
|
||||
let mut indices = Vec::<u32>::new();
|
||||
for model in &scene.models {
|
||||
// Indices must be offset by the number of vertices that are already registered.
|
||||
let idx_offset = vertices.len() as u32;
|
||||
|
||||
// Put all vertex info together for a MeshInfo struct
|
||||
for vertex in model.vertices() {
|
||||
vertices.push(*vertex.position.as_ref());
|
||||
normals.push(*vertex.normal.as_ref());
|
||||
uvs.push([vertex.tex_coords.x, vertex.tex_coords.y, 0.0]);
|
||||
for mesh in document.meshes() {
|
||||
for primitive in mesh.primitives() {
|
||||
// Read the primitives as vertices and indices,
|
||||
// and register a material for the primitive as well
|
||||
let vert_offset = vertices.len() as u32;
|
||||
|
||||
let reader = primitive.reader(|buffer_index| Some(&buffers[buffer_index.index()]));
|
||||
|
||||
if let Some(positions) = reader.read_positions() {
|
||||
for position in positions {
|
||||
vertices.push(position);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(normals_iter) = reader.read_normals() {
|
||||
for normal in normals_iter {
|
||||
normals.push(normal);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(tex_coords) = reader.read_tex_coords(0) {
|
||||
for uv in tex_coords.into_f32() {
|
||||
uvs.push([uv[0], uv[1], 0.0]);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(indices_iter) = reader.read_indices() {
|
||||
for index in indices_iter.into_u32() {
|
||||
indices.push(index + vert_offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
model.indices()
|
||||
.expect("failed to find indices")
|
||||
.iter()
|
||||
.map(|i| i + idx_offset)
|
||||
.for_each(|i| indices.push(i));
|
||||
}
|
||||
|
||||
Ok(MeshInfo {
|
||||
|
||||
Reference in new issue
Block a user