Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several bug fixes #146

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions pof/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,41 @@ use dae_parser as dae;
use glm::Mat4x4;
use log::warn;
use nalgebra_glm as glm;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::fs::File;
use std::io::{self, BufReader, ErrorKind, Read, Seek, SeekFrom};
use std::path::PathBuf;

impl Model {
fn prune_unused_textures(&mut self) {
// remove unused textures
// tally up used texture ids
let mut used_tex_ids = HashSet::new();
for subobj in &self.sub_objects {
for (_, poly) in subobj.bsp_data.collision_tree.leaves() {
used_tex_ids.insert(poly.texture);
}
}

// while mapping the old to new ids, consume the existing textures, and re-add them once its clear they're used
let mut tex_map = HashMap::new();
for (i, tex) in std::mem::take(&mut self.textures).into_iter().enumerate() {
if used_tex_ids.contains(&TextureId(i as u32)) {
self.textures.push(tex);
tex_map.insert(TextureId(i as u32), TextureId(tex_map.len() as u32));
}
}

// apply the mapping
for subobj in self.sub_objects.iter_mut() {
for (_, poly) in subobj.bsp_data.collision_tree.leaves_mut() {
poly.texture = tex_map[&poly.texture];
}
}
}
}

pub struct Parser<R> {
file: R,
version: Version,
Expand Down Expand Up @@ -1525,6 +1554,9 @@ pub fn parse_dae(path: std::path::PathBuf) -> Model {

let scene = &document.scene.as_ref().unwrap().instance_visual_scene.as_ref().unwrap().url;
ctx.parse_top_level_nodes(&mut model, &ctx.local_maps.get(scene).unwrap().nodes);

model.prune_unused_textures();

model
}

Expand Down Expand Up @@ -1642,7 +1674,14 @@ pub fn parse_gltf(path: std::path::PathBuf) -> Model {
})
.collect();

GltfContext { buffers }.parse_top_level_nodes(&mut model, gltf.default_scene().expect("Default scene in gltf file not set!").nodes());
let scene = gltf
.default_scene()
.unwrap_or_else(|| gltf.scenes().next().expect("no scene found in gltf file!"));

GltfContext { buffers }.parse_top_level_nodes(&mut model, scene.nodes());

model.prune_unused_textures();

model
}

Expand Down
13 changes: 9 additions & 4 deletions src/ui_properties_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use egui::{style::Widgets, text::LayoutJob, CollapsingHeader, Color32, DragValue, Label, Response, RichText, TextEdit, TextFormat, TextStyle, Ui};
use glium::Display;
use itertools::Itertools;

Check warning on line 7 in src/ui_properties_panel.rs

View workflow job for this annotation

GitHub Actions / Build Linux

unused import: `itertools::Itertools`

Check warning on line 7 in src/ui_properties_panel.rs

View workflow job for this annotation

GitHub Actions / Build Windows

unused import: `itertools::Itertools`
use nalgebra_glm::TMat4;
use pof::{
Dock, Error, EyePoint, GlowPoint, GlowPointBank, Insignia, ObjectId, PathId, PathPoint, Set::*, SpecialPoint, SubsysRotationAxis,
Expand Down Expand Up @@ -2636,20 +2636,25 @@
if merge_duplicate_textures {
use pof::TextureId;
let mut tex_name_map = HashMap::new();
let mut merged_map = HashMap::new();
let mut changed_id_map = HashMap::new();
let mut new_textures = vec![];
for (i, tex) in self.model.textures.iter().enumerate() {
if tex_name_map.contains_key(tex) {
merged_map.insert(TextureId(i as u32), tex_name_map[tex]);
changed_id_map.insert(TextureId(i as u32), tex_name_map[tex]);
} else {
merged_map.insert(TextureId(i as u32), TextureId(tex_name_map.len() as u32));
changed_id_map.insert(TextureId(i as u32), TextureId(tex_name_map.len() as u32));
tex_name_map.insert(tex, TextureId(tex_name_map.len() as u32));
new_textures.push(tex.clone());
}
}

let mut new_map = self.model.texture_map.clone();
for (id1, id2) in changed_id_map {
*(new_map.get_mut(&id1).unwrap()) = id2;
}

undo_history
.apply(&mut self.model, UndoAction::ChangeTextures { id_map: merged_map, textures: new_textures })
.apply(&mut self.model, UndoAction::ChangeTextures { id_map: new_map, textures: new_textures })
.unwrap();

self.ui_state.properties_panel_dirty = true;
Expand Down
Loading