-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use meshloader to support multiple files
Co-authored-by: Tin Lai <[email protected]>
- Loading branch information
Showing
14 changed files
with
207 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Unreleased | ||
|
||
Renamed the crate from `rapier3d-stl` to `rapier3d-meshloader`, to better reflect its support for multiple formats. | ||
|
||
### Added | ||
|
||
- Add optional support for Collada and Wavefront files through new feature flags `collada` and `wavefront`. | ||
|
||
### Modified | ||
|
||
- Support for STL is now optional through feature `stl`. | ||
- Features `stl`, `wavefront` and `collada` are enabled by default. | ||
|
||
## 0.3.0 | ||
|
||
This is the initial release of the `rapier3d-stl` crate. | ||
|
||
### Added | ||
|
||
- Add `load_from_path` for creating a shape from a stl file. | ||
- Add `load_from_reader` for creating a shape from an object implementing `Read`. | ||
- Add `load_from_raw_mesh` for creating a shape from an already loaded `IndexedMesh`. |
20 changes: 16 additions & 4 deletions
20
crates/rapier3d-stl/Cargo.toml → crates/rapier3d-meshloader/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
[package] | ||
name = "rapier3d-stl" | ||
name = "rapier3d-meshloader" | ||
version = "0.3.0" | ||
authors = ["Sébastien Crozet <[email protected]>"] | ||
description = "STL file loader for the 3D rapier physics engine." | ||
documentation = "https://docs.rs/rapier3d-stl" | ||
documentation = "https://docs.rs/rapier3d-meshloader" | ||
homepage = "https://rapier.rs" | ||
repository = "https://github.com/dimforge/rapier" | ||
readme = "README.md" | ||
categories = ["science", "game-development", "mathematics", "simulation", "wasm"] | ||
categories = [ | ||
"science", | ||
"game-development", | ||
"mathematics", | ||
"simulation", | ||
"wasm", | ||
] | ||
keywords = ["physics", "joints", "multibody", "robotics", "urdf"] | ||
license = "Apache-2.0" | ||
edition = "2021" | ||
|
||
[features] | ||
default = ["stl", "collada", "wavefront"] | ||
stl = ["mesh-loader/stl"] | ||
collada = ["mesh-loader/collada"] | ||
wavefront = ["mesh-loader/obj"] | ||
|
||
[dependencies] | ||
thiserror = "1.0.61" | ||
stl_io = "0.7" | ||
mesh-loader = { version = "0.1.12", optional = true } | ||
|
||
rapier3d = { version = "0.22", path = "../rapier3d" } |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! ## Mesh loader for the Rapier physics engine | ||
//! | ||
//! See documentation from [`mesh_loader`] for supported formats. | ||
//! | ||
//! Rapier is a set of 2D and 3D physics engines for games, animation, and robotics. The `rapier3d-meshloader` | ||
//! crate lets you create a shape compatible with `rapier3d` and `parry3d` (the underlying collision-detection | ||
//! library) from different file formats, see the following features list: | ||
//! `stl`: support .stl files | ||
//! `collada`: support .dae files | ||
//! `wavefront`: support .obj files | ||
|
||
#![deny(missing_docs)] | ||
|
||
use mesh_loader::Mesh; | ||
use rapier3d::geometry::{MeshConverter, SharedShape}; | ||
use rapier3d::math::{Isometry, Point, Real}; | ||
use rapier3d::prelude::MeshConverterError; | ||
use std::path::Path; | ||
|
||
/// The result of loading a shape. | ||
pub struct LoadedShape { | ||
/// The shape loaded from the file and converted by the [`MeshConverter`]. | ||
pub shape: SharedShape, | ||
/// The shape’s pose. | ||
pub pose: Isometry<Real>, | ||
/// The raw mesh read from the file without any modification. | ||
pub raw_mesh: Mesh, | ||
} | ||
|
||
/// Error while loading an STL file. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum MeshLoaderError { | ||
/// An error triggered by rapier’s [`MeshConverter`]. | ||
#[error(transparent)] | ||
MeshConverter(#[from] MeshConverterError), | ||
/// A generic IO error. | ||
#[error(transparent)] | ||
Io(#[from] std::io::Error), | ||
} | ||
|
||
/// Loads an file as shapes from a file. | ||
/// | ||
/// # Parameters | ||
/// - `path`: the file’s path. | ||
/// - `converter`: controls how the shapes are computed from the content. In particular, it lets | ||
/// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, | ||
/// bounding box, etc. | ||
// TODO: call a function for each mesh to load ? To be able to have a different mesh converter? | ||
pub fn load_from_path( | ||
path: impl AsRef<Path>, | ||
converter: &MeshConverter, | ||
) -> Result<Vec<Result<LoadedShape, MeshConverterError>>, MeshLoaderError> { | ||
let loader = mesh_loader::Loader::default(); | ||
let mut colliders = vec![]; | ||
let scene = loader.load(path)?; | ||
for (raw_mesh, _) in scene.meshes.into_iter().zip(scene.materials) { | ||
let shape = load_from_raw_mesh(&raw_mesh, converter); | ||
colliders.push(match shape { | ||
Ok((shape, pose)) => Ok(LoadedShape { | ||
shape, | ||
pose, | ||
raw_mesh, | ||
}), | ||
Err(e) => Err(e), | ||
}); | ||
} | ||
Ok(colliders) | ||
} | ||
|
||
/// Loads an file as a shape from a preloaded raw [`mesh_loader::Mesh`]. | ||
/// | ||
/// # Parameters | ||
/// - `raw_mesh`: the raw mesh. | ||
/// - `converter`: controls how the shape is computed from the STL content. In particular, it lets | ||
/// you specify if the computed [`SharedShape`] is a triangle mesh, its convex hull, | ||
/// bounding box, etc. | ||
pub fn load_from_raw_mesh( | ||
raw_mesh: &Mesh, | ||
converter: &MeshConverter, | ||
) -> Result<(SharedShape, Isometry<Real>), MeshConverterError> { | ||
let vertices: Vec<_> = raw_mesh | ||
.vertices | ||
.iter() | ||
.map(|xyz| Point::new(xyz[0], xyz[1], xyz[2])) | ||
.collect(); | ||
let indices: Vec<_> = raw_mesh.faces.clone(); | ||
converter.convert(vertices, indices) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.