Skip to content

Commit d0af199

Browse files
authored
Add a test for Mesh::triangles and fix for TriangleStrip (bevyengine#16026)
# Objective - Illustrate behavior with a test - Fix a bug revealed by a test ## Solution - Add a test and fix ## Testing Test added.
1 parent 465d113 commit d0af199

File tree

1 file changed

+106
-10
lines changed

1 file changed

+106
-10
lines changed

crates/bevy_mesh/src/mesh.rs

Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,16 +1162,34 @@ impl Mesh {
11621162
// If there aren't enough indices to make a triangle, then an empty vector will be
11631163
// returned.
11641164
let iterator = match indices {
1165-
Indices::U16(vec) => FourIterators::Third(
1166-
vec.as_slice()
1167-
.windows(3)
1168-
.flat_map(move |indices| indices_to_triangle(vertices, indices)),
1169-
),
1170-
Indices::U32(vec) => FourIterators::Fourth(
1171-
vec.as_slice()
1172-
.windows(3)
1173-
.flat_map(move |indices| indices_to_triangle(vertices, indices)),
1174-
),
1165+
Indices::U16(vec) => {
1166+
FourIterators::Third(vec.as_slice().windows(3).enumerate().flat_map(
1167+
move |(i, indices)| {
1168+
if i % 2 == 0 {
1169+
indices_to_triangle(vertices, indices)
1170+
} else {
1171+
indices_to_triangle(
1172+
vertices,
1173+
&[indices[1], indices[0], indices[2]],
1174+
)
1175+
}
1176+
},
1177+
))
1178+
}
1179+
Indices::U32(vec) => {
1180+
FourIterators::Fourth(vec.as_slice().windows(3).enumerate().flat_map(
1181+
move |(i, indices)| {
1182+
if i % 2 == 0 {
1183+
indices_to_triangle(vertices, indices)
1184+
} else {
1185+
indices_to_triangle(
1186+
vertices,
1187+
&[indices[1], indices[0], indices[2]],
1188+
)
1189+
}
1190+
},
1191+
))
1192+
}
11751193
};
11761194

11771195
return Ok(iterator);
@@ -1209,6 +1227,7 @@ mod tests {
12091227
use super::Mesh;
12101228
use crate::mesh::{Indices, MeshWindingInvertError, VertexAttributeValues};
12111229
use bevy_asset::RenderAssetUsages;
1230+
use bevy_math::primitives::Triangle3d;
12121231
use bevy_math::Vec3;
12131232
use bevy_transform::components::Transform;
12141233
use wgpu::PrimitiveTopology;
@@ -1398,4 +1417,81 @@ mod tests {
13981417
// 3
13991418
assert_eq!([1., 0., 0.], normals[3]);
14001419
}
1420+
1421+
#[test]
1422+
fn triangles_from_triangle_list() {
1423+
let mut mesh = Mesh::new(
1424+
PrimitiveTopology::TriangleList,
1425+
RenderAssetUsages::default(),
1426+
);
1427+
mesh.insert_attribute(
1428+
Mesh::ATTRIBUTE_POSITION,
1429+
vec![[0., 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.]],
1430+
);
1431+
mesh.insert_indices(Indices::U32(vec![0, 1, 2, 2, 3, 0]));
1432+
assert_eq!(
1433+
vec![
1434+
Triangle3d {
1435+
vertices: [
1436+
Vec3::new(0., 0., 0.),
1437+
Vec3::new(1., 0., 0.),
1438+
Vec3::new(1., 1., 0.),
1439+
]
1440+
},
1441+
Triangle3d {
1442+
vertices: [
1443+
Vec3::new(1., 1., 0.),
1444+
Vec3::new(0., 1., 0.),
1445+
Vec3::new(0., 0., 0.),
1446+
]
1447+
}
1448+
],
1449+
mesh.triangles().unwrap().collect::<Vec<Triangle3d>>()
1450+
);
1451+
}
1452+
1453+
#[test]
1454+
fn triangles_from_triangle_strip() {
1455+
let mut mesh = Mesh::new(
1456+
PrimitiveTopology::TriangleStrip,
1457+
RenderAssetUsages::default(),
1458+
);
1459+
// Triangles: (0, 1, 2), (2, 1, 3), (2, 3, 4), (4, 3, 5)
1460+
//
1461+
// 4 - 5
1462+
// | \ |
1463+
// 2 - 3
1464+
// | \ |
1465+
// 0 - 1
1466+
let positions: Vec<Vec3> = [
1467+
[0., 0., 0.],
1468+
[1., 0., 0.],
1469+
[0., 1., 0.],
1470+
[1., 1., 0.],
1471+
[0., 2., 0.],
1472+
[1., 2., 0.],
1473+
]
1474+
.into_iter()
1475+
.map(Vec3::from_array)
1476+
.collect();
1477+
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions.clone());
1478+
mesh.insert_indices(Indices::U32(vec![0, 1, 2, 3, 4, 5]));
1479+
assert_eq!(
1480+
vec![
1481+
Triangle3d {
1482+
vertices: [positions[0], positions[1], positions[2]]
1483+
},
1484+
Triangle3d {
1485+
vertices: [positions[2], positions[1], positions[3]]
1486+
},
1487+
Triangle3d {
1488+
vertices: [positions[2], positions[3], positions[4]]
1489+
},
1490+
Triangle3d {
1491+
vertices: [positions[4], positions[3], positions[5]]
1492+
},
1493+
],
1494+
mesh.triangles().unwrap().collect::<Vec<Triangle3d>>()
1495+
);
1496+
}
14011497
}

0 commit comments

Comments
 (0)