Skip to content

Commit 48ac955

Browse files
committedMar 31, 2022
Fix loading non-TriangleList meshes without normals in gltf loader (#4376)
# Objective Make it so that loading in a mesh without normals that is not a `TriangleList` succeeds. ## Solution Flat normals can only be calculated on a mesh made of triangles. Check whether the mesh is a `TriangleList` before trying to compute missing normals. ## Additional changes The panic condition in `duplicate_vertices` did not make sense to me. I moved it to `compute_flat_normals` where the algorithm would produce incorrect results if the mesh is not a `TriangleList`. Co-authored-by: devil-ira <justthecooldude@gmail.com>
1 parent aca7fc1 commit 48ac955

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed
 

‎crates/bevy_gltf/src/loader.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ async fn load_gltf<'a, 'b>(
268268
mesh.set_indices(Some(Indices::U32(indices.into_u32().collect())));
269269
};
270270

271-
if mesh.attribute(Mesh::ATTRIBUTE_NORMAL).is_none() {
271+
if mesh.attribute(Mesh::ATTRIBUTE_NORMAL).is_none()
272+
&& matches!(mesh.primitive_topology(), PrimitiveTopology::TriangleList)
273+
{
272274
let vertex_count_before = mesh.count_vertices();
273275
mesh.duplicate_vertices();
274276
mesh.compute_flat_normals();

‎crates/bevy_render/src/mesh/mesh/mod.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,16 @@ impl Mesh {
252252
///
253253
/// This can dramatically increase the vertex count, so make sure this is what you want.
254254
/// Does nothing if no [Indices] are set.
255-
///
256-
/// # Panics
257-
/// If the mesh has any other topology than [`PrimitiveTopology::TriangleList`].
258255
pub fn duplicate_vertices(&mut self) {
259256
fn duplicate<T: Copy>(values: &[T], indices: impl Iterator<Item = usize>) -> Vec<T> {
260257
indices.map(|i| values[i]).collect()
261258
}
262259

263-
assert!(
264-
matches!(self.primitive_topology, PrimitiveTopology::TriangleList),
265-
"can only duplicate vertices for `TriangleList`s"
266-
);
267-
268260
let indices = match self.indices.take() {
269261
Some(indices) => indices,
270262
None => return,
271263
};
264+
272265
for attributes in self.attributes.values_mut() {
273266
let indices = indices.iter();
274267
match &mut attributes.values {
@@ -307,11 +300,17 @@ impl Mesh {
307300
/// Calculates the [`Mesh::ATTRIBUTE_NORMAL`] of a mesh.
308301
///
309302
/// # Panics
310-
/// Panics if [`Indices`] are set or [`Mesh::ATTRIBUTE_POSITION`] is not of type `float3`.
303+
/// Panics if [`Indices`] are set or [`Mesh::ATTRIBUTE_POSITION`] is not of type `float3` or
304+
/// if the mesh has any other topology than [`PrimitiveTopology::TriangleList`].
311305
/// Consider calling [`Mesh::duplicate_vertices`] or export your mesh with normal attributes.
312306
pub fn compute_flat_normals(&mut self) {
313307
assert!(self.indices().is_none(), "`compute_flat_normals` can't work on indexed geometry. Consider calling `Mesh::duplicate_vertices`.");
314308

309+
assert!(
310+
matches!(self.primitive_topology, PrimitiveTopology::TriangleList),
311+
"`compute_flat_normals` can only work on `TriangleList`s"
312+
);
313+
315314
let positions = self
316315
.attribute(Mesh::ATTRIBUTE_POSITION)
317316
.unwrap()

0 commit comments

Comments
 (0)
Please sign in to comment.