gltf: only save used materials

This commit is contained in:
Robin Appelman 2023-12-17 15:37:55 +01:00
commit 655c4a561f
2 changed files with 11 additions and 12 deletions

View file

@ -11,7 +11,7 @@ use gltf_json::{Accessor, Extras, Image, Index, Material, Mesh, Texture, Value};
use image::png::PngEncoder;
use image::GenericImageView;
use std::mem::size_of;
use vmdl::{Model, SkinTable};
use vmdl::Model;
#[derive(Copy, Clone, Debug, Default, Zeroable, Pod)]
#[repr(C)]
@ -116,14 +116,13 @@ pub fn push_model(
views: &mut Vec<View>,
accessors: &mut Vec<Accessor>,
model: &Model,
skin: &SkinTable,
) -> Mesh {
let accessor_start = accessors.len() as u32;
push_vertices(buffer, views, accessors, model);
let primitives = model
.meshes()
.map(|mesh| push_primitive(buffer, views, accessors, &mesh, accessor_start, skin))
.map(|mesh| push_primitive(buffer, views, accessors, &mesh, accessor_start))
.collect();
Mesh {
@ -141,7 +140,6 @@ pub fn push_primitive(
accessors: &mut Vec<Accessor>,
mesh: &vmdl::Mesh,
vertex_accessor_start: u32,
skin: &SkinTable,
) -> Primitive {
let buffer_start = buffer.len() as u32;
let view_start = views.len() as u32;
@ -203,10 +201,7 @@ pub fn push_primitive(
extensions: Default::default(),
extras: Default::default(),
indices: Some(Index::new(accessor_start)),
material: Some(Index::new(
skin.texture_index(mesh.material_index())
.expect("skin out of bounds") as u32,
)),
material: Some(Index::new(mesh.material_index() as u32)),
mode: Valid(Mode::Triangles),
targets: None,
}

View file

@ -15,6 +15,7 @@ pub use error::Error;
use gltf_json::Index;
use main_error::MainResult;
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::path::PathBuf;
use vmdl::Model;
@ -43,11 +44,14 @@ fn export(model: Model, skin: u16, target: PathBuf) -> Result<(), Error> {
let loader = Loader::new()?;
let mesh = push_model(&mut buffer, &mut views, &mut accessors, &model, &skin);
let mesh = push_model(&mut buffer, &mut views, &mut accessors, &model);
let materials = model
.textures()
.iter()
let used_materials: BTreeSet<_> = model.meshes().map(|mesh| mesh.material_index()).collect();
let materials = used_materials
.into_iter()
.map(|mat_index| skin.texture_index(mat_index).unwrap())
.map(|tex_index| &model.textures()[tex_index])
.map(|tex| load_material_fallback(&tex.name, &tex.search_paths, &loader))
.map(|material| {
push_material(