@@ -561,6 +561,56 @@ void addMetaData(::fbxsdk::FbxNode* skeletonRootNode, const Character& character
561561 }
562562}
563563
564+ void writePolygonsToFbxMesh (const Mesh& mesh, ::fbxsdk::FbxMesh* lMesh) {
565+ if (!mesh.polyFaces .empty () && !mesh.polyFaceSizes .empty ()) {
566+ // Write original polygon topology
567+ uint32_t offset = 0 ;
568+ for (const auto polySize : mesh.polyFaceSizes ) {
569+ lMesh->BeginPolygon ();
570+ for (uint32_t i = 0 ; i < polySize; ++i) {
571+ lMesh->AddPolygon (mesh.polyFaces [offset + i]);
572+ }
573+ lMesh->EndPolygon ();
574+ offset += polySize;
575+ }
576+ } else {
577+ // Fall back to triangulated faces
578+ for (const auto & face : mesh.faces ) {
579+ lMesh->BeginPolygon ();
580+ for (int i = 0 ; i < 3 ; i++) {
581+ lMesh->AddPolygon (face[i]);
582+ }
583+ lMesh->EndPolygon ();
584+ }
585+ }
586+ }
587+
588+ void writeTextureUVIndicesToFbxMesh (
589+ const Mesh& mesh,
590+ ::fbxsdk::FbxMesh* lMesh,
591+ fbxsdk::FbxLayerElement::EType uvType) {
592+ if (!mesh.polyFaces .empty () && !mesh.polyFaceSizes .empty ()) {
593+ uint32_t offset = 0 ;
594+ int polyIdx = 0 ;
595+ for (const auto polySize : mesh.polyFaceSizes ) {
596+ for (uint32_t i = 0 ; i < polySize; ++i) {
597+ const uint32_t texIdx = mesh.polyTexcoordFaces .empty () ? mesh.polyFaces [offset + i]
598+ : mesh.polyTexcoordFaces [offset + i];
599+ lMesh->SetTextureUVIndex (polyIdx, i, texIdx, uvType);
600+ }
601+ offset += polySize;
602+ polyIdx++;
603+ }
604+ } else {
605+ for (int faceIdx = 0 ; faceIdx < static_cast <int >(mesh.texcoord_faces .size ()); ++faceIdx) {
606+ const auto & texcoords = mesh.texcoord_faces [faceIdx];
607+ lMesh->SetTextureUVIndex (faceIdx, 0 , texcoords[0 ], uvType);
608+ lMesh->SetTextureUVIndex (faceIdx, 1 , texcoords[1 ], uvType);
609+ lMesh->SetTextureUVIndex (faceIdx, 2 , texcoords[2 ], uvType);
610+ }
611+ }
612+ }
613+
564614void saveFbxCommon (
565615 const filesystem::path& filename,
566616 const Character& character,
@@ -687,7 +737,6 @@ void saveFbxCommon(
687737 if (saveMesh && character.mesh != nullptr ) {
688738 // Add the mesh
689739 const int numVertices = character.mesh .get ()->vertices .size ();
690- const int numFaces = character.mesh .get ()->faces .size ();
691740 ::fbxsdk::FbxNode* meshNode = ::fbxsdk::FbxNode::Create (scene, " body_mesh" );
692741 ::fbxsdk::FbxMesh* lMesh = ::fbxsdk::FbxMesh::Create (scene, " mesh" );
693742 lMesh->SetControlPointCount (numVertices);
@@ -704,14 +753,7 @@ void saveFbxCommon(
704753 lMesh->SetControlPointAt (point, normal, i);
705754 }
706755 // Add polygons to lMesh
707- for (int iFace = 0 ; iFace < numFaces; iFace++) {
708- lMesh->BeginPolygon ();
709- for (int i = 0 ; i < 3 ; i++) { // We have tris for models. This could be extended for
710- // supporting Quads or npoly if needed.
711- lMesh->AddPolygon (character.mesh .get ()->faces [iFace][i]);
712- }
713- lMesh->EndPolygon ();
714- }
756+ writePolygonsToFbxMesh (*character.mesh , lMesh);
715757 lMesh->BuildMeshEdgeArray ();
716758 meshNode->SetNodeAttribute (lMesh);
717759
@@ -733,13 +775,8 @@ void saveFbxCommon(
733775 lMesh->AddTextureUV (::fbxsdk::FbxVector2 (texcoords[0 ], 1 .0f - texcoords[1 ]), uvType);
734776 }
735777
736- // Set UV indices for each face. We only have triangles.
737- int faceCount = 0 ;
738- for (const auto & texcoords : character.mesh ->texcoord_faces ) {
739- lMesh->SetTextureUVIndex (faceCount, 0 , texcoords[0 ], uvType);
740- lMesh->SetTextureUVIndex (faceCount, 1 , texcoords[1 ], uvType);
741- lMesh->SetTextureUVIndex (faceCount++, 2 , texcoords[2 ], uvType);
742- }
778+ // Set UV indices for each face.
779+ writeTextureUVIndicesToFbxMesh (*character.mesh , lMesh, uvType);
743780 }
744781
745782 // ---------------------------------------------
0 commit comments