@@ -395,15 +395,31 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh, Mesh mesh, int material
395
395
396
396
FbxLayerElementArray fbxElementArray = fbxLayerElement . GetIndexArray ( ) ;
397
397
398
- // assuming that each polygon is a triangle
399
- // TODO: Add support for other mesh topologies (e.g. quads)
400
- fbxElementArray . SetCount ( mesh . triangles . Length / 3 ) ;
401
-
402
398
for ( int i = 0 ; i < mesh . subMeshCount ; i ++ ) {
403
- int start = ( ( int ) mesh . GetIndexStart ( i ) ) / 3 ;
404
- int count = ( ( int ) mesh . GetIndexCount ( i ) ) / 3 ;
405
- for ( int j = start ; j < start + count ; j ++ ) {
406
- fbxElementArray . SetAt ( j , i ) ;
399
+ var topology = mesh . GetTopology ( i ) ;
400
+ int polySize ;
401
+
402
+ switch ( topology ) {
403
+ case MeshTopology . Quads :
404
+ polySize = 4 ;
405
+ break ;
406
+ case MeshTopology . Lines :
407
+ throw new System . NotImplementedException ( ) ;
408
+ break ;
409
+ case MeshTopology . Points :
410
+ throw new System . NotImplementedException ( ) ;
411
+ break ;
412
+ case MeshTopology . LineStrip :
413
+ throw new System . NotImplementedException ( ) ;
414
+ break ;
415
+ default : /* MeshTopology.Triangles */
416
+ polySize = 3 ;
417
+ break ;
418
+ }
419
+
420
+ var indices = mesh . GetIndices ( i ) ;
421
+ for ( int j = 0 ; j < indices . Length / polySize ; j ++ ) {
422
+ fbxElementArray . Add ( i ) ;
407
423
}
408
424
}
409
425
}
@@ -473,38 +489,63 @@ meshInfo.Vertices [v].z*UnitScaleFactor
473
489
fbxNode . AddMaterial ( fbxMaterial ) ;
474
490
}
475
491
476
- /*
477
- * Triangles have to be added in reverse order,
478
- * or else they will be inverted on import
479
- * (due to the conversion from left to right handed coords)
480
- */
481
- int [ ] unmergedTriangles = new int [ meshInfo . Triangles . Length ] ;
492
+ int [ ] unmergedPolygons = new int [ meshInfo . Triangles . Length ] ;
482
493
int current = 0 ;
483
- for ( int f = 0 ; f < meshInfo . Triangles . Length / 3 ; f ++ ) {
484
- fbxMesh . BeginPolygon ( ) ;
494
+ var mesh = meshInfo . mesh ;
495
+ for ( int s = 0 ; s < mesh . subMeshCount ; s ++ ) {
496
+ var topology = mesh . GetTopology ( s ) ;
497
+ var indices = mesh . GetIndices ( s ) ;
498
+
499
+ int polySize ;
500
+ int [ ] vertOrder ;
501
+
502
+ switch ( topology ) {
503
+ case MeshTopology . Quads :
504
+ polySize = 4 ;
505
+ // vertices have to be reordered as this gets flipped back during import
506
+ vertOrder = new int [ ] { 0 , 3 , 2 , 1 } ;
507
+ break ;
508
+ case MeshTopology . Lines :
509
+ throw new System . NotImplementedException ( ) ;
510
+ break ;
511
+ case MeshTopology . Points :
512
+ throw new System . NotImplementedException ( ) ;
513
+ break ;
514
+ case MeshTopology . LineStrip :
515
+ throw new System . NotImplementedException ( ) ;
516
+ break ;
517
+ default : /* MeshTopology.Triangles */
518
+ polySize = 3 ;
519
+ // triangle vertices have to be reordered to be 0,2,1 instead
520
+ // of 0,1,2, as this gets flipped back during import
521
+ vertOrder = new int [ ] { 0 , 2 , 1 } ;
522
+ break ;
523
+ }
485
524
486
- // triangle vertices have to be reordered to be 0,2,1 instead
487
- // of 0,1,2, as this gets flipped back during import
488
- foreach ( int val in new int [ ] { 0 , 2 , 1 } ) {
489
- int tri = meshInfo . Triangles [ 3 * f + val ] ;
525
+ for ( int f = 0 ; f < indices . Length / polySize ; f ++ ) {
526
+ fbxMesh . BeginPolygon ( ) ;
490
527
491
- // Save the triangle order (without merging vertices) so we
492
- // properly export UVs, normals, binormals, etc.
493
- unmergedTriangles [ current ] = tri ;
528
+ foreach ( int val in vertOrder ) {
529
+ int polyVert = indices [ polySize * f + val ] ;
494
530
495
- if ( weldVertices ) {
496
- tri = ControlPointToIndex [ meshInfo . Vertices [ tri ] ] ;
497
- }
498
- fbxMesh . AddPolygon ( tri ) ;
531
+ // Save the polygon order (without merging vertices) so we
532
+ // properly export UVs, normals, binormals, etc.
533
+ unmergedPolygons [ current ] = polyVert ;
499
534
500
- current ++ ;
535
+ if ( weldVertices ) {
536
+ polyVert = ControlPointToIndex [ meshInfo . Vertices [ polyVert ] ] ;
537
+ }
538
+ fbxMesh . AddPolygon ( polyVert ) ;
539
+
540
+ current ++ ;
541
+ }
542
+ fbxMesh . EndPolygon ( ) ;
501
543
}
502
- fbxMesh . EndPolygon ( ) ;
503
544
}
504
545
505
546
AssignLayerElementMaterial ( fbxMesh , meshInfo . mesh , meshInfo . Materials . Length ) ;
506
547
507
- ExportComponentAttributes ( meshInfo , fbxMesh , unmergedTriangles ) ;
548
+ ExportComponentAttributes ( meshInfo , fbxMesh , unmergedPolygons ) ;
508
549
509
550
// set the fbxNode containing the mesh
510
551
fbxNode . SetNodeAttribute ( fbxMesh ) ;
0 commit comments