Skip to content

Commit

Permalink
add post-loader albedo_factor
Browse files Browse the repository at this point in the history
- remove `albedo_factor` from loader
  • Loading branch information
EtaLoop committed Oct 2, 2024
1 parent 161224d commit 6b1a7b0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
5 changes: 2 additions & 3 deletions crates/viewer/re_renderer/src/importer/obj.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use re_types::components::{AlbedoFactor, Color};
use re_types::components::Color;
use smallvec::smallvec;

use crate::{
Expand All @@ -26,7 +26,6 @@ pub fn load_obj_from_buffer(
buffer: &[u8],
ctx: &RenderContext,
vertex_colors: &Option<Vec<Color>>,
albedo_factor: &Option<AlbedoFactor>,
) -> Result<Vec<MeshInstance>, ObjImportError> {
re_tracing::profile_function!();

Expand Down Expand Up @@ -102,7 +101,7 @@ pub fn load_obj_from_buffer(
label: "default material".into(),
index_range: 0..mesh.indices.len() as u32,
albedo: texture.clone(),
albedo_factor: albedo_factor.map_or(crate::Rgba::WHITE, |c| c.0.into()),
albedo_factor: crate::Rgba::WHITE,
}],
};

Expand Down
3 changes: 1 addition & 2 deletions crates/viewer/re_renderer/src/importer/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub fn load_stl_from_buffer(
let Asset3D {
blob,
vertex_colors,
albedo_factor,
..
} = asset3d;

Expand Down Expand Up @@ -65,7 +64,7 @@ pub fn load_stl_from_buffer(
label: name.clone().into(),
index_range: 0..num_vertices as u32,
albedo: ctx.texture_manager_2d.white_texture_unorm_handle().clone(),
albedo_factor: albedo_factor.map_or(crate::Rgba::WHITE, |c| c.0.into()),
albedo_factor: crate::Rgba::WHITE,
};

let mesh = mesh::Mesh {
Expand Down
14 changes: 14 additions & 0 deletions crates/viewer/re_renderer/src/renderer/mesh_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ pub struct MeshInstance {
pub picking_layer_id: PickingLayerId,
}

impl Clone for MeshInstance {
#[inline]
fn clone(&self) -> Self {
Self {
gpu_mesh: self.gpu_mesh.clone(),
mesh: self.mesh.clone(),
world_from_mesh: self.world_from_mesh,
additive_tint: self.additive_tint,
outline_mask_ids: self.outline_mask_ids,
picking_layer_id: self.picking_layer_id,
}
}
}

impl MeshInstance {
/// Creates a new instance of a mesh with all fields set to default except for required ones.
pub fn new(gpu_mesh: Arc<GpuMesh>) -> Self {
Expand Down
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, &None).unwrap()
re_renderer::importer::obj::load_obj_from_buffer(&obj_data, re_ctx, &None).unwrap()
}

struct WrapApp<E: Example + 'static> {
Expand Down
61 changes: 57 additions & 4 deletions crates/viewer/re_space_view_spatial/src/mesh_loader.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use itertools::Itertools;
use re_chunk_store::RowId;
use re_renderer::{mesh::GpuMesh, RenderContext, Rgba32Unmul};
use re_renderer::{
mesh::{self, GpuMesh, MeshError},
renderer::MeshInstance,
RenderContext, Rgba32Unmul,
};
use re_types::{
archetypes::{Asset3D, Mesh3D},
components::MediaType,
};
use re_viewer_context::{gpu_bridge::texture_creation_desc_from_color_image, ImageInfo};
use smallvec::smallvec;
use std::sync::Arc;

use crate::mesh_cache::AnyMesh;

Expand Down Expand Up @@ -55,20 +61,31 @@ impl LoadedMesh {
bytes,
render_ctx,
&asset3d.vertex_colors,
&asset3d.albedo_factor,
)?,
MediaType::STL => {
re_renderer::importer::stl::load_stl_from_buffer(asset3d, render_ctx, texture_key)?
}
_ => anyhow::bail!("{media_type} files are not supported"),
};

let bbox = re_renderer::importer::calculate_bounding_box(&mesh_instances);
let prev_mesh_instances = mesh_instances.clone();
let mesh_instances = add_albedo_factor_to_mesh(mesh_instances, asset3d, render_ctx);

if let Ok(mesh_instances) = mesh_instances {
let bbox = re_renderer::importer::calculate_bounding_box(&mesh_instances);

return Ok(Self {
name,
bbox,
mesh_instances,
});
}
let bbox = re_renderer::importer::calculate_bounding_box(&prev_mesh_instances);

Ok(Self {
name,
bbox,
mesh_instances,
mesh_instances: prev_mesh_instances,
})
}

Expand Down Expand Up @@ -205,6 +222,42 @@ impl LoadedMesh {
}
}

fn add_albedo_factor_to_mesh(
mesh_instances: Vec<MeshInstance>,
asset3d: &Asset3D,
render_ctx: &RenderContext,
) -> Result<Vec<MeshInstance>, MeshError> {
if let Some(m) = &mesh_instances[0].mesh {
let material = mesh::Material {
label: m.materials[0].label.clone(),
index_range: std::ops::Range {
start: m.materials[0].index_range.start,
end: m.materials[0].index_range.end,
},
albedo_factor: asset3d
.albedo_factor
.map_or(re_renderer::Rgba::WHITE, |c| c.0.into()),
albedo: m.materials[0].albedo.clone(),
};

let mesh_inst = Arc::clone(m);

let mesh = mesh::Mesh {
materials: smallvec![material],
..(*mesh_inst).clone()
};

mesh.sanity_check()?;

Ok(vec![MeshInstance::new_with_cpu_mesh(
Arc::new(GpuMesh::new(render_ctx, &mesh)?),
Some(Arc::new(mesh)),
)])
} else {
Ok(mesh_instances)
}
}

fn try_get_or_create_albedo_texture(
albedo_texture_buffer: &Option<re_types::components::ImageBuffer>,
albedo_texture_format: &Option<re_types::components::ImageFormat>,
Expand Down

0 comments on commit 6b1a7b0

Please sign in to comment.