Skip to content

Commit

Permalink
Write the materials
Browse files Browse the repository at this point in the history
  • Loading branch information
plule authored and Neo-Zhixing committed May 18, 2023
1 parent 5f79247 commit 43396c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/dot_vox_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub struct DotVoxData {

impl DotVoxData {
/// Serializes `self` in the `.vox` format.
/// TODO: write the material set
pub fn write_vox<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
self.write_header(writer)?;

Expand All @@ -29,6 +28,7 @@ impl DotVoxData {
self.write_models(&mut children_buffer)?;
self.write_scene_graph(&mut children_buffer)?;
self.write_palette_chunk(&mut children_buffer)?;
self.write_materials(&mut children_buffer)?;
let num_main_children_bytes = children_buffer.len() as u32;

self.write_main_chunk(writer, num_main_children_bytes)?;
Expand Down Expand Up @@ -159,6 +159,16 @@ impl DotVoxData {
Self::write_leaf_chunk(writer, "RGBA", &chunk)
}

fn write_materials<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
for material in self.materials.iter() {
let mut chunk = Vec::new();
chunk.extend_from_slice(&material.id.to_le_bytes());
Self::write_dict(&mut chunk, &material.properties);
Self::write_leaf_chunk(writer, "MATL", &chunk)?;
}
Ok(())
}

fn write_leaf_chunk<W: Write>(writer: &mut W, id: &str, chunk: &[u8]) -> Result<(), io::Error> {
let num_children_bytes: u32 = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,10 @@ mod tests {
}

#[test]
fn can_write_vox_format_without_materials() {
fn can_write_vox_format() {
write_and_load(placeholder(
DEFAULT_PALETTE.to_vec(),
Vec::new(),
DEFAULT_MATERIALS.to_vec(),
Vec::new(),
Vec::new(),
));
Expand Down

0 comments on commit 43396c6

Please sign in to comment.