New flag: get_materials_from_prefabs

This commit is contained in:
Piotr Siuszko 2023-12-29 15:16:51 +01:00
parent c46de6ebae
commit c3b959f632
3 changed files with 14 additions and 16 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "lwa_unity_unpack" name = "lwa_unity_unpack"
version = "0.3.0" version = "0.4.0"
edition = "2021" edition = "2021"
repository = "https://github.com/Leinnan/lwa_unity_unpack" repository = "https://github.com/Leinnan/lwa_unity_unpack"
homepage = "https://github.com/Leinnan/lwa_unity_unpack" homepage = "https://github.com/Leinnan/lwa_unity_unpack"

View File

@ -16,6 +16,11 @@ pub struct Args {
#[arg(short, long)] #[arg(short, long)]
pub fbx_to_gltf: Option<PathBuf>, pub fbx_to_gltf: Option<PathBuf>,
/// checks if material base texture in prefabs differ from the one specified in fbx model
/// that is converted to GLTF and overrides it with the one from prefab and copy texture to models folder
#[arg(long, default_value = "false", default_missing_value = "true")]
pub get_materials_from_prefabs: bool,
/// optional- extensions that will be ignored during unpacking /// optional- extensions that will be ignored during unpacking
#[arg(long, action = clap::ArgAction::Append)] #[arg(long, action = clap::ArgAction::Append)]
pub ignore_extensions: Option<Vec<String>>, pub ignore_extensions: Option<Vec<String>>,

View File

@ -68,7 +68,7 @@ impl Unpacker {
} }
pub fn update_gltf_materials(&self) { pub fn update_gltf_materials(&self) {
if self.args.fbx_to_gltf.is_none() { if self.args.fbx_to_gltf.is_none() || !self.args.get_materials_from_prefabs {
return; return;
} }
let fbx_models: Vec<Asset> = self let fbx_models: Vec<Asset> = self
@ -122,7 +122,7 @@ impl Unpacker {
}; };
// here we should read gltf file and replace material texture with Uri based on texture_asset // here we should read gltf file and replace material texture with Uri based on texture_asset
let model_path = Path::new(&model.path).with_extension("glb"); let model_path = Path::new(&model.path).with_extension("glb");
Self::modify_material(&model_path, Path::new(&texture_asset.path)); Self::update_material(&model_path, Path::new(&texture_asset.path));
}); });
} }
@ -130,7 +130,7 @@ impl Unpacker {
*n = (*n + 3) & !3; *n = (*n + 3) & !3;
} }
fn modify_material(gltf_path: &Path, texture_asset: &Path) { fn update_material(gltf_path: &Path, texture_asset: &Path) {
let file = fs::File::open(gltf_path).unwrap(); let file = fs::File::open(gltf_path).unwrap();
let reader = io::BufReader::new(file); let reader = io::BufReader::new(file);
let mut gltf = gltf::Gltf::from_reader(reader).unwrap(); let mut gltf = gltf::Gltf::from_reader(reader).unwrap();
@ -211,16 +211,7 @@ impl Unpacker {
fn process_fbx_file(&self, source_asset: &Path, result_path: &Path) { fn process_fbx_file(&self, source_asset: &Path, result_path: &Path) {
let tool = self.args.fbx_to_gltf.clone().unwrap(); let tool = self.args.fbx_to_gltf.clone().unwrap();
let out_path = result_path.with_extension(""); let out_path = result_path.with_extension("");
println!( let _output = Command::new(tool)
"{:?}",
&[
"--input",
source_asset.to_str().unwrap(),
"--output",
out_path.to_str().unwrap()
]
);
let output = Command::new(tool)
.args([ .args([
"--input", "--input",
source_asset.to_str().unwrap(), source_asset.to_str().unwrap(),
@ -230,8 +221,10 @@ impl Unpacker {
]) ])
.output() .output()
.unwrap(); .unwrap();
let output_result = String::from_utf8_lossy(&output.stdout); println!(
println!("output: {}", output_result); "Fbx converted to GLTF: {}",
out_path.with_extension("glb").to_str().unwrap()
);
} }
fn extract_archive(archive_path: &Path, extract_to: &Path) -> io::Result<()> { fn extract_archive(archive_path: &Path, extract_to: &Path) -> io::Result<()> {