Skip to content

Commit

Permalink
Implement COLLADA parser
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Apr 7, 2022
1 parent c1152ce commit a2cca45
Show file tree
Hide file tree
Showing 18 changed files with 2,536 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Rust
run: rustup update stable
- name: Install cargo-hack
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
if: github.repository_owner == 'openrr'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Rust
run: rustup update stable
- run: cargo package
Expand Down
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "mesh-loader"
version = "0.1.0"
version = "0.0.1"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/openrr/mesh-loader"
keywords = ["asset", "mesh", "stl"] # TODO: "collada", "obj"
keywords = ["asset", "mesh", "stl", "collada"] # TODO: "obj"
categories = ["parser-implementations", "graphics"]
exclude = ["/.*", "/assets"]
description = """
Expand All @@ -16,15 +16,14 @@ resolver = "2"
members = ["example"]

[features]
default = ["stl"]
default = ["stl", "collada"]

# STL (.stl)
# https://en.wikipedia.org/wiki/STL_(file_format)
stl = ["rustc-hash"]
# TODO
# # COLLADA (.dae)
# # https://en.wikipedia.org/wiki/COLLADA
# collada = []
# COLLADA (.dae)
# https://en.wikipedia.org/wiki/COLLADA
collada = ["indexmap", "roxmltree"]
# TODO
# # Wavefront OBJ (.obj)
# # https://en.wikipedia.org/wiki/Wavefront_.obj_file
Expand All @@ -38,4 +37,8 @@ memchr = "2.4"
# Used in STL parsing.
rustc-hash = { version = "1", optional = true }

# Used in COLLADA parsing.
indexmap = { version = "1.5.2", optional = true, features = ["std"] }
roxmltree = { version = "0.14", optional = true }

[dev-dependencies]
4 changes: 2 additions & 2 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "examples"
name = "example"
version = "0.0.0"
edition = "2021"
publish = false
Expand All @@ -9,6 +9,6 @@ mesh-loader = { path = ".." }

anyhow = "1"
clap = { version = "3", features = ["derive"] }
kiss3d = "0.33"
kiss3d = "0.34"
tracing = { version = "0.1", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["ansi", "env-filter"] }
56 changes: 55 additions & 1 deletion example/examples/kiss3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() -> Result<()> {

let mut base = match path.extension().and_then(OsStr::to_str) {
Some("stl" | "STL") => add_stl(&mut window, path, scale)?,
// Some("dae" | "DAE") => add_collada(&mut window, path, scale)?,
Some("dae" | "DAE") => add_collada(&mut window, path, scale)?,
// Some("obj" | "OBJ") => add_obj(&mut window, path, scale)?,
_ => bail!("unsupported file type {path:?}"),
};
Expand Down Expand Up @@ -97,3 +97,57 @@ fn add_stl(
let mesh = Rc::new(RefCell::new(mesh));
Ok(window.add_mesh(mesh, scale))
}

fn add_collada(
window: &mut Window,
path: impl AsRef<Path>,
scale: na::Vector3<f32>,
) -> Result<SceneNode> {
let path = path.as_ref();
let mut base = window.add_group();
let collada = mesh_loader::collada::from_str(&fs::read_to_string(path)?)?;
for mesh in collada.meshes {
debug!(
"name={},vertices={},normals={},texcoords0={},texcoords1={},faces={}",
mesh.name,
mesh.vertices.len(),
mesh.normals.len(),
mesh.texcoords[0].len(),
mesh.texcoords[1].len(),
mesh.faces.len()
);
let positions = mesh.vertices.iter().map(|&v| na::Point3::from(v)).collect();
let normals = if mesh.normals.is_empty() {
None
} else {
Some(mesh.normals.iter().map(|&v| na::Vector3::from(v)).collect())
};
let texcoords = if mesh.texcoords[0].is_empty() {
None
} else {
Some(
mesh.texcoords[0]
.iter()
.map(|&v| na::Point2::from(v))
.collect(),
)
};
let faces = mesh
.faces
.iter()
.map(|v| na::Point3::new(v[0] as u16, v[1] as u16, v[2] as u16))
.collect();
let mut _scene = base.add_mesh(
Rc::new(RefCell::new(kiss3d::resource::Mesh::new(
positions, faces, normals, texcoords, false,
))),
scale,
);

// TODO(material)
// if let Some(path) = materials.get(0) {
// scene.set_texture_from_file(path, path.to_str().unwrap());
// }
}
Ok(base)
}
Loading

0 comments on commit a2cca45

Please sign in to comment.