From 1d5cc12f4cc12e09832357a9466fac83d49c4a19 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:10:58 +0100 Subject: [PATCH] generate indices for Mikktspace (#8862) # Objective mikktspace tangent generation requires mesh indices, and currently fails when they are not present. we can just generate them instead. ## Solution generate the indices. --- crates/bevy_render/src/mesh/mesh/mod.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index c971538c71085..5830e26cb04df 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -981,7 +981,7 @@ impl RenderAsset for Mesh { } struct MikktspaceGeometryHelper<'a> { - indices: &'a Indices, + indices: Option<&'a Indices>, positions: &'a Vec<[f32; 3]>, normals: &'a Vec<[f32; 3]>, uvs: &'a Vec<[f32; 2]>, @@ -993,15 +993,19 @@ impl MikktspaceGeometryHelper<'_> { let index_index = face * 3 + vert; match self.indices { - Indices::U16(indices) => indices[index_index] as usize, - Indices::U32(indices) => indices[index_index] as usize, + Some(Indices::U16(indices)) => indices[index_index] as usize, + Some(Indices::U32(indices)) => indices[index_index] as usize, + None => index_index, } } } impl bevy_mikktspace::Geometry for MikktspaceGeometryHelper<'_> { fn num_faces(&self) -> usize { - self.indices.len() / 3 + self.indices + .map(Indices::len) + .unwrap_or_else(|| self.positions.len()) + / 3 } fn num_vertices_of_face(&self, _: usize) -> usize { @@ -1080,14 +1084,11 @@ fn generate_tangents_for_mesh(mesh: &Mesh) -> Result, GenerateTang )) } }; - let indices = mesh - .indices() - .ok_or(GenerateTangentsError::MissingIndices)?; let len = positions.len(); let tangents = vec![[0., 0., 0., 0.]; len]; let mut mikktspace_mesh = MikktspaceGeometryHelper { - indices, + indices: mesh.indices(), positions, normals, uvs,