mirror of
https://codeberg.org/icewind/vbspview.git
synced 2026-06-03 18:24:09 +02:00
material handling
This commit is contained in:
parent
5def69f525
commit
eb3df01eb6
7 changed files with 172 additions and 99 deletions
46
Cargo.lock
generated
46
Cargo.lock
generated
|
|
@ -740,23 +740,22 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "5.0.1"
|
||||
version = "3.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
|
||||
checksum = "30baa043103c9d0c2a57cf537cc2f35623889dc0d405e6c3cccfadbc81c71309"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.4.1"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
|
||||
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"option-ext",
|
||||
"redox_users",
|
||||
"windows-sys 0.48.0",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1530,6 +1529,12 @@ dependencies = [
|
|||
"syn 2.0.39",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.3.7"
|
||||
|
|
@ -1662,9 +1667,13 @@ checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
|
|||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "1.2.4"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
|
|
@ -1850,12 +1859,6 @@ version = "1.19.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "osmesa-sys"
|
||||
version = "0.1.2"
|
||||
|
|
@ -2494,23 +2497,20 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "steamlocate"
|
||||
version = "1.2.1"
|
||||
version = "2.0.0-alpha.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ec01c74611d14a808cb212d17c6e03f0e30736a15ed1d5736f8a53154cea3ae"
|
||||
checksum = "2b1568c4a70a26c4373fe1131ffa4eff055459631b6e40c6bc118615f2d870c3"
|
||||
dependencies = [
|
||||
"dirs",
|
||||
"keyvalues-parser",
|
||||
"keyvalues-serde",
|
||||
"serde",
|
||||
"steamy-vdf",
|
||||
"winreg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "steamy-vdf"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "533127ad49314bfe71c3d3fd36b3ebac3d24f40618092e70e1cfe8362c7fac79"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
|
@ -2987,6 +2987,7 @@ dependencies = [
|
|||
"splines",
|
||||
"steamid-ng",
|
||||
"steamlocate",
|
||||
"steamy-vdf",
|
||||
"tf-demo-parser",
|
||||
"thiserror",
|
||||
"three-d",
|
||||
|
|
@ -3448,11 +3449,10 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.11.0"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76a1a57ff50e9b408431e8f97d5456f2807f8eb2a2cd79b06068fc87f8ecf189"
|
||||
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ miette = { version = "5.5.0", features = ["fancy"] }
|
|||
thiserror = "1.0.37"
|
||||
delaunator = "1.0.1"
|
||||
itertools = "0.10.5"
|
||||
steamlocate = "1.1.0"
|
||||
steamlocate = "2.0.0-alpha.0"
|
||||
vpk = { version = "0.1.4", git = "https://github.com/icewind1991/vpk-rs", branch = "perf" }
|
||||
#vmdl = { version = "*", git = "https://github.com/icewind1991/vmdl" }
|
||||
vmdl = { version = "*", path = "../vmdl" }
|
||||
|
|
@ -26,6 +26,7 @@ steamid-ng = "1.0.0"
|
|||
clap = { version = "4.0.29", features = ["derive"] }
|
||||
splines = { version = "4.1.1", features = ["cgmath"] }
|
||||
vtf = { version = "0.1.5", git = "https://github.com/roman901/vtf-rs", rev = "55670df" }
|
||||
steamy-vdf = { version = "0.3.0", path = "../rust/steamy/vdf" }
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 2
|
||||
|
|
|
|||
32
src/bsp.rs
32
src/bsp.rs
|
|
@ -1,10 +1,9 @@
|
|||
use crate::material::load_material_fallback;
|
||||
use crate::prop::load_props;
|
||||
use crate::{Error, Loader};
|
||||
use cgmath::vec4;
|
||||
use std::collections::HashMap;
|
||||
use three_d::{
|
||||
Color, CpuMaterial, CpuMesh, CpuModel, CpuTexture, Mat4, Positions, TextureData, Vec2, Vec3,
|
||||
};
|
||||
use three_d::{CpuMesh, CpuModel, Mat4, Positions, Vec2, Vec3};
|
||||
use vbsp::{Bsp, Face, Handle};
|
||||
|
||||
pub fn load_map(data: &[u8], loader: &mut Loader) -> Result<Vec<CpuModel>, Error> {
|
||||
|
|
@ -84,32 +83,7 @@ fn model_to_model(model: Handle<vbsp::data::Model>, loader: &Loader) -> CpuModel
|
|||
.values()
|
||||
.map(|face| {
|
||||
let texture = face.first().unwrap().texture();
|
||||
let tex_file = format!("materials/{}.vtf", texture.name().to_lowercase());
|
||||
let vtf_data = loader.load(&tex_file).ok();
|
||||
let texture_data = vtf_data.and_then(|mut vtf_data| {
|
||||
let vtf = vtf::from_bytes(&mut vtf_data).ok()?;
|
||||
let image = vtf.highres_image.decode(0).ok()?;
|
||||
Some(CpuTexture {
|
||||
name: texture.name().into(),
|
||||
data: TextureData::RgbaU8(
|
||||
image.into_rgba8().pixels().map(|pixel| pixel.0).collect(),
|
||||
),
|
||||
height: texture.texture_data().height as u32,
|
||||
width: texture.texture_data().width as u32,
|
||||
..CpuTexture::default()
|
||||
})
|
||||
});
|
||||
let color = if texture_data.is_some() {
|
||||
Color::default()
|
||||
} else {
|
||||
Color::new(255, 0, 255, 255)
|
||||
};
|
||||
CpuMaterial {
|
||||
albedo: color,
|
||||
albedo_texture: texture_data,
|
||||
name: texture.name().into(),
|
||||
..Default::default()
|
||||
}
|
||||
load_material_fallback(texture.name(), &["".into()], loader)
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use std::fmt::{Debug, Formatter};
|
|||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use steamlocate::SteamDir;
|
||||
use tracing::{debug, error};
|
||||
use tracing::{debug, error, info};
|
||||
use vbsp::Packfile;
|
||||
use vpk::VPK;
|
||||
|
||||
|
|
@ -89,8 +89,8 @@ impl Loader {
|
|||
return Ok(data);
|
||||
}
|
||||
}
|
||||
error!("Failed to find {} in vpk", name);
|
||||
Err(Error::Other("Can't find file in vpks"))
|
||||
info!("Failed to find {} in vpk", name);
|
||||
Err(Error::ResourceNotFound(name.to_string()))
|
||||
}
|
||||
|
||||
pub fn load_from_paths(&self, name: &str, paths: &[String]) -> Result<Vec<u8>, Error> {
|
||||
|
|
@ -100,6 +100,6 @@ impl Loader {
|
|||
}
|
||||
}
|
||||
error!("Failed to find {} in vpk paths: {}", name, paths.join(", "));
|
||||
Err(Error::Other("Can't find file in vpks"))
|
||||
Err(Error::ResourceNotFound(name.to_string()))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ mod bsp;
|
|||
mod control;
|
||||
mod demo;
|
||||
mod loader;
|
||||
mod material;
|
||||
mod prop;
|
||||
mod renderer;
|
||||
mod ui;
|
||||
|
|
@ -9,6 +10,7 @@ mod wrapping;
|
|||
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use crate::bsp::load_map;
|
||||
use crate::control::{Control, DemoCamera};
|
||||
|
|
@ -45,6 +47,8 @@ pub enum Error {
|
|||
#[error(transparent)]
|
||||
Vtf(#[from] vtf::Error),
|
||||
#[error(transparent)]
|
||||
Vdf(#[from] steamy_vdf::Error),
|
||||
#[error(transparent)]
|
||||
Mdl(#[from] vmdl::ModelError),
|
||||
#[error(transparent)]
|
||||
Demo(#[from] tf_demo_parser::ParseError),
|
||||
|
|
@ -54,6 +58,10 @@ pub enum Error {
|
|||
Window(#[from] WindowError),
|
||||
#[error(transparent)]
|
||||
Render(#[from] RendererError),
|
||||
#[error(transparent)]
|
||||
String(#[from] FromUtf8Error),
|
||||
#[error("resource {0} not found in vpks or pack")]
|
||||
ResourceNotFound(String),
|
||||
}
|
||||
|
||||
impl From<&'static str> for Error {
|
||||
|
|
|
|||
127
src/material.rs
Normal file
127
src/material.rs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
use crate::loader::Loader;
|
||||
use crate::Error;
|
||||
use std::io::Cursor;
|
||||
use steamy_vdf::{Entry, Table};
|
||||
use three_d::{Color, CpuMaterial, CpuTexture, TextureData};
|
||||
use tracing::error;
|
||||
use vtf::vtf::VTF;
|
||||
|
||||
pub fn load_material_fallback(name: &str, search_dirs: &[String], loader: &Loader) -> CpuMaterial {
|
||||
match load_material(name, search_dirs, loader) {
|
||||
Ok(material) => material,
|
||||
Err(e) => {
|
||||
error!(
|
||||
material = name,
|
||||
error = ?e,
|
||||
"failed to load material, falling back"
|
||||
);
|
||||
CpuMaterial {
|
||||
albedo: Color {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 255,
|
||||
a: 255,
|
||||
},
|
||||
name: name.into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_material(
|
||||
name: &str,
|
||||
search_dirs: &[String],
|
||||
loader: &Loader,
|
||||
) -> Result<CpuMaterial, Error> {
|
||||
let dirs = search_dirs
|
||||
.iter()
|
||||
.map(|dir| {
|
||||
format!(
|
||||
"materials/{}",
|
||||
dir.to_ascii_lowercase().trim_start_matches("/")
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let path = format!("{}.vmt", name.to_ascii_lowercase());
|
||||
let raw = loader.load_from_paths(&path, &dirs)?;
|
||||
|
||||
let vmt = parse_vdf(raw)?;
|
||||
let vmt = resolve_vmt_patch(vmt, loader)?;
|
||||
let table = vmt
|
||||
.values()
|
||||
.next()
|
||||
.expect("empty vmt")
|
||||
.as_table()
|
||||
.expect("vmt not a table");
|
||||
let base_texture = table
|
||||
.iter()
|
||||
.find_map(|(key, value)| (key.to_ascii_lowercase() == "$basetexture").then_some(value))
|
||||
.expect("no $basetexture")
|
||||
.as_value()
|
||||
.expect("$basetexture not a value")
|
||||
.to_string()
|
||||
.to_ascii_lowercase()
|
||||
.replace('\\', "/")
|
||||
.replace('\t', "/t");
|
||||
let texture = load_texture(base_texture.as_str(), loader)?;
|
||||
Ok(CpuMaterial {
|
||||
name: name.into(),
|
||||
albedo: Color::WHITE,
|
||||
albedo_texture: Some(texture),
|
||||
..CpuMaterial::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_vdf(bytes: Vec<u8>) -> Result<Table, Error> {
|
||||
let mut reader = steamy_vdf::read(Cursor::new(&bytes))?;
|
||||
Table::load(&mut reader).map_err(|e| {
|
||||
error!(
|
||||
source = String::from_utf8_lossy(&bytes).to_string(),
|
||||
"failed to parse vmt"
|
||||
);
|
||||
e.into()
|
||||
})
|
||||
}
|
||||
|
||||
fn load_texture(name: &str, loader: &Loader) -> Result<CpuTexture, Error> {
|
||||
let path = format!(
|
||||
"materials/{}.vtf",
|
||||
name.trim_end_matches(".vtf").trim_start_matches("/")
|
||||
);
|
||||
let mut raw = loader.load(&path)?;
|
||||
let vtf = VTF::read(&mut raw)?;
|
||||
let image = vtf.highres_image.decode(0)?;
|
||||
Ok(CpuTexture {
|
||||
name: name.into(),
|
||||
data: TextureData::RgbaU8(image.into_rgba8().pixels().map(|pixel| pixel.0).collect()),
|
||||
height: vtf.header.height as u32,
|
||||
width: vtf.header.width as u32,
|
||||
..CpuTexture::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_vmt_patch(vmt: Table, loader: &Loader) -> Result<Table, Error> {
|
||||
if vmt.len() != 1 {
|
||||
panic!("vmt with more than 1 item?");
|
||||
}
|
||||
if let Some(Entry::Table(patch)) = vmt.get("patch") {
|
||||
let include = patch
|
||||
.get("include")
|
||||
.expect("no include in patch")
|
||||
.as_value()
|
||||
.expect("include is not a value")
|
||||
.to_string();
|
||||
let _replace = patch
|
||||
.get("replace")
|
||||
.expect("no replace in patch")
|
||||
.as_table()
|
||||
.expect("replace is not a table");
|
||||
let included_raw = loader.load(&include.to_ascii_lowercase())?;
|
||||
|
||||
// todo actually patch
|
||||
parse_vdf(included_raw)
|
||||
} else {
|
||||
Ok(vmt)
|
||||
}
|
||||
}
|
||||
47
src/prop.rs
47
src/prop.rs
|
|
@ -1,16 +1,14 @@
|
|||
use crate::bsp::{apply_transform, map_coords};
|
||||
use crate::material::load_material_fallback;
|
||||
use crate::{Error, Loader};
|
||||
use cgmath::{Matrix, SquareMatrix};
|
||||
use std::collections::HashMap;
|
||||
use three_d::{
|
||||
Color, CpuMaterial, CpuMesh, CpuModel, CpuTexture, Mat4, Positions, TextureData, Vec2, Vec3,
|
||||
};
|
||||
use tracing::error;
|
||||
use three_d::{CpuMaterial, CpuMesh, CpuModel, Mat4, Positions, Vec2, Vec3};
|
||||
use tracing::warn;
|
||||
use vbsp::{Handle, StaticPropLump};
|
||||
use vmdl::mdl::{Mdl, TextureInfo};
|
||||
use vmdl::vtx::Vtx;
|
||||
use vmdl::vvd::Vvd;
|
||||
use vtf::vtf::VTF;
|
||||
|
||||
#[tracing::instrument(skip(loader))]
|
||||
pub fn load_prop(loader: &Loader, name: &str) -> Result<vmdl::Model, Error> {
|
||||
|
|
@ -68,7 +66,7 @@ fn prop_to_meshes(prop: &PropData) -> impl Iterator<Item = CpuMesh> + '_ {
|
|||
let skin = match model.skin_tables().nth(prop.skin as usize) {
|
||||
Some(skin) => skin,
|
||||
None => {
|
||||
error!(index = prop.skin, "invalid skin index");
|
||||
warn!(index = prop.skin, "invalid skin index");
|
||||
model.skin_tables().next().unwrap()
|
||||
}
|
||||
};
|
||||
|
|
@ -104,40 +102,5 @@ fn prop_to_meshes(prop: &PropData) -> impl Iterator<Item = CpuMesh> + '_ {
|
|||
}
|
||||
|
||||
fn prop_texture_to_material(texture: &TextureInfo, loader: &Loader) -> CpuMaterial {
|
||||
match load_texture(&texture.name, &texture.search_paths, loader) {
|
||||
Ok(texture) => CpuMaterial {
|
||||
albedo: Color::default(),
|
||||
name: texture.name.clone(),
|
||||
albedo_texture: Some(texture),
|
||||
..Default::default()
|
||||
},
|
||||
Err(_) => CpuMaterial {
|
||||
albedo: Color {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 255,
|
||||
a: 255,
|
||||
},
|
||||
name: texture.name.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn load_texture(name: &str, dirs: &[String], loader: &Loader) -> Result<CpuTexture, Error> {
|
||||
let dirs = dirs
|
||||
.iter()
|
||||
.map(|dir| format!("materials/{}", dir))
|
||||
.collect::<Vec<_>>();
|
||||
let path = format!("{}.vtf", name);
|
||||
let mut raw = loader.load_from_paths(&path, &dirs)?;
|
||||
let vtf = VTF::read(&mut raw)?;
|
||||
let image = vtf.highres_image.decode(0)?;
|
||||
Ok(CpuTexture {
|
||||
name: name.into(),
|
||||
data: TextureData::RgbaU8(image.into_rgba8().pixels().map(|pixel| pixel.0).collect()),
|
||||
height: vtf.header.height as u32,
|
||||
width: vtf.header.width as u32,
|
||||
..CpuTexture::default()
|
||||
})
|
||||
load_material_fallback(&texture.name, &texture.search_paths, loader)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue