texture gaming

This commit is contained in:
Quaternions 2024-01-08 23:08:35 -08:00
parent 9e30f5dca7
commit f639047980

View File

@ -1047,50 +1047,39 @@ fn write_attributes() -> AResult<()>{
fn extract_textures(paths:Vec<std::path::PathBuf>,vpk_path:std::path::PathBuf)->AResult<()>{ fn extract_textures(paths:Vec<std::path::PathBuf>,vpk_path:std::path::PathBuf)->AResult<()>{
let vpk_index=vpk::VPK::read(&vpk_path)?; let vpk_index=vpk::VPK::read(&vpk_path)?;
let mut vpk_cache=std::collections::HashMap::new(); let mut deduplicate=std::collections::HashSet::new();
for path in paths{ for path in paths{
let bsp=vbsp::Bsp::read(std::fs::read(path)?.as_ref())?; let bsp=vbsp::Bsp::read(std::fs::read(path)?.as_ref())?;
for texture in bsp.textures(){ for texture in bsp.textures(){
let texture_name=texture.texture_data().name(); let texture_name=texture.texture_data().name();
let texture_file_name=format!("materials/{}.vtf",texture_name); let texture_file_name=format!("materials/{}.vtf",texture_name.to_lowercase());
if !deduplicate.insert(texture_file_name.clone()){
continue;
}
println!("texture_name={}",texture_file_name); println!("texture_name={}",texture_file_name);
if let Some(stuff)=vpk_index.tree.get(texture_file_name.as_str()){ if let Some(stuff)=vpk_index.tree.get(texture_file_name.as_str()){
let vpk=if let Some(vpk)=vpk_cache.get(stuff.archive_path.as_str()){ let image=vtf::from_bytes(&mut stuff.get()?.to_vec())?.highres_image.decode(0)?.to_rgba8();
println!("ayyyy");
vpk let format=if image.width()%4!=0||image.height()%4!=0{
image_dds::ImageFormat::R8G8B8A8Srgb
}else{ }else{
println!("hiii"); image_dds::ImageFormat::BC7Srgb
let vpk=vpk::VPK::read(&std::path::PathBuf::from(&stuff.archive_path))?;
println!("hiiiiii");
vpk_cache.insert(stuff.archive_path.as_str(),vpk);
vpk_cache.get(stuff.archive_path.as_str()).unwrap()
}; };
if let Some(stuff)=vpk.tree.get(texture_file_name.as_str()){ //this fails if the image dimensions are not a multiple of 4
let image=vtf::from_bytes(&mut stuff.get()?.to_vec())?.highres_image.decode(0)?.to_rgba8(); let dds = image_dds::dds_from_image(
&image,
format,
image_dds::Quality::Slow,
image_dds::Mipmaps::GeneratedAutomatic,
)?;
let format=if image.width()%4!=0||image.height()%4!=0{ //write dds
image_dds::ImageFormat::R8G8B8A8Srgb let mut dest=std::path::PathBuf::from("textures/dds");
}else{ dest.push(texture_name);
image_dds::ImageFormat::BC7Srgb dest.set_extension("dds");
}; std::fs::create_dir_all(dest.parent().unwrap())?;
//this fails if the image dimensions are not a multiple of 4 let mut writer = std::io::BufWriter::new(std::fs::File::create(dest)?);
let dds = image_dds::dds_from_image( dds.write(&mut writer)?;
&image,
format,
image_dds::Quality::Slow,
image_dds::Mipmaps::GeneratedAutomatic,
)?;
//write dds
let mut dest=std::path::PathBuf::from("textures/dds");
dest.push(texture_name);
dest.set_extension("dds");
std::fs::create_dir_all(dest.parent().unwrap())?;
let mut writer = std::io::BufWriter::new(std::fs::File::create(dest)?);
dds.write(&mut writer)?;
}else{
println!("zip");
}
}else{ }else{
println!("nonadat"); println!("nonadat");
} }