Skip to content

Commit

Permalink
add post-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
EtaLoop committed Oct 2, 2024
1 parent 161224d commit a45071a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
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
60 changes: 57 additions & 3 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 @@ -63,12 +69,24 @@ impl LoadedMesh {
_ => 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 +223,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 a45071a

Please sign in to comment.