Skip to content

Commit

Permalink
remove vertex_colors from Asset3D
Browse files Browse the repository at this point in the history
  • Loading branch information
EtaLoop committed Oct 3, 2024
1 parent 9d8131d commit 36c02cd
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 195 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5320,7 +5320,6 @@ dependencies = [
"re_log",
"re_math",
"re_tracing",
"re_types",
"re_video",
"serde",
"slotmap",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ table Asset3D (

// --- Optional ---

/// An optional color for each vertex.
vertex_colors: [rerun.components.Color] ("attr.rerun.component_optional", nullable, order: 3100);

/// A color multiplier applied to the whole asset.
albedo_factor: rerun.components.AlbedoFactor ("attr.rerun.component_optional", nullable, order: 3200);
albedo_factor: rerun.components.AlbedoFactor ("attr.rerun.component_optional", nullable, order: 3100);
}
52 changes: 6 additions & 46 deletions crates/store/re_types/src/archetypes/asset3d.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/store/re_types/src/archetypes/asset3d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ impl Asset3D {
Self {
blob: contents.into(),
media_type,
vertex_colors: None,
albedo_factor: None,
}
}
Expand Down
5 changes: 0 additions & 5 deletions crates/store/re_types/tests/types/asset3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ fn roundtrip() {
let expected = Asset3D {
blob: Blob(BYTES.to_vec().into()),
media_type: Some(MediaType(Utf8(MediaType::GLTF.into()))),
vertex_colors: Some(vec![
Rgba32::from_unmultiplied_rgba(0xAA, 0x00, 0x00, 0xCC).into(), //
Rgba32::from_unmultiplied_rgba(0x00, 0xBB, 0x00, 0xDD).into(),
]),
albedo_factor: Some(Rgba32::from_unmultiplied_rgba(0xEE, 0x11, 0x22, 0x33).into()),
};

let arch = Asset3D::from_file_contents(BYTES.to_vec(), Some(MediaType::gltf()))
.with_vertex_colors([0xAA0000CC, 0x00BB00DD])
.with_albedo_factor(0xEE112233);
similar_asserts::assert_eq!(expected, arch);

Expand Down
1 change: 0 additions & 1 deletion crates/viewer/re_renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ re_log.workspace = true
re_math.workspace = true
re_tracing.workspace = true
re_video.workspace = true
re_types.workspace = true

ahash.workspace = true
anyhow.workspace = true
Expand Down
30 changes: 13 additions & 17 deletions crates/viewer/re_renderer/src/importer/obj.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use re_types::components::Color;
use smallvec::smallvec;

use crate::{
Expand All @@ -9,8 +8,6 @@ use crate::{
RenderContext, Rgba32Unmul,
};

use super::stl::clamped_vec_or_empty_color;

#[derive(thiserror::Error, Debug)]
pub enum ObjImportError {
#[error(transparent)]
Expand All @@ -25,7 +22,6 @@ pub enum ObjImportError {
pub fn load_obj_from_buffer(
buffer: &[u8],
ctx: &RenderContext,
vertex_colors: &Option<Vec<Color>>,
) -> Result<Vec<MeshInstance>, ObjImportError> {
re_tracing::profile_function!();

Expand Down Expand Up @@ -58,19 +54,19 @@ pub fn load_obj_from_buffer(
.map(|p| glam::uvec3(p[0], p[1], p[2]))
.collect();

let num_positions = vertex_positions.len();

let vertex_colors = if let Some(vertex_colors) = vertex_colors {
let vertex_colors_arr =
clamped_vec_or_empty_color(vertex_colors.as_slice(), vertex_positions.len());
re_tracing::profile_scope!("copy_colors");
vertex_colors_arr
.iter()
.map(|c| Rgba32Unmul::from_rgba_unmul_array(c.to_array()))
.collect()
} else {
vec![Rgba32Unmul::WHITE; num_positions]
};
let mut vertex_colors: Vec<Rgba32Unmul> = mesh
.vertex_color
.chunks_exact(3)
.map(|c| {
Rgba32Unmul::from_rgb(
// It is not specified if the color is in linear or gamma space, but gamma seems a safe bet.
(c[0] * 255.0).round() as u8,
(c[1] * 255.0).round() as u8,
(c[2] * 255.0).round() as u8,
)
})
.collect();
vertex_colors.resize(vertex_positions.len(), Rgba32Unmul::WHITE);

let mut vertex_normals: Vec<glam::Vec3> = mesh
.normals
Expand Down
55 changes: 4 additions & 51 deletions crates/viewer/re_renderer/src/importer/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use tinystl::StlData;
use crate::{
mesh::{self, GpuMesh},
renderer::MeshInstance,
RenderContext, Rgba32Unmul,
RenderContext,
};
use re_types::{archetypes::Asset3D, components::Color};

#[derive(thiserror::Error, Debug)]
pub enum StlImportError {
Expand All @@ -22,20 +21,12 @@ pub enum StlImportError {

/// Load a [STL .stl file](https://en.wikipedia.org/wiki/STL_(file_format)) into the mesh manager.
pub fn load_stl_from_buffer(
asset3d: &Asset3D,
buffer: &[u8],
ctx: &RenderContext,
_texture_key: u64,
) -> Result<Vec<MeshInstance>, StlImportError> {
re_tracing::profile_function!();

let Asset3D {
blob,
vertex_colors,
..
} = asset3d;

let buffer = blob.as_slice();

let cursor = std::io::Cursor::new(buffer);
let StlData {
name,
Expand All @@ -45,20 +36,6 @@ pub fn load_stl_from_buffer(
} = StlData::read_buffer(std::io::BufReader::new(cursor)).map_err(StlImportError::TinyStl)?;

let num_vertices = triangles.len() * 3;
let vertex_positions: &[glam::Vec3] = bytemuck::cast_slice(&triangles);
let num_positions = vertex_positions.len();

let vertex_colors = if let Some(vertex_colors) = vertex_colors {
let vertex_colors_arr =
clamped_vec_or_empty_color(vertex_colors.as_slice(), vertex_positions.len());
re_tracing::profile_scope!("copy_colors");
vertex_colors_arr
.iter()
.map(|c| Rgba32Unmul::from_rgba_unmul_array(c.to_array()))
.collect()
} else {
vec![Rgba32Unmul::WHITE; num_positions]
};

let material = mesh::Material {
label: name.clone().into(),
Expand All @@ -85,8 +62,8 @@ pub fn load_stl_from_buffer(
})
.collect(),

vertex_colors,
// STL has no texcoords.
// 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],
Expand All @@ -99,27 +76,3 @@ pub fn load_stl_from_buffer(
Some(Arc::new(mesh)),
)])
}

pub fn clamped_vec_or_empty_color(values: &[Color], clamped_len: usize) -> Vec<Color> {
if values.len() == clamped_len {
// Happy path
values.to_vec() // TODO(emilk): return a slice reference instead, in a `Cow` or similar
} else if let Some(last) = values.last() {
if values.len() == 1 {
// Commo happy path
return vec![*last; clamped_len];
} else if values.len() < clamped_len {
// Clamp
let mut vec = Vec::with_capacity(clamped_len);
vec.extend(values.iter());
vec.extend(std::iter::repeat(last).take(clamped_len - values.len()));
vec
} else {
// Trim
values.iter().take(clamped_len).copied().collect()
}
} else {
// Empty input
Vec::new()
}
}
2 changes: 1 addition & 1 deletion crates/viewer/re_renderer_examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub fn load_rerun_mesh(re_ctx: &RenderContext) -> Vec<re_renderer::renderer::Mes
let mut zipped_obj = zip.by_name("rerun.obj").unwrap();
let mut obj_data = Vec::new();
std::io::Read::read_to_end(&mut zipped_obj, &mut obj_data).unwrap();
re_renderer::importer::obj::load_obj_from_buffer(&obj_data, re_ctx, &None).unwrap()
re_renderer::importer::obj::load_obj_from_buffer(&obj_data, re_ctx).unwrap()
}

struct WrapApp<E: Example + 'static> {
Expand Down
8 changes: 2 additions & 6 deletions crates/viewer/re_space_view_spatial/src/mesh_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ impl LoadedMesh {
MediaType::GLTF | MediaType::GLB => {
re_renderer::importer::gltf::load_gltf_from_buffer(&name, bytes, render_ctx)?
}
MediaType::OBJ => re_renderer::importer::obj::load_obj_from_buffer(
bytes,
render_ctx,
&asset3d.vertex_colors,
)?,
MediaType::OBJ => re_renderer::importer::obj::load_obj_from_buffer(bytes, render_ctx)?,
MediaType::STL => {
re_renderer::importer::stl::load_stl_from_buffer(asset3d, render_ctx, texture_key)?
re_renderer::importer::stl::load_stl_from_buffer(bytes, render_ctx, texture_key)?
}
_ => anyhow::bail!("{media_type} files are not supported"),
};
Expand Down
Loading

0 comments on commit 36c02cd

Please sign in to comment.