mirror of
https://codeberg.org/icewind/vmdl.git
synced 2026-06-03 16:44:11 +02:00
texture wip
This commit is contained in:
parent
cd5ec8492e
commit
7948e6ba5b
8 changed files with 1180 additions and 67 deletions
97
examples/view/loader.rs
Normal file
97
examples/view/loader.rs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
use std::fmt::{Debug, Formatter};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use steamlocate::SteamDir;
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, error, info};
|
||||
use vpk::VPK;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum LoadError {
|
||||
#[error("{0}")]
|
||||
Other(&'static str),
|
||||
#[error(transparent)]
|
||||
IO(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
impl From<&'static str> for LoadError {
|
||||
fn from(e: &'static str) -> Self {
|
||||
LoadError::Other(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Loader {
|
||||
tf_dir: PathBuf,
|
||||
download: PathBuf,
|
||||
vpks: Vec<VPK>,
|
||||
}
|
||||
|
||||
impl Debug for Loader {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Loader")
|
||||
.field("tf_dir", &self.tf_dir)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl Loader {
|
||||
pub fn new() -> Result<Self, LoadError> {
|
||||
let tf_dir = SteamDir::locate()
|
||||
.ok_or("Can't find steam directory")?
|
||||
.app(&440)
|
||||
.ok_or("Can't find tf2 directory")?
|
||||
.path
|
||||
.join("tf");
|
||||
let download = tf_dir.join("download");
|
||||
let vpks = tf_dir
|
||||
.read_dir()?
|
||||
.filter_map(|item| item.ok())
|
||||
.filter_map(|item| Some(item.path().to_str()?.to_string()))
|
||||
.filter(|path| path.ends_with("dir.vpk"))
|
||||
.map(|path| vpk::from_path(&path))
|
||||
.filter_map(|res| res.ok())
|
||||
.collect();
|
||||
|
||||
Ok(Loader {
|
||||
tf_dir,
|
||||
download,
|
||||
vpks,
|
||||
})
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
pub fn load(&self, name: &str) -> Result<Vec<u8>, LoadError> {
|
||||
debug!("loading {}", name);
|
||||
if name.ends_with("bsp") {
|
||||
let path = self.tf_dir.join(name);
|
||||
if path.exists() {
|
||||
debug!("found in tf2 dir");
|
||||
return Ok(fs::read(path)?);
|
||||
}
|
||||
let path = self.download.join(name);
|
||||
if path.exists() {
|
||||
debug!("found in download dir");
|
||||
return Ok(fs::read(path)?);
|
||||
}
|
||||
}
|
||||
for vpk in self.vpks.iter() {
|
||||
if let Some(entry) = vpk.tree.get(name) {
|
||||
let data = entry.get()?.into_owned();
|
||||
debug!("got {} bytes from vpk", data.len());
|
||||
return Ok(data);
|
||||
}
|
||||
}
|
||||
info!("Failed to find {} in vpk", name);
|
||||
Err(LoadError::Other("Can't find file in vpks"))
|
||||
}
|
||||
|
||||
pub fn load_from_paths(&self, name: &str, paths: &[String]) -> Result<Vec<u8>, LoadError> {
|
||||
for path in paths {
|
||||
if let Ok(data) = self.load(&format!("{}{}", path, name)) {
|
||||
return Ok(data);
|
||||
}
|
||||
}
|
||||
error!("Failed to find {} in vpk paths: {}", name, paths.join(", "));
|
||||
Err(LoadError::Other("Can't find file in vpks"))
|
||||
}
|
||||
}
|
||||
361
examples/view/main.rs
Normal file
361
examples/view/main.rs
Normal file
|
|
@ -0,0 +1,361 @@
|
|||
mod loader;
|
||||
|
||||
use crate::loader::{LoadError, Loader};
|
||||
use std::env::args_os;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use thiserror::Error;
|
||||
use three_d::*;
|
||||
use tracing::error;
|
||||
use vmdl::mdl::Mdl;
|
||||
use vmdl::vtx::Vtx;
|
||||
use vmdl::vvd::Vvd;
|
||||
use vmdl::{Model, Vector};
|
||||
use vtf::vtf::VTF;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
enum Error {
|
||||
#[error(transparent)]
|
||||
Three(#[from] Box<dyn std::error::Error>),
|
||||
#[error(transparent)]
|
||||
Mdl(#[from] vmdl::ModelError),
|
||||
#[error(transparent)]
|
||||
IO(#[from] std::io::Error),
|
||||
#[error(transparent)]
|
||||
Render(#[from] RendererError),
|
||||
#[error(transparent)]
|
||||
Loader(#[from] LoadError),
|
||||
#[error(transparent)]
|
||||
Vtf(#[from] vtf::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[allow(missing_docs)]
|
||||
pub enum DebugType {
|
||||
POSITION,
|
||||
NORMAL,
|
||||
COLOR,
|
||||
DEPTH,
|
||||
ORM,
|
||||
UV,
|
||||
NONE,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
miette::set_panic_hook();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let mut args = args_os();
|
||||
let _ = args.next();
|
||||
let path = PathBuf::from(args.next().expect("No demo file provided"));
|
||||
let model = load(&path).unwrap();
|
||||
|
||||
let window = Window::new(WindowSettings {
|
||||
title: path.display().to_string(),
|
||||
min_size: (512, 512),
|
||||
max_size: Some((1920, 1080)),
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
let context = window.gl();
|
||||
|
||||
let mut camera = Camera::new_perspective(
|
||||
window.viewport(),
|
||||
vec3(2.0, 2.0, 5.0),
|
||||
vec3(0.0, 0.0, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
degrees(90.0),
|
||||
0.01,
|
||||
300.0,
|
||||
);
|
||||
|
||||
let mut control = OrbitControl::new(*camera.target(), 1.0, 100.0);
|
||||
let mut gui = three_d::GUI::new(&context);
|
||||
|
||||
let loader = Loader::new().expect("loader");
|
||||
|
||||
let cpu_model = model_to_model(&model, &loader);
|
||||
let ph_material = PhysicalMaterial {
|
||||
albedo: Color {
|
||||
r: 128,
|
||||
g: 128,
|
||||
b: 128,
|
||||
a: 255,
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let model: three_d::Model<PhysicalMaterial> = three_d::Model::new(&context, &cpu_model)?;
|
||||
|
||||
let mut directional = [
|
||||
DirectionalLight::new(&context, 1.0, Color::WHITE, &vec3(1.0, -1.0, 0.0)),
|
||||
DirectionalLight::new(&context, 1.0, Color::WHITE, &vec3(1.0, 1.0, 0.0)),
|
||||
];
|
||||
let mut ambient = AmbientLight {
|
||||
color: Color::WHITE,
|
||||
intensity: 0.2,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// main loop
|
||||
let mut shadows_enabled = true;
|
||||
let mut directional_intensity = directional[0].intensity;
|
||||
let mut depth_max = 30.0;
|
||||
let mut fov = 60.0;
|
||||
let mut debug_type = DebugType::NONE;
|
||||
|
||||
window.render_loop(move |mut frame_input| {
|
||||
let mut change = frame_input.first_frame;
|
||||
let mut panel_width = frame_input.viewport.width;
|
||||
change |= gui.update(
|
||||
&mut frame_input.events,
|
||||
frame_input.accumulated_time,
|
||||
frame_input.viewport,
|
||||
frame_input.device_pixel_ratio,
|
||||
|gui_context| {
|
||||
use three_d::egui::*;
|
||||
SidePanel::left("side_panel").show(gui_context, |ui| {
|
||||
ui.heading("Debug Panel");
|
||||
|
||||
ui.label("Light options");
|
||||
ui.add(
|
||||
Slider::new(&mut ambient.intensity, 0.0..=1.0).text("Ambient intensity"),
|
||||
);
|
||||
ui.add(
|
||||
Slider::new(&mut directional_intensity, 0.0..=1.0)
|
||||
.text("Directional intensity"),
|
||||
);
|
||||
directional[0].intensity = directional_intensity;
|
||||
directional[1].intensity = directional_intensity;
|
||||
if ui.checkbox(&mut shadows_enabled, "Shadows").clicked() {
|
||||
if !shadows_enabled {
|
||||
directional[0].clear_shadow_map();
|
||||
directional[1].clear_shadow_map();
|
||||
}
|
||||
}
|
||||
|
||||
ui.label("Debug options");
|
||||
ui.radio_value(&mut debug_type, DebugType::NONE, "None");
|
||||
ui.radio_value(&mut debug_type, DebugType::POSITION, "Position");
|
||||
ui.radio_value(&mut debug_type, DebugType::NORMAL, "Normal");
|
||||
ui.radio_value(&mut debug_type, DebugType::COLOR, "Color");
|
||||
ui.radio_value(&mut debug_type, DebugType::DEPTH, "Depth");
|
||||
ui.radio_value(&mut debug_type, DebugType::ORM, "ORM");
|
||||
|
||||
ui.label("View options");
|
||||
ui.add(Slider::new(&mut depth_max, 1.0..=30.0).text("Depth max"));
|
||||
ui.add(Slider::new(&mut fov, 45.0..=90.0).text("FOV"));
|
||||
|
||||
ui.label("Position");
|
||||
ui.add(Label::new(format!("\tx: {}", camera.position().x)));
|
||||
ui.add(Label::new(format!("\ty: {}", camera.position().y)));
|
||||
ui.add(Label::new(format!("\tz: {}", camera.position().z)));
|
||||
});
|
||||
panel_width = gui_context.used_size().x as u32;
|
||||
},
|
||||
);
|
||||
|
||||
let viewport = Viewport {
|
||||
x: panel_width as i32,
|
||||
y: 0,
|
||||
width: frame_input.viewport.width - panel_width,
|
||||
height: frame_input.viewport.height,
|
||||
};
|
||||
change |= camera.set_viewport(viewport);
|
||||
change |= control.handle_events(&mut camera, &mut frame_input.events);
|
||||
|
||||
// Draw
|
||||
{
|
||||
camera.set_perspective_projection(degrees(fov), camera.z_near(), camera.z_far());
|
||||
if shadows_enabled {
|
||||
directional[0].generate_shadow_map(1024, model.iter().map(|gm| &gm.geometry));
|
||||
directional[1].generate_shadow_map(1024, model.iter().map(|gm| &gm.geometry));
|
||||
}
|
||||
|
||||
let lights = &[&ambient as &dyn Light, &directional[0], &directional[1]];
|
||||
|
||||
// Light pass
|
||||
let screen = frame_input.screen();
|
||||
let target = screen.clear(ClearState::default());
|
||||
match debug_type {
|
||||
DebugType::NORMAL => target.render_with_material(
|
||||
&NormalMaterial::from_physical_material(&ph_material),
|
||||
&camera,
|
||||
model.iter().map(|gm| &gm.geometry),
|
||||
lights,
|
||||
),
|
||||
DebugType::DEPTH => {
|
||||
let mut depth_material = DepthMaterial::default();
|
||||
depth_material.max_distance = Some(depth_max);
|
||||
target.render_with_material(
|
||||
&depth_material,
|
||||
&camera,
|
||||
model.iter().map(|gm| &gm.geometry),
|
||||
lights,
|
||||
)
|
||||
}
|
||||
DebugType::ORM => target.render_with_material(
|
||||
&ORMMaterial::from_physical_material(&ph_material),
|
||||
&camera,
|
||||
model.iter().map(|gm| &gm.geometry),
|
||||
lights,
|
||||
),
|
||||
DebugType::POSITION => {
|
||||
let position_material = PositionMaterial::default();
|
||||
target.render_with_material(
|
||||
&position_material,
|
||||
&camera,
|
||||
model.iter().map(|gm| &gm.geometry),
|
||||
lights,
|
||||
)
|
||||
}
|
||||
DebugType::UV => {
|
||||
let uv_material = UVMaterial::default();
|
||||
target.render_with_material(
|
||||
&uv_material,
|
||||
&camera,
|
||||
model.iter().map(|gm| &gm.geometry),
|
||||
lights,
|
||||
)
|
||||
}
|
||||
DebugType::COLOR => target.render_with_material(
|
||||
&ColorMaterial::from_physical_material(&ph_material),
|
||||
&camera,
|
||||
model.iter().map(|gm| &gm.geometry),
|
||||
lights,
|
||||
),
|
||||
DebugType::NONE => target.render(&camera, &model, lights),
|
||||
}
|
||||
.write(|| gui.render());
|
||||
}
|
||||
|
||||
let _ = change;
|
||||
|
||||
FrameOutput::default()
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load(path: &Path) -> Result<Model, vmdl::ModelError> {
|
||||
let data = fs::read(path)?;
|
||||
let mdl = Mdl::read(&data)?;
|
||||
let data = fs::read(path.with_extension("dx90.vtx"))?;
|
||||
let vtx = Vtx::read(&data)?;
|
||||
let data = fs::read(path.with_extension("vvd"))?;
|
||||
let vvd = Vvd::read(&data)?;
|
||||
|
||||
Ok(Model::from_parts(mdl, vtx, vvd))
|
||||
}
|
||||
|
||||
// 1 hammer unit is ~1.905cm
|
||||
const UNIT_SCALE: f32 = 1.0 / (1.905 * 100.0);
|
||||
|
||||
fn model_to_model(model: &Model, loader: &Loader) -> CpuModel {
|
||||
let offset = model
|
||||
.vertices()
|
||||
.iter()
|
||||
.map(|vert| vert.position.y)
|
||||
.max_by(|a, b| a.total_cmp(b))
|
||||
.unwrap();
|
||||
let offset = Vector {
|
||||
x: 0.0,
|
||||
y: -offset / 2.0,
|
||||
z: 0.0,
|
||||
};
|
||||
|
||||
let positions: Vec<Vec3> = model
|
||||
.vertices()
|
||||
.iter()
|
||||
.map(|vertex| ((vertex.position + offset) * UNIT_SCALE * 10.0).into())
|
||||
.collect();
|
||||
let normals: Vec<Vec3> = model
|
||||
.vertices()
|
||||
.iter()
|
||||
.map(|vertex| vertex.normal.into())
|
||||
.collect();
|
||||
let uvs: Vec<Vec2> = model
|
||||
.vertices()
|
||||
.iter()
|
||||
.map(|vertex| vertex.texture_coordinates.into())
|
||||
.collect();
|
||||
|
||||
let texture_names = model.textures();
|
||||
|
||||
let geometries = model
|
||||
.meshes()
|
||||
.map(|mesh| {
|
||||
let indices = Indices::U32(
|
||||
mesh.vertex_strip_indices()
|
||||
.flat_map(|strip| strip.map(|index| index as u32))
|
||||
.collect(),
|
||||
);
|
||||
|
||||
CpuMesh {
|
||||
positions: Positions::F32(positions.clone()),
|
||||
normals: Some(normals.clone()),
|
||||
uvs: Some(uvs.clone()),
|
||||
material_name: Some(
|
||||
texture_names
|
||||
.get(mesh.material_index() as usize)
|
||||
.expect("texture out of bounds")
|
||||
.name
|
||||
.clone(),
|
||||
),
|
||||
indices,
|
||||
..Default::default()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let materials = model
|
||||
.textures()
|
||||
.iter()
|
||||
.map(|texture| {
|
||||
let dirs = model.texture_directories();
|
||||
match load_texture(&texture.name, dirs, loader) {
|
||||
Ok(texture) => CpuMaterial {
|
||||
albedo: Color::default(),
|
||||
name: texture.name.clone(),
|
||||
albedo_texture: Some(texture),
|
||||
..Default::default()
|
||||
},
|
||||
Err(e) => {
|
||||
error!("{:#}", e);
|
||||
CpuMaterial {
|
||||
albedo: Color {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 255,
|
||||
a: 255,
|
||||
},
|
||||
name: texture.name.clone(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
CpuModel {
|
||||
materials,
|
||||
geometries,
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue