66 lines
2.7 KiB
Rust
66 lines
2.7 KiB
Rust
use horizon_assets::{
|
|
bake::{BlockModelTransform, bake_block_model},
|
|
model::CuboidModel,
|
|
resolve::ModelLibrary,
|
|
};
|
|
use std::{collections::BTreeMap, env, fmt::Write, fs, path::PathBuf};
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let models = PathBuf::from("assets/airdef/models/block");
|
|
println!("cargo:rerun-if-changed={}", models.display());
|
|
let output = PathBuf::from(env::var_os("OUT_DIR").expect("Cargo sets OUT_DIR"));
|
|
let mut decoded = BTreeMap::new();
|
|
for entry in fs::read_dir(models)? {
|
|
let path = entry?.path();
|
|
if path.extension().is_some_and(|ext| ext == "json") {
|
|
let name = path
|
|
.file_stem()
|
|
.and_then(|s| s.to_str())
|
|
.expect("UTF-8 model name");
|
|
let model = CuboidModel::from_json(&fs::read_to_string(&path)?)
|
|
.unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
|
decoded.insert(format!("airdef:block/{name}"), model);
|
|
}
|
|
}
|
|
|
|
// Use the same cuboid baker as the client so element rotations, omitted faces and
|
|
// model coordinates match. Include only positions, with offsets for the local AABBs.
|
|
let names: Vec<_> = decoded.keys().cloned().collect();
|
|
let library = ModelLibrary::new(decoded)?;
|
|
let transform = BlockModelTransform::new([0; 3], false);
|
|
let mut bytes = Vec::new();
|
|
let mut table = String::from("&[\n");
|
|
for name in names {
|
|
let model = library.resolve(&name)?;
|
|
let faces = bake_block_model(&model, transform)?;
|
|
if faces.is_empty() {
|
|
continue;
|
|
}
|
|
let start = bytes.len();
|
|
let mut min = [f32::INFINITY; 3];
|
|
let mut max = [f32::NEG_INFINITY; 3];
|
|
for face in faces {
|
|
for position in face.positions {
|
|
for (i, coordinate) in position.into_iter().enumerate() {
|
|
let centred = coordinate - 0.5;
|
|
min[i] = min[i].min(centred);
|
|
max[i] = max[i].max(centred);
|
|
bytes.extend_from_slice(¢red.to_le_bytes());
|
|
}
|
|
}
|
|
}
|
|
let id = name.replace(":block/", ":");
|
|
let extent = std::array::from_fn::<_, 3, _>(|i| min[i].abs().max(max[i].abs()));
|
|
let radius = extent.iter().map(|value| value * value).sum::<f32>().sqrt();
|
|
writeln!(
|
|
table,
|
|
" ({id:?}, Mesh {{ min: {min:?}, max: {max:?}, radius: {radius:?}, start: {start}, end: {} }}),",
|
|
bytes.len()
|
|
)?;
|
|
}
|
|
table.push_str("]\n");
|
|
fs::write(output.join("inspect-meshes.rs"), table)?;
|
|
fs::write(output.join("inspect-faces.bin"), bytes)?;
|
|
Ok(())
|
|
}
|