Skip to content

Commit

Permalink
generate indices for Mikktspace (#8862)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
robtfm authored Sep 16, 2023
1 parent c61faf3 commit 9d23f82
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions crates/bevy_render/src/mesh/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]>,
Expand All @@ -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 {
Expand Down Expand Up @@ -1080,14 +1084,11 @@ fn generate_tangents_for_mesh(mesh: &Mesh) -> Result<Vec<[f32; 4]>, 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,
Expand Down

0 comments on commit 9d23f82

Please sign in to comment.