-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What Allows to log stl through `Asset3D` or load it directly into the viewer via drag/drop or the CLI https://github.com/rerun-io/rerun/assets/1220815/b3050b4d-8115-47fd-abb2-7f03285d322f To test that the api works I ran `python ./docs/code-examples/all/asset3d_simple.py ../buckle.stl` with [this](https://github.com/rerun-io/rerun/files/14351841/buckle.stl.zip) file. ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using newly built examples: [app.rerun.io](https://app.rerun.io/pr/5244/index.html) * Using examples from latest `main` build: [app.rerun.io](https://app.rerun.io/pr/5244/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [app.rerun.io](https://app.rerun.io/pr/5244/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! - [PR Build Summary](https://build.rerun.io/pr/5244) - [Docs preview](https://rerun.io/preview/648d891e1f1f8ec715aacbb123a71792224e5ac1/docs) <!--DOCS-PREVIEW--> - [Examples preview](https://rerun.io/preview/648d891e1f1f8ec715aacbb123a71792224e5ac1/examples) <!--EXAMPLES-PREVIEW--> - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
- Loading branch information
Showing
23 changed files
with
203 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,80 @@ | ||
use itertools::Itertools; | ||
use smallvec::smallvec; | ||
use tinystl::StlData; | ||
|
||
use crate::{mesh, renderer::MeshInstance, resource_managers::ResourceLifeTime, RenderContext}; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum StlImportError { | ||
#[error("Error loading STL mesh: {0}")] | ||
TinyStl(tinystl::Error), | ||
|
||
#[error(transparent)] | ||
MeshError(#[from] mesh::MeshError), | ||
|
||
#[error(transparent)] | ||
ResourceManagerError(#[from] crate::resource_managers::ResourceManagerError), | ||
} | ||
|
||
/// Load a [STL .stl file](https://en.wikipedia.org/wiki/STL_(file_format)) into the mesh manager. | ||
pub fn load_stl_from_buffer( | ||
buffer: &[u8], | ||
ctx: &RenderContext, | ||
) -> Result<Vec<MeshInstance>, StlImportError> { | ||
re_tracing::profile_function!(); | ||
|
||
let cursor = std::io::Cursor::new(buffer); | ||
let StlData { | ||
name, | ||
triangles, | ||
normals, | ||
.. | ||
} = StlData::read_buffer(std::io::BufReader::new(cursor)).map_err(StlImportError::TinyStl)?; | ||
|
||
let num_vertices = triangles.len() * 3; | ||
|
||
let material = mesh::Material { | ||
label: "default material".into(), | ||
index_range: 0..num_vertices as u32, | ||
albedo: ctx.texture_manager_2d.white_texture_unorm_handle().clone(), | ||
albedo_multiplier: crate::Rgba::WHITE, | ||
}; | ||
|
||
let mesh = mesh::Mesh { | ||
label: name.into(), | ||
triangle_indices: (0..num_vertices as u32) | ||
.tuples::<(_, _, _)>() | ||
.map(glam::UVec3::from) | ||
.collect::<Vec<_>>(), | ||
vertex_positions: bytemuck::cast_slice(&triangles).to_vec(), | ||
|
||
// Normals on STL are per triangle, not per vertex. | ||
// Yes, this makes STL always look faceted. | ||
vertex_normals: normals | ||
.into_iter() | ||
.flat_map(|n| { | ||
let n = glam::Vec3::from_array(n); | ||
[n, n, n] | ||
}) | ||
.collect(), | ||
|
||
// STL has neither colors nor texcoords. | ||
vertex_colors: vec![crate::Rgba32Unmul::WHITE; num_vertices], | ||
vertex_texcoords: vec![glam::Vec2::ZERO; num_vertices], | ||
|
||
materials: smallvec![material], | ||
}; | ||
|
||
mesh.sanity_check()?; | ||
|
||
let gpu_mesh = ctx | ||
.mesh_manager | ||
.write() | ||
.create(ctx, &mesh, ResourceLifeTime::LongLived)?; | ||
|
||
Ok(vec![MeshInstance { | ||
gpu_mesh, | ||
mesh: Some(std::sync::Arc::new(mesh)), | ||
..Default::default() | ||
}]) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,7 @@ | |
"stacklevel", | ||
"startswith", | ||
"staticmethod", | ||
"Stereolithography", | ||
"struct", | ||
"Struct", | ||
"structs", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Oops, something went wrong.