Files
vulkan-test-rs/src/loaders/gltf_loader.rs
T
alphafetus 639875b603 feat: switch to gltf crate and remove easy_gltf
This is because easy_gltf did not expose texture information, which the gltf crate did.
2026-08-13 07:12:21 -04:00

57 lines
1.8 KiB
Rust

use std::path::Path;
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 (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 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);
}
}
}
}
Ok(MeshInfo {
vertices,
indices,
normals,
uvs
})
}
}