From 11c335aa3fbe94b953afedc83e8e15dc756c8d81 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 13 Dec 2023 23:39:58 -0800 Subject: [PATCH] convert: export OBJ materials with the level --- bin/convert/level_obj.rs | 32 ++++++++++++++++++++++++++------ bin/convert/main.rs | 4 ++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/bin/convert/level_obj.rs b/bin/convert/level_obj.rs index 37c5c22..2e33afb 100644 --- a/bin/convert/level_obj.rs +++ b/bin/convert/level_obj.rs @@ -10,9 +10,10 @@ const MAX_COLUMN_VERTICES: usize = 4 * 4; const EXTREME_HEIGHT: i32 = i32::max_value(); #[derive(Debug)] -pub struct Config { +pub struct Config<'a> { pub xr: Range, pub yr: Range, + pub palette: Option<&'a [u8]>, } #[derive(Clone)] @@ -158,7 +159,7 @@ struct VertexCollector<'p> { final_vertices: Vec<[i32; 3]>, vertex_columns: Vec>, face_columns: Vec>, - config: &'p Config, + config: &'p Config<'p>, initial_vertices: usize, initial_quads: usize, } @@ -190,6 +191,22 @@ impl VertexCollector<'_> { } pub fn save(path: &Path, level: &Level, config: &Config) { + if let Some(palette) = config.palette { + let mat_path = path.with_extension("mtl"); + let mut dest = BufWriter::new(File::create(&mat_path).unwrap()); + for (i, color) in palette.chunks(3).enumerate() { + writeln!(dest, "newmtl t{}", i).unwrap(); + writeln!( + dest, + "\tKd {} {} {}", + color[0] as f32 / 255.0, + color[1] as f32 / 255.0, + color[2] as f32 / 255.0, + ) + .unwrap(); + } + } + let mut groups: [Vec<[u32; 4]>; 16] = Default::default(); let mut bar = progress::Bar::new(); @@ -330,7 +347,10 @@ pub fn save(path: &Path, level: &Level, config: &Config) { } bar.jobs_done(); - let num_quads: usize = groups.iter().map(|g| g.len()).sum(); + let num_quads: usize = groups + .iter() + .flat_map(|g| g.iter().map(|plane| plane.len())) + .sum(); fn unit(count: usize) -> f32 { count as f32 / 1_000_000.0 } @@ -349,9 +369,9 @@ pub fn save(path: &Path, level: &Level, config: &Config) { } bar.set_job_title("Faces:"); writeln!(dest).unwrap(); - for (i, g) in groups.iter().enumerate() { - writeln!(dest, "g m{}", i).unwrap(); - for t in g { + for (i, group) in groups.iter().enumerate() { + writeln!(dest, "usemtl t{}", i).unwrap(); + for t in group { writeln!( dest, "f {} {} {} {}", diff --git a/bin/convert/main.rs b/bin/convert/main.rs index ed2a8ce..06ac81d 100644 --- a/bin/convert/main.rs +++ b/bin/convert/main.rs @@ -150,6 +150,8 @@ fn main() { println!("\tLoading the level..."); let config = vangers::level::LevelConfig::load(&src_path); let level = vangers::level::load(&config, &geometry); + let pal_owned = layers::extract_palette(&level); + let palette = Some(pal_owned.as_slice()); if let Some(chunks) = matches.opt_get::("c").unwrap() { for i in 0..chunks { let file_name = format!( @@ -162,6 +164,7 @@ fn main() { let export_config = level_obj::Config { xr: 0..level.size.0, yr: i * chunk_y..level.size.1.min((i + 1) * chunk_y), + palette, }; level_obj::save(&dst_path.with_file_name(file_name), &level, &export_config); } @@ -170,6 +173,7 @@ fn main() { let export_config = level_obj::Config { xr: 0..level.size.0, yr: 0..level.size.1, + palette, }; level_obj::save(&dst_path, &level, &export_config); }