@@ -1167,16 +1167,34 @@ impl Mesh {
1167
1167
// If there aren't enough indices to make a triangle, then an empty vector will be
1168
1168
// returned.
1169
1169
let iterator = match indices {
1170
- Indices :: U16 ( vec) => FourIterators :: Third (
1171
- vec. as_slice ( )
1172
- . windows ( 3 )
1173
- . flat_map ( move |indices| indices_to_triangle ( vertices, indices) ) ,
1174
- ) ,
1175
- Indices :: U32 ( vec) => FourIterators :: Fourth (
1176
- vec. as_slice ( )
1177
- . windows ( 3 )
1178
- . flat_map ( move |indices| indices_to_triangle ( vertices, indices) ) ,
1179
- ) ,
1170
+ Indices :: U16 ( vec) => {
1171
+ FourIterators :: Third ( vec. as_slice ( ) . windows ( 3 ) . enumerate ( ) . flat_map (
1172
+ move |( i, indices) | {
1173
+ if i % 2 == 0 {
1174
+ indices_to_triangle ( vertices, indices)
1175
+ } else {
1176
+ indices_to_triangle (
1177
+ vertices,
1178
+ & [ indices[ 1 ] , indices[ 0 ] , indices[ 2 ] ] ,
1179
+ )
1180
+ }
1181
+ } ,
1182
+ ) )
1183
+ }
1184
+ Indices :: U32 ( vec) => {
1185
+ FourIterators :: Fourth ( vec. as_slice ( ) . windows ( 3 ) . enumerate ( ) . flat_map (
1186
+ move |( i, indices) | {
1187
+ if i % 2 == 0 {
1188
+ indices_to_triangle ( vertices, indices)
1189
+ } else {
1190
+ indices_to_triangle (
1191
+ vertices,
1192
+ & [ indices[ 1 ] , indices[ 0 ] , indices[ 2 ] ] ,
1193
+ )
1194
+ }
1195
+ } ,
1196
+ ) )
1197
+ }
1180
1198
} ;
1181
1199
1182
1200
return Ok ( iterator) ;
@@ -1214,6 +1232,7 @@ mod tests {
1214
1232
use super :: Mesh ;
1215
1233
use crate :: mesh:: { Indices , MeshWindingInvertError , VertexAttributeValues } ;
1216
1234
use bevy_asset:: RenderAssetUsages ;
1235
+ use bevy_math:: primitives:: Triangle3d ;
1217
1236
use bevy_math:: Vec3 ;
1218
1237
use bevy_transform:: components:: Transform ;
1219
1238
use wgpu:: PrimitiveTopology ;
@@ -1403,4 +1422,81 @@ mod tests {
1403
1422
// 3
1404
1423
assert_eq ! ( [ 1. , 0. , 0. ] , normals[ 3 ] ) ;
1405
1424
}
1425
+
1426
+ #[ test]
1427
+ fn triangles_from_triangle_list ( ) {
1428
+ let mut mesh = Mesh :: new (
1429
+ PrimitiveTopology :: TriangleList ,
1430
+ RenderAssetUsages :: default ( ) ,
1431
+ ) ;
1432
+ mesh. insert_attribute (
1433
+ Mesh :: ATTRIBUTE_POSITION ,
1434
+ vec ! [ [ 0. , 0. , 0. ] , [ 1. , 0. , 0. ] , [ 1. , 1. , 0. ] , [ 0. , 1. , 0. ] ] ,
1435
+ ) ;
1436
+ mesh. insert_indices ( Indices :: U32 ( vec ! [ 0 , 1 , 2 , 2 , 3 , 0 ] ) ) ;
1437
+ assert_eq ! (
1438
+ vec![
1439
+ Triangle3d {
1440
+ vertices: [
1441
+ Vec3 :: new( 0. , 0. , 0. ) ,
1442
+ Vec3 :: new( 1. , 0. , 0. ) ,
1443
+ Vec3 :: new( 1. , 1. , 0. ) ,
1444
+ ]
1445
+ } ,
1446
+ Triangle3d {
1447
+ vertices: [
1448
+ Vec3 :: new( 1. , 1. , 0. ) ,
1449
+ Vec3 :: new( 0. , 1. , 0. ) ,
1450
+ Vec3 :: new( 0. , 0. , 0. ) ,
1451
+ ]
1452
+ }
1453
+ ] ,
1454
+ mesh. triangles( ) . unwrap( ) . collect:: <Vec <Triangle3d >>( )
1455
+ ) ;
1456
+ }
1457
+
1458
+ #[ test]
1459
+ fn triangles_from_triangle_strip ( ) {
1460
+ let mut mesh = Mesh :: new (
1461
+ PrimitiveTopology :: TriangleStrip ,
1462
+ RenderAssetUsages :: default ( ) ,
1463
+ ) ;
1464
+ // Triangles: (0, 1, 2), (2, 1, 3), (2, 3, 4), (4, 3, 5)
1465
+ //
1466
+ // 4 - 5
1467
+ // | \ |
1468
+ // 2 - 3
1469
+ // | \ |
1470
+ // 0 - 1
1471
+ let positions: Vec < Vec3 > = [
1472
+ [ 0. , 0. , 0. ] ,
1473
+ [ 1. , 0. , 0. ] ,
1474
+ [ 0. , 1. , 0. ] ,
1475
+ [ 1. , 1. , 0. ] ,
1476
+ [ 0. , 2. , 0. ] ,
1477
+ [ 1. , 2. , 0. ] ,
1478
+ ]
1479
+ . into_iter ( )
1480
+ . map ( Vec3 :: from_array)
1481
+ . collect ( ) ;
1482
+ mesh. insert_attribute ( Mesh :: ATTRIBUTE_POSITION , positions. clone ( ) ) ;
1483
+ mesh. insert_indices ( Indices :: U32 ( vec ! [ 0 , 1 , 2 , 3 , 4 , 5 ] ) ) ;
1484
+ assert_eq ! (
1485
+ vec![
1486
+ Triangle3d {
1487
+ vertices: [ positions[ 0 ] , positions[ 1 ] , positions[ 2 ] ]
1488
+ } ,
1489
+ Triangle3d {
1490
+ vertices: [ positions[ 2 ] , positions[ 1 ] , positions[ 3 ] ]
1491
+ } ,
1492
+ Triangle3d {
1493
+ vertices: [ positions[ 2 ] , positions[ 3 ] , positions[ 4 ] ]
1494
+ } ,
1495
+ Triangle3d {
1496
+ vertices: [ positions[ 4 ] , positions[ 3 ] , positions[ 5 ] ]
1497
+ } ,
1498
+ ] ,
1499
+ mesh. triangles( ) . unwrap( ) . collect:: <Vec <Triangle3d >>( )
1500
+ ) ;
1501
+ }
1406
1502
}
0 commit comments