Skip to content

Commit

Permalink
convert: export OBJ materials with the level
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Dec 14, 2023
1 parent e6e925b commit 11c335a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
32 changes: 26 additions & 6 deletions bin/convert/level_obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>,
pub yr: Range<i32>,
pub palette: Option<&'a [u8]>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -158,7 +159,7 @@ struct VertexCollector<'p> {
final_vertices: Vec<[i32; 3]>,
vertex_columns: Vec<Vec<VertexColumn>>,
face_columns: Vec<Vec<FaceColumn>>,
config: &'p Config,
config: &'p Config<'p>,
initial_vertices: usize,
initial_quads: usize,
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {} {} {} {}",
Expand Down
4 changes: 4 additions & 0 deletions bin/convert/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>("c").unwrap() {
for i in 0..chunks {
let file_name = format!(
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 11c335a

Please sign in to comment.