Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for parsing rOBJ and rCAM chunks #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target
Cargo.lock
.vscode
.idea/
*.iml
*.iml
.DS_Store
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ nom = { version = "^7", default-features = false, features = ["alloc"] }
[dev-dependencies]
avow = "0.2.0"
env_logger = "^0.9"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
12 changes: 10 additions & 2 deletions src/dot_vox_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{Layer, Material, Model, SceneNode};
use std::io::{self, Write};
use crate::{parser::RenderCamera, Layer, Material, Model, SceneNode};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you re-export RenderCamera in the crate root and use it directly?

use std::{
collections::HashMap,
io::{self, Write},
};
pub type Dict = HashMap<String, String>;

/// Container for .vox file data
#[derive(Debug, PartialEq, Eq)]
Expand All @@ -12,6 +16,10 @@ pub struct DotVoxData {
pub palette: Vec<u32>,
/// A Vec containing all the Materials set
pub materials: Vec<Material>,
/// A Vec containing a collection Render Object dicts
pub render_objects: Vec<Dict>,
// A Vec containing a collection of Render Cameras
pub render_cameras: Vec<RenderCamera>,
/// Scene. The first node in this list is always the root node.
pub scenes: Vec<SceneNode>,
/// Layers. Used by scene transform nodes.
Expand Down
49 changes: 49 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ use std::io::Read;
/// }
/// })
/// .collect(),
/// render_objects: vec![],
/// render_cameras: vec![],
/// scenes: placeholder::SCENES.to_vec(),
/// layers: placeholder::LAYERS.to_vec(),
/// });
Expand Down Expand Up @@ -153,6 +155,8 @@ pub fn load(filename: &str) -> Result<DotVoxData, &'static str> {
/// }
/// })
/// .collect(),
/// render_objects: vec![],
/// render_cameras: vec![],
/// scenes: placeholder::SCENES.to_vec(),
/// layers: placeholder::LAYERS.to_vec(),
/// });
Expand Down Expand Up @@ -218,6 +222,8 @@ pub mod placeholder {
mod tests {
use std::collections::HashMap;

use crate::parser::RenderCamera;

use super::*;
use avow::vec;

Expand All @@ -239,6 +245,27 @@ mod tests {
.collect();
}

lazy_static! {
static ref DEFAULT_RENDER_OBJECTS: Vec<Dict> = serde_json::from_slice::<Vec<Dict>>(
include_bytes!("resources/default_render_objects.json")
)
.unwrap();
}

lazy_static! {
static ref DEFAULT_RENDER_CAMERAS: Vec<RenderCamera> =
serde_json::from_slice::<Vec<(u32, Dict)>>(include_bytes!(
"resources/default_render_cameras.json"
))
.unwrap()
.iter()
.map(|(id, properties)| RenderCamera {
id: *id,
properties: properties.to_owned()
})
.collect();
}

fn placeholder(
palette: Vec<u32>,
materials: Vec<Material>,
Expand Down Expand Up @@ -278,6 +305,8 @@ mod tests {
}],
palette,
materials,
render_objects: vec![],
render_cameras: vec![],
scenes,
layers,
}
Expand Down Expand Up @@ -378,6 +407,26 @@ mod tests {
);
}

#[test]
fn can_parse_vox_file_with_render_objects() {
let bytes = include_bytes!("resources/placeholder-with-render-objects.vox").to_vec();
let result = super::parse_vox_file(&bytes);
assert!(result.is_ok());
let (_, voxel_data) = result.unwrap();

vec::are_eq(voxel_data.render_objects, DEFAULT_RENDER_OBJECTS.to_vec());
}

#[test]
fn can_parse_vox_file_with_render_cameras() {
let bytes = include_bytes!("resources/placeholder-with-render-objects.vox").to_vec();
let result = super::parse_vox_file(&bytes);
assert!(result.is_ok());
let (_, voxel_data) = result.unwrap();

vec::are_eq(voxel_data.render_cameras, DEFAULT_RENDER_CAMERAS.to_vec());
}

fn write_and_load(data: DotVoxData) {
let mut buffer = Vec::new();
let write_result = data.write_vox(&mut buffer);
Expand Down
46 changes: 43 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum Chunk {
Pack(Model),
Palette(Vec<u32>),
Material(Material),
RenderObjects(Dict),
RenderCamera(RenderCamera),
TransformNode(SceneTransform),
GroupNode(SceneGroup),
ShapeNode(SceneShape),
Expand All @@ -41,6 +43,15 @@ pub struct Material {
pub properties: Dict,
}

/// A material used to render this model.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RenderCamera {
/// The Cameras's ID.
pub id: u32,
/// Properties of the camera, mapped by property name.
pub properties: Dict,
}

// TODO: maybe material schemas?
impl Material {
/// The '_type' field, if present
Expand All @@ -56,9 +67,10 @@ impl Material {
pub fn weight(&self) -> Option<f32> {
let w = self.get_f32("_weight");

if let Some(w) = w && (w < 0.0 || w > 1.0)
{
debug!("_weight observed outside of range of [0..1]: {}", w);
if let Some(w) = w {
if w < 0.0 || w > 1.0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to change unrelated code, please do it in a separate PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, not sure if this is still a problem in the latest main branch, will check and see. It was preventing me from compiling at all, but yeah I probably shouldn't have included in the PR :)

debug!("_weight observed outside of range of [0..1]: {}", w);
}
}

w
Expand Down Expand Up @@ -183,6 +195,8 @@ fn map_chunk_to_data(version: u32, main: Chunk) -> DotVoxData {
let mut models: Vec<Model> = vec![];
let mut palette_holder: Vec<u32> = DEFAULT_PALETTE.to_vec();
let mut materials: Vec<Material> = vec![];
let mut render_objects: Vec<Dict> = vec![];
let mut render_cameras: Vec<RenderCamera> = vec![];
let mut scene: Vec<SceneNode> = vec![];
let mut layers: Vec<Layer> = Vec::new();

Expand All @@ -197,6 +211,8 @@ fn map_chunk_to_data(version: u32, main: Chunk) -> DotVoxData {
Chunk::Pack(model) => models.push(model),
Chunk::Palette(palette) => palette_holder = palette,
Chunk::Material(material) => materials.push(material),
Chunk::RenderObjects(dict) => render_objects.push(dict),
Chunk::RenderCamera(camera) => render_cameras.push(camera),
Chunk::TransformNode(scene_transform) => {
scene.push(SceneNode::Transform {
attributes: scene_transform.header.attributes,
Expand Down Expand Up @@ -238,6 +254,8 @@ fn map_chunk_to_data(version: u32, main: Chunk) -> DotVoxData {
models,
palette: palette_holder,
materials,
render_objects,
render_cameras,
scenes: scene,
layers,
}
Expand All @@ -247,6 +265,8 @@ fn map_chunk_to_data(version: u32, main: Chunk) -> DotVoxData {
models: vec![],
palette: vec![],
materials: vec![],
render_objects: vec![],
render_cameras: vec![],
scenes: vec![],
layers: vec![],
},
Expand All @@ -270,6 +290,8 @@ fn build_chunk(id: &str, chunk_content: &[u8], children_size: u32, child_content
"PACK" => build_pack_chunk(chunk_content),
"RGBA" => build_palette_chunk(chunk_content),
"MATL" => build_material_chunk(chunk_content),
"rOBJ" => build_render_objects_chunk(chunk_content),
"rCAM" => build_render_camera_chunk(chunk_content),
"nTRN" => build_scene_transform_chunk(chunk_content),
"nGRP" => build_scene_group_chunk(chunk_content),
"nSHP" => build_scene_shape_chunk(chunk_content),
Expand Down Expand Up @@ -305,6 +327,19 @@ fn build_material_chunk(chunk_content: &[u8]) -> Chunk {
}
Chunk::Invalid(chunk_content.to_vec())
}
fn build_render_camera_chunk(chunk_content: &[u8]) -> Chunk {
if let Ok((_, camera)) = parse_render_camera(chunk_content) {
return Chunk::RenderCamera(camera);
}
Chunk::Invalid(chunk_content.to_vec())
}

fn build_render_objects_chunk(chunk_content: &[u8]) -> Chunk {
if let Ok((_, dict)) = parse_dict(chunk_content) {
return Chunk::RenderObjects(dict);
}
Chunk::Invalid(chunk_content.to_vec())
}

fn build_palette_chunk(chunk_content: &[u8]) -> Chunk {
if let Ok((_, palette)) = palette::extract_palette(chunk_content) {
Expand Down Expand Up @@ -374,6 +409,11 @@ pub fn parse_material(i: &[u8]) -> IResult<&[u8], Material> {
Ok((i, Material { id, properties }))
}

pub fn parse_render_camera(i: &[u8]) -> IResult<&[u8], RenderCamera> {
let (i, (id, properties)) = pair(le_u32, parse_dict)(i)?;
Ok((i, RenderCamera { id, properties }))
}

pub(crate) fn parse_dict(i: &[u8]) -> IResult<&[u8], Dict> {
let (i, n) = le_u32(i)?;

Expand Down
112 changes: 112 additions & 0 deletions src/resources/default_render_cameras.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
[
[
0,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
1,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
2,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
3,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
4,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
5,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
6,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
7,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
8,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
],
[
9,
{
"_angle": "0 0 0",
"_focus": "0 0 0",
"_fov": "45",
"_frustum": "0.414214",
"_mode": "pers",
"_radius": "0"
}
]
]
Loading