Skip to content

Commit e93813a

Browse files
authored
Merge pull request #60 from Unity-Technologies/UNI-21559-handle-quad-export
UNI-21559 handle export of Quads
2 parents cecdfef + 6e44729 commit e93813a

File tree

1 file changed

+79
-31
lines changed

1 file changed

+79
-31
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,36 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh, Mesh mesh, int material
427427

428428
FbxLayerElementArray fbxElementArray = fbxLayerElement.GetIndexArray ();
429429

430-
// assuming that each polygon is a triangle
431-
// TODO: Add support for other mesh topologies (e.g. quads)
432-
fbxElementArray.SetCount (mesh.triangles.Length / 3);
433-
434-
for (int i = 0; i < mesh.subMeshCount; i++) {
435-
int start = ((int)mesh.GetIndexStart (i)) / 3;
436-
int count = ((int)mesh.GetIndexCount (i)) / 3;
437-
for (int j = start; j < start + count; j++) {
438-
fbxElementArray.SetAt (j, i);
430+
for (int subMeshIndex = 0; subMeshIndex < mesh.subMeshCount; subMeshIndex++) {
431+
var topology = mesh.GetTopology (subMeshIndex);
432+
int polySize;
433+
434+
switch (topology) {
435+
case MeshTopology.Triangles:
436+
polySize = 3;
437+
break;
438+
case MeshTopology.Quads:
439+
polySize = 4;
440+
break;
441+
case MeshTopology.Lines:
442+
throw new System.NotImplementedException();
443+
break;
444+
case MeshTopology.Points:
445+
throw new System.NotImplementedException();
446+
break;
447+
case MeshTopology.LineStrip:
448+
throw new System.NotImplementedException();
449+
break;
450+
default:
451+
throw new System.NotImplementedException ();
452+
break;
453+
}
454+
455+
// Specify the material index for each polygon.
456+
// Material index should match subMeshIndex.
457+
var indices = mesh.GetIndices (subMeshIndex);
458+
for(int j = 0, n = indices.Length / polySize; j < n; j++){
459+
fbxElementArray.Add (subMeshIndex);
439460
}
440461
}
441462
}
@@ -505,38 +526,65 @@ meshInfo.Vertices [v].z*UnitScaleFactor
505526
fbxNode.AddMaterial (fbxMaterial);
506527
}
507528

508-
/*
509-
* Triangles have to be added in reverse order,
510-
* or else they will be inverted on import
511-
* (due to the conversion from left to right handed coords)
512-
*/
513-
int[] unmergedTriangles = new int[meshInfo.Triangles.Length];
529+
int[] unmergedPolygons = new int[meshInfo.Triangles.Length];
514530
int current = 0;
515-
for (int f = 0; f < meshInfo.Triangles.Length / 3; f++) {
516-
fbxMesh.BeginPolygon ();
531+
var mesh = meshInfo.mesh;
532+
for (int s = 0; s < mesh.subMeshCount; s++) {
533+
var topology = mesh.GetTopology (s);
534+
var indices = mesh.GetIndices (s);
535+
536+
int polySize;
537+
int[] vertOrder;
538+
539+
switch (topology) {
540+
case MeshTopology.Triangles:
541+
polySize = 3;
542+
// flip winding order so that Maya and Unity import it properly
543+
vertOrder = new int[]{ 0, 2, 1 };
544+
break;
545+
case MeshTopology.Quads:
546+
polySize = 4;
547+
// flip winding order so that Maya and Unity import it properly
548+
vertOrder = new int[]{ 0, 3, 2, 1 };
549+
break;
550+
case MeshTopology.Lines:
551+
throw new System.NotImplementedException();
552+
break;
553+
case MeshTopology.Points:
554+
throw new System.NotImplementedException();
555+
break;
556+
case MeshTopology.LineStrip:
557+
throw new System.NotImplementedException();
558+
break;
559+
default:
560+
throw new System.NotImplementedException ();
561+
break;
562+
}
517563

518-
// triangle vertices have to be reordered to be 0,2,1 instead
519-
// of 0,1,2, as this gets flipped back during import
520-
foreach (int val in new int[]{0,2,1}) {
521-
int tri = meshInfo.Triangles [3 * f + val];
564+
for (int f = 0; f < indices.Length / polySize; f++) {
565+
fbxMesh.BeginPolygon ();
522566

523-
// Save the triangle order (without merging vertices) so we
524-
// properly export UVs, normals, binormals, etc.
525-
unmergedTriangles [current] = tri;
567+
foreach (int val in vertOrder) {
568+
int polyVert = indices [polySize * f + val];
526569

527-
if (weldVertices) {
528-
tri = ControlPointToIndex [meshInfo.Vertices [tri]];
529-
}
530-
fbxMesh.AddPolygon (tri);
570+
// Save the polygon order (without merging vertices) so we
571+
// properly export UVs, normals, binormals, etc.
572+
unmergedPolygons [current] = polyVert;
531573

532-
current++;
574+
if (weldVertices) {
575+
polyVert = ControlPointToIndex [meshInfo.Vertices [polyVert]];
576+
}
577+
fbxMesh.AddPolygon (polyVert);
578+
579+
current++;
580+
}
581+
fbxMesh.EndPolygon ();
533582
}
534-
fbxMesh.EndPolygon ();
535583
}
536584

537585
AssignLayerElementMaterial (fbxMesh, meshInfo.mesh, meshInfo.Materials.Length);
538586

539-
ExportComponentAttributes (meshInfo, fbxMesh, unmergedTriangles);
587+
ExportComponentAttributes (meshInfo, fbxMesh, unmergedPolygons);
540588

541589
// set the fbxNode containing the mesh
542590
fbxNode.SetNodeAttribute (fbxMesh);

0 commit comments

Comments
 (0)