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:
alphafetus committed 2026-08-13 07:12:21 -04:00
1 parent 2d8b895841
commit 639875b603
6 files changed
+78 -68

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
use std::collections::HashMap;
pub struct MaterialRegistry {
materials: HashMap<String, Material>
}
impl MaterialRegistry {
pub fn new() -> Self {
Self {
materials: HashMap::new()
}
}
pub fn register(&mut self, id: String, material: Material) {
self.materials.insert(id, material);
}
pub fn get(&self, id: &String) -> Option<&Material> {
self.materials.get(id)
}
}
/// Encapsulates all the information that makes up a PBR (physically based rendering) material.
/// These materials should be stored in a [`MaterialRegistry`], with a unique key.
pub struct Material {
/// The base color of the material.
pub base_color: [f32; 4],
/// The metallic value of the material. This should be a number between 0 and 1.
pub metallic: f32,
/// The roughness value of the material. This should be a number between 0 and 1.
pub roughness: f32,
/// The path of the normal map for this material, if any.
pub normal_map: Option<String>,
/// The path of the base color's texture, if any.
pub base_color_texture: Option<String>,
}
+1
View File
@@ -3,3 +3,4 @@ pub mod mesh;
pub mod scene;
pub mod render_batch;
pub mod camera;
mod material;
+33 -23
View File
@@ -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 {