Simplify process of FBX model conversion.

This commit is contained in:
Piotr Siuszko 2023-12-29 15:06:28 +01:00
parent 4868671e78
commit c46de6ebae
3 changed files with 40 additions and 50 deletions

View File

@ -23,6 +23,7 @@ opt-level = 2
clap = { version = "4.4", features = ["derive"] }
flate2 = "1.0"
gltf = "1.4.0"
pathdiff = "0.2.1"
rayon = "1.8.0"
regex = "1.10.2"
tar = "0.4"

View File

@ -6,7 +6,6 @@ use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;
#[derive(Clone)]
pub struct Asset {
@ -34,9 +33,9 @@ impl Asset {
}
let file = File::open(&self.path).unwrap();
let buf_reader = BufReader::new(file);
let search = buf_reader.lines().into_iter().find(|s| {
let search = buf_reader.lines().find(|s| {
let ss = s.as_ref().unwrap();
return ss.contains("m_Texture") && ss.contains("guid: ");
ss.contains("m_Texture") && ss.contains("guid: ")
});
if let Some(line) = search {
let line = line.unwrap_or_default();
@ -65,7 +64,7 @@ impl Asset {
}
}
pub fn from_path(entry: &DirEntry, output_dir: &PathBuf) -> Option<Asset> {
pub fn from_path(entry: &DirEntry, output_dir: &Path) -> Option<Asset> {
let root_file = entry.path();
if !root_file.is_dir() {
return None;

View File

@ -56,7 +56,7 @@ impl Unpacker {
.par_bridge()
.for_each_with(sender, |s, entry| {
let entry = entry.unwrap();
let asset = crate::asset::Asset::from_path(&entry, &output_dir.to_path_buf());
let asset = crate::asset::Asset::from_path(&entry, output_dir);
if let Some(asset) = asset {
let extension = &asset.extension.clone().unwrap_or_default();
if !ignored_extensions.contains(extension) {
@ -75,19 +75,19 @@ impl Unpacker {
.assets
.clone()
.into_iter()
.filter(|a| &a.asset_type == &AssetType::FbxModel)
.filter(|a| a.asset_type == AssetType::FbxModel)
.collect();
let prefabs: Vec<Asset> = self
.assets
.clone()
.into_iter()
.filter(|a| &a.asset_type == &AssetType::Prefab)
.filter(|a| a.asset_type == AssetType::Prefab)
.collect();
let materials: Vec<Asset> = self
.assets
.clone()
.into_iter()
.filter(|a| &a.asset_type == &AssetType::Material)
.filter(|a| a.asset_type == AssetType::Material)
.collect();
println!(
"There are {} models, {} prefabs and {} materials",
@ -117,9 +117,7 @@ impl Unpacker {
let texture_guid: Option<String> = material.try_get_mat_texture_guid();
let texture_asset: &Asset = match &texture_guid {
Some(guid) => {
self.assets.iter().find(|a| guid.eq(&a.guid)).unwrap()
}
Some(guid) => self.assets.iter().find(|a| guid.eq(&a.guid)).unwrap(),
None => return,
};
// here we should read gltf file and replace material texture with Uri based on texture_asset
@ -180,11 +178,10 @@ impl Unpacker {
let copy_meta_files = self.args.copy_meta_files;
let tmp_path = Path::new("./tmp_dir");
let mapping_arc = Arc::new(&self.assets);
let tmp_dir = Arc::new(tmp_path);
fs::create_dir(output_dir).unwrap();
mapping_arc.par_iter().for_each(|asset| {
self.assets.par_iter().for_each(|asset| {
let asset_hash = &asset.guid;
let path = Path::new(&asset.path);
let source_asset = Path::new(&*tmp_dir).join(asset_hash).join("asset");
@ -201,20 +198,18 @@ impl Unpacker {
panic!("SOURCE ASSET DOES NOT EXIST: {}", source_asset.display());
}
if self.args.fbx_to_gltf.is_some() && &asset.asset_type == &AssetType::FbxModel {
process_fbx_file(
&source_asset,
path,
&self.args.fbx_to_gltf.clone().unwrap(),
);
if self.args.fbx_to_gltf.is_some() && asset.asset_type == AssetType::FbxModel {
self.process_fbx_file(&source_asset, path);
} else {
process_non_fbx_file(&source_asset, path);
fs::rename(source_asset, path).unwrap();
}
});
fs::remove_dir_all(Path::new(&*tmp_dir)).unwrap();
}
fn process_fbx_file(source_asset: &Path, result_path: &Path, tool: &PathBuf) {
fn process_fbx_file(&self, source_asset: &Path, result_path: &Path) {
let tool = self.args.fbx_to_gltf.clone().unwrap();
let out_path = result_path.with_extension("");
println!(
"{:?}",
@ -239,11 +234,6 @@ impl Unpacker {
println!("output: {}", output_result);
}
fn process_non_fbx_file(source_asset: &Path, result_path: &Path) {
fs::rename(source_asset, result_path).unwrap();
}
}
fn extract_archive(archive_path: &Path, extract_to: &Path) -> io::Result<()> {
let tar_gz = File::open(archive_path)?;
let tar = GzDecoder::new(tar_gz);