diff --git a/crates/graphics/src/common/global_buffers.rs b/crates/graphics/src/common/global_buffers.rs index 3697b59d..03de7f83 100644 --- a/crates/graphics/src/common/global_buffers.rs +++ b/crates/graphics/src/common/global_buffers.rs @@ -82,10 +82,8 @@ impl GlobalBuffers { let mut meshlets = Vec::new(); let mut lod_data = Vec::new(); - let mut starting_child_offset = 0; - mesh_data.meshlets.iter().rev().for_each(|meshlets_data| { + mesh_data.meshlets.iter().for_each(|meshlets_data| { lod_data.push(meshlets_data.len()); - starting_child_offset += meshlets_data.len(); meshlets_data.iter().for_each(|meshlet_data| { let triangle_id = generate_random_uid(); self.triangles_ids @@ -114,7 +112,7 @@ impl GlobalBuffers { .iter() .enumerate() .for_each(|(index, &mi)| { - child_meshlets[index] = (starting_child_offset + mi as usize) as i32; + child_meshlets[index] = mi as i32; }); let meshlet = GPUMeshlet { mesh_index, @@ -149,7 +147,17 @@ impl GlobalBuffers { .unwrap() .allocate(mesh_id, meshlets.as_slice()) .1; - (blas_index, meshlet_range.start, lod_data) + let meshlet_start_index = meshlet_range.start as i32; + self.meshlets.write().unwrap().data_mut()[meshlet_range] + .iter_mut() + .for_each(|m| { + m.child_meshlets.iter_mut().for_each(|v| { + if *v >= 0 { + *v += meshlet_start_index; + } + }); + }); + (blas_index, meshlet_start_index as _, lod_data) } fn add_vertex_data( &self, diff --git a/crates/plugins/binarizer/src/compilers/gltf_compiler.rs b/crates/plugins/binarizer/src/compilers/gltf_compiler.rs index d043e525..e8df2226 100644 --- a/crates/plugins/binarizer/src/compilers/gltf_compiler.rs +++ b/crates/plugins/binarizer/src/compilers/gltf_compiler.rs @@ -368,11 +368,13 @@ impl GltfCompiler { let (mesh_vertices, geometry_indices) = optimize_mesh(&geometry.vertices, &geometry.indices); - let mut meshlet_indices_offset = 0; + let mut mesh_indices_offset = 0; + let mut meshlets_offset = 0; let mut meshlets_per_lod = Vec::new(); let (meshlets, mut mesh_indices) = compute_meshlets(&mesh_vertices, &geometry_indices); + meshlets_offset += meshlets.len(); meshlets_per_lod.push(meshlets); - meshlet_indices_offset += mesh_indices.len(); + mesh_indices_offset += mesh_indices.len(); let mut is_meshlet_tree_created = false; let mut level = 0; @@ -385,13 +387,15 @@ impl GltfCompiler { let (mut cluster_indices, cluster_meshlets) = compute_clusters( &groups, previous_lod_meshlets, - meshlet_indices_offset, + mesh_indices_offset, + meshlets_offset, &mesh_vertices, &mesh_indices, ); - meshlet_indices_offset += cluster_indices.len(); + mesh_indices_offset += cluster_indices.len(); mesh_indices.append(&mut cluster_indices); + meshlets_offset += cluster_meshlets.len(); meshlets_per_lod.push(cluster_meshlets); is_meshlet_tree_created = groups.len() == 1 || level >= (MAX_LOD_LEVELS - 1); diff --git a/crates/plugins/binarizer/src/mesh.rs b/crates/plugins/binarizer/src/mesh.rs index 1fdae4b9..8a755c4a 100644 --- a/crates/plugins/binarizer/src/mesh.rs +++ b/crates/plugins/binarizer/src/mesh.rs @@ -155,8 +155,8 @@ where let vertex_data_adapter = meshopt::VertexDataAdapter::new(vertices_bytes, vertex_stride, 0); let mut new_meshlets = Vec::new(); - let max_vertices = 64; - let max_triangles = 124; + let max_vertices = 128; + let max_triangles = 256; let cone_weight = 0.7; let meshlets = meshopt::build_meshlets( indices, @@ -196,6 +196,7 @@ pub fn compute_clusters( groups: &[Vec], parent_meshlets: &mut [MeshletData], mesh_indices_offset: usize, + meshlets_offset: usize, vertices: &[MeshVertex], indices: &[u32], ) -> (Vec, Vec) { @@ -277,7 +278,7 @@ pub fn compute_clusters( for i in 0..meshlets.len() { meshlet .child_meshlets - .push((cluster_meshlets.len() + i) as u32); + .push((meshlets_offset + cluster_meshlets.len() + i) as u32); } }); cluster_indices.append(&mut global_group_indices);