BsFBXImporter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. #include "BsFBXImporter.h"
  2. #include "BsResource.h"
  3. #include "BsCoreApplication.h"
  4. #include "BsDebug.h"
  5. #include "BsDataStream.h"
  6. #include "BsMeshData.h"
  7. #include "BsMesh.h"
  8. #include "BsVector2.h"
  9. #include "BsVector3.h"
  10. #include "BsVector4.h"
  11. #include "BsVertexDataDesc.h"
  12. namespace BansheeEngine
  13. {
  14. FBXImporter::FBXImporter()
  15. :SpecificImporter()
  16. {
  17. mExtensions.push_back(L"fbx");
  18. }
  19. FBXImporter::~FBXImporter()
  20. {
  21. }
  22. bool FBXImporter::isExtensionSupported(const WString& ext) const
  23. {
  24. WString lowerCaseExt = ext;
  25. StringUtil::toLowerCase(lowerCaseExt);
  26. return find(mExtensions.begin(), mExtensions.end(), lowerCaseExt) != mExtensions.end();
  27. }
  28. bool FBXImporter::isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const
  29. {
  30. return true; // FBX files can be plain-text so I don't even check for magic number
  31. }
  32. ResourcePtr FBXImporter::import(const Path& filePath, ConstImportOptionsPtr importOptions)
  33. {
  34. FbxManager* fbxManager = nullptr;
  35. FbxScene* fbxScene = nullptr;
  36. startUpSdk(fbxManager, fbxScene);
  37. loadScene(fbxManager, fbxScene, filePath);
  38. Vector<SubMesh> subMeshes;
  39. MeshDataPtr meshData = parseScene(fbxManager, fbxScene, subMeshes);
  40. shutDownSdk(fbxManager);
  41. MeshPtr mesh = Mesh::_createPtr(meshData, subMeshes);
  42. WString fileName = filePath.getWFilename(false);
  43. mesh->setName(fileName);
  44. return mesh;
  45. }
  46. void FBXImporter::startUpSdk(FbxManager*& manager, FbxScene*& scene)
  47. {
  48. // TODO Low priority - Initialize allocator methods for FBX. It calls a lot of heap allocs (200 000 calls for a simple 2k poly mesh) which slows down the import.
  49. // Custom allocator would help a lot.
  50. manager = FbxManager::Create();
  51. if(manager == nullptr)
  52. BS_EXCEPT(InternalErrorException, "FBX SDK failed to initialize. FbxManager::Create() failed.");
  53. FbxIOSettings* ios = FbxIOSettings::Create(manager, IOSROOT);
  54. manager->SetIOSettings(ios);
  55. scene = FbxScene::Create(manager, "Import Scene");
  56. if(scene == nullptr)
  57. BS_EXCEPT(InternalErrorException, "Failed to create FBX scene.");
  58. }
  59. void FBXImporter::shutDownSdk(FbxManager* manager)
  60. {
  61. manager->Destroy();
  62. }
  63. void FBXImporter::loadScene(FbxManager* manager, FbxScene* scene, const Path& filePath)
  64. {
  65. int lFileMajor, lFileMinor, lFileRevision;
  66. int lSDKMajor, lSDKMinor, lSDKRevision;
  67. FbxManager::GetFileFormatVersion(lSDKMajor, lSDKMinor, lSDKRevision);
  68. FbxImporter* importer = FbxImporter::Create(manager, "");
  69. bool importStatus = importer->Initialize(filePath.toString().c_str(), -1, manager->GetIOSettings());
  70. importer->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);
  71. if(!importStatus)
  72. {
  73. BS_EXCEPT(InternalErrorException, "Call to FbxImporter::Initialize() failed.\n" +
  74. String("Error returned: %s\n\n") + String(importer->GetStatus().GetErrorString()));
  75. }
  76. manager->GetIOSettings()->SetBoolProp(IMP_FBX_ANIMATION, false);
  77. manager->GetIOSettings()->SetBoolProp(IMP_FBX_TEXTURE, false);
  78. manager->GetIOSettings()->SetBoolProp(IMP_FBX_LINK, false);
  79. manager->GetIOSettings()->SetBoolProp(IMP_FBX_GOBO, false);
  80. manager->GetIOSettings()->SetBoolProp(IMP_FBX_SHAPE, false);
  81. // TODO - Parse animations
  82. // TODO - Parse blend shapes
  83. importStatus = importer->Import(scene);
  84. if(!importStatus)
  85. {
  86. importer->Destroy();
  87. BS_EXCEPT(InternalErrorException, "Call to FbxImporter::Initialize() failed.\n" +
  88. String("Error returned: %s\n\n") + String(importer->GetStatus().GetErrorString()));
  89. }
  90. importer->Destroy();
  91. }
  92. MeshDataPtr FBXImporter::parseScene(FbxManager* manager, FbxScene* scene, Vector<SubMesh>& subMeshes)
  93. {
  94. Stack<FbxNode*> todo;
  95. todo.push(scene->GetRootNode());
  96. Vector<MeshDataPtr> allMeshes;
  97. Vector<Vector<SubMesh>> allSubMeshes;
  98. while(!todo.empty())
  99. {
  100. FbxNode* curNode = todo.top();
  101. todo.pop();
  102. const char* name = curNode->GetName();
  103. FbxNodeAttribute* attrib = curNode->GetNodeAttribute();
  104. if(attrib != nullptr)
  105. {
  106. FbxNodeAttribute::EType attribType = attrib->GetAttributeType();
  107. switch(attribType)
  108. {
  109. case FbxNodeAttribute::eMesh:
  110. {
  111. FbxMesh* mesh = static_cast<FbxMesh*>(attrib);
  112. mesh->RemoveBadPolygons();
  113. if(!mesh->IsTriangleMesh())
  114. {
  115. FbxGeometryConverter geomConverter(manager);
  116. geomConverter.Triangulate(mesh, true);
  117. attrib = curNode->GetNodeAttribute();
  118. mesh = static_cast<FbxMesh*>(attrib);
  119. }
  120. allSubMeshes.push_back(Vector<SubMesh>());
  121. MeshDataPtr meshData = parseMesh(mesh, allSubMeshes.back());
  122. allMeshes.push_back(meshData);
  123. // TODO - Transform meshes based on node transform
  124. }
  125. break;
  126. case FbxNodeAttribute::eSkeleton:
  127. break; // TODO - I should probably implement skeleton parsing
  128. }
  129. }
  130. for(int i = 0; i < curNode->GetChildCount(); i++)
  131. todo.push(curNode->GetChild(i));
  132. }
  133. if(allMeshes.size() == 0)
  134. return nullptr;
  135. else if(allMeshes.size() == 1)
  136. {
  137. subMeshes = allSubMeshes[0];
  138. return allMeshes[0];
  139. }
  140. else
  141. {
  142. MeshDataPtr combinedMeshData = MeshData::combine(allMeshes, allSubMeshes, subMeshes);
  143. return combinedMeshData;
  144. }
  145. }
  146. MeshDataPtr FBXImporter::parseMesh(FbxMesh* mesh, Vector<SubMesh>& subMeshes, bool createTangentsIfMissing)
  147. {
  148. if (!mesh->GetNode())
  149. {
  150. VertexDataDescPtr tmpVertDesc = bs_shared_ptr<VertexDataDesc>();
  151. return bs_shared_ptr<MeshData, ScratchAlloc>(0, 0, tmpVertDesc);
  152. }
  153. // Find out which vertex attributes exist
  154. bool hasColor = mesh->GetElementVertexColorCount() > 0;
  155. const FbxGeometryElementVertexColor* colorElement = nullptr;
  156. FbxGeometryElement::EMappingMode colorMappingMode = FbxGeometryElement::eNone;
  157. FbxGeometryElement::EReferenceMode colorRefMode = FbxGeometryElement::eDirect;
  158. if(hasColor)
  159. {
  160. colorElement = mesh->GetElementVertexColor(0);
  161. colorMappingMode = colorElement->GetMappingMode();
  162. colorRefMode = colorElement->GetReferenceMode();
  163. if (colorMappingMode == FbxGeometryElement::eNone)
  164. hasColor = false;
  165. }
  166. bool hasNormal = mesh->GetElementNormalCount() > 0;
  167. const FbxGeometryElementNormal* normalElement = nullptr;
  168. FbxGeometryElement::EMappingMode normalMappingMode = FbxGeometryElement::eNone;
  169. FbxGeometryElement::EReferenceMode normalRefMode = FbxGeometryElement::eDirect;
  170. if (hasNormal)
  171. {
  172. normalElement = mesh->GetElementNormal(0);
  173. normalMappingMode = normalElement->GetMappingMode();
  174. normalRefMode = normalElement->GetReferenceMode();
  175. if (normalMappingMode == FbxGeometryElement::eNone)
  176. hasNormal = false;
  177. }
  178. bool hasTangent = mesh->GetElementTangentCount() > 0;
  179. const FbxGeometryElementTangent* tangentElement = nullptr;
  180. FbxGeometryElement::EMappingMode tangentMappingMode = FbxGeometryElement::eNone;
  181. FbxGeometryElement::EReferenceMode tangentRefMode = FbxGeometryElement::eDirect;
  182. if (hasTangent)
  183. {
  184. tangentElement = mesh->GetElementTangent(0);
  185. tangentMappingMode = tangentElement->GetMappingMode();
  186. tangentRefMode = tangentElement->GetReferenceMode();
  187. if (tangentMappingMode == FbxGeometryElement::eNone)
  188. hasTangent = false;
  189. }
  190. bool hasBitangent = mesh->GetElementBinormalCount() > 0;
  191. const FbxGeometryElementBinormal* bitangentElement = nullptr;
  192. FbxGeometryElement::EMappingMode bitangentMappingMode = FbxGeometryElement::eNone;
  193. FbxGeometryElement::EReferenceMode bitangentRefMode = FbxGeometryElement::eDirect;
  194. if (hasBitangent)
  195. {
  196. bitangentElement = mesh->GetElementBinormal(0);
  197. bitangentMappingMode = bitangentElement->GetMappingMode();
  198. bitangentRefMode = bitangentElement->GetReferenceMode();
  199. if (bitangentMappingMode == FbxGeometryElement::eNone)
  200. hasBitangent = false;
  201. }
  202. bool hasUV0 = mesh->GetElementUVCount() > 0;
  203. const FbxGeometryElementUV* UVElement0 = nullptr;
  204. FbxGeometryElement::EMappingMode UVMappingMode0 = FbxGeometryElement::eNone;
  205. FbxGeometryElement::EReferenceMode UVRefMode0 = FbxGeometryElement::eDirect;
  206. if (hasUV0)
  207. {
  208. UVElement0 = mesh->GetElementUV(0);
  209. UVMappingMode0 = UVElement0->GetMappingMode();
  210. UVRefMode0 = UVElement0->GetReferenceMode();
  211. if (UVMappingMode0 == FbxGeometryElement::eNone)
  212. hasUV0 = false;
  213. }
  214. bool hasUV1 = mesh->GetElementUVCount() > 1;
  215. const FbxGeometryElementUV* UVElement1 = nullptr;
  216. FbxGeometryElement::EMappingMode UVMappingMode1 = FbxGeometryElement::eNone;
  217. FbxGeometryElement::EReferenceMode UVRefMode1 = FbxGeometryElement::eDirect;
  218. if (hasUV1)
  219. {
  220. UVElement1 = mesh->GetElementUV(1);
  221. UVMappingMode1 = UVElement1->GetMappingMode();
  222. UVRefMode1 = UVElement1->GetReferenceMode();
  223. if (UVMappingMode1 == FbxGeometryElement::eNone)
  224. hasUV1 = false;
  225. }
  226. // Create tangents if needed
  227. if(createTangentsIfMissing && mesh->GetElementUVCount() > 0)
  228. mesh->GenerateTangentsData(0, false);
  229. // Calculate number of vertices and indexes
  230. int polygonCount = mesh->GetPolygonCount();
  231. UINT32 vertexCount = 0;
  232. VertexDataDescPtr vertexDesc = bs_shared_ptr<VertexDataDesc>();
  233. vertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  234. if(hasColor)
  235. vertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  236. if(hasNormal)
  237. vertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
  238. if(hasTangent)
  239. vertexDesc->addVertElem(VET_FLOAT3, VES_TANGENT);
  240. if(hasBitangent)
  241. vertexDesc->addVertElem(VET_FLOAT3, VES_BITANGENT);
  242. if (hasUV0)
  243. vertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD, 0);
  244. if (hasUV1)
  245. vertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD, 1);
  246. // Count the polygon count of each material
  247. FbxLayerElementArrayTemplate<int>* materialElementArray = NULL;
  248. FbxGeometryElement::EMappingMode materialMappingMode = FbxGeometryElement::eNone;
  249. Set<UINT32> uniqueIndices;
  250. UINT32 numIndices = 0;
  251. if (mesh->GetElementMaterial())
  252. {
  253. materialElementArray = &mesh->GetElementMaterial()->GetIndexArray();
  254. materialMappingMode = mesh->GetElementMaterial()->GetMappingMode();
  255. if (materialElementArray && materialMappingMode == FbxGeometryElement::eByPolygon)
  256. {
  257. FBX_ASSERT(materialElementArray->GetCount() == polygonCount);
  258. if (materialElementArray->GetCount() == polygonCount)
  259. {
  260. // Count the faces of each material
  261. for (int polygonIndex = 0; polygonIndex < polygonCount; ++polygonIndex)
  262. {
  263. const UINT32 materialIndex = (UINT32)materialElementArray->GetAt(polygonIndex);
  264. if (subMeshes.size() < materialIndex + 1)
  265. {
  266. subMeshes.resize(materialIndex + 1);
  267. }
  268. subMeshes[materialIndex].indexCount += 3;
  269. }
  270. // Record the offsets and allocate index arrays
  271. UINT32 materialCount = (UINT32)subMeshes.size();
  272. int offset = 0;
  273. for (UINT32 i = 0; i < materialCount; ++i)
  274. {
  275. subMeshes[i].indexOffset = offset;
  276. offset += subMeshes[i].indexCount;
  277. numIndices += subMeshes[i].indexCount;
  278. }
  279. FBX_ASSERT(offset == polygonCount * 3);
  280. }
  281. }
  282. }
  283. if (subMeshes.size() == 0)
  284. {
  285. numIndices = polygonCount * 3;
  286. subMeshes.resize(1);
  287. subMeshes[0].indexCount = polygonCount * 3;
  288. }
  289. // Count number of unique vertices
  290. for (int polygonIndex = 0; polygonIndex < polygonCount; ++polygonIndex)
  291. {
  292. for (int vertexIndex = 0; vertexIndex < 3; ++vertexIndex)
  293. {
  294. const int controlPointIndex = mesh->GetPolygonVertex(polygonIndex, vertexIndex);
  295. if (uniqueIndices.find(controlPointIndex) == uniqueIndices.end())
  296. {
  297. uniqueIndices.insert(controlPointIndex);
  298. vertexCount++;
  299. }
  300. }
  301. }
  302. // Allocate the array memory for all vertices and indices
  303. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(vertexCount, numIndices, vertexDesc);
  304. VertexElemIter<Vector3> positions = meshData->getVec3DataIter(VES_POSITION);
  305. VertexElemIter<UINT32> colors;
  306. if(hasColor)
  307. colors = meshData->getDWORDDataIter(VES_COLOR);
  308. VertexElemIter<Vector3> normals;
  309. if (hasNormal)
  310. normals = meshData->getVec3DataIter(VES_NORMAL);
  311. VertexElemIter<Vector3> tangents;
  312. if (hasTangent)
  313. tangents = meshData->getVec3DataIter(VES_TANGENT);
  314. VertexElemIter<Vector3> bitangents;
  315. if (hasBitangent)
  316. bitangents = meshData->getVec3DataIter(VES_BITANGENT);
  317. VertexElemIter<Vector2> uv0;
  318. if (hasUV0)
  319. uv0 = meshData->getVec2DataIter(VES_TEXCOORD, 0);
  320. VertexElemIter<Vector2> uv1;
  321. if (hasUV1)
  322. uv1 = meshData->getVec2DataIter(VES_TEXCOORD, 1);
  323. // Get node transform
  324. FbxAMatrix worldTransform;
  325. FbxAMatrix worldTransformIT;
  326. worldTransform = computeWorldTransform(mesh->GetNode());
  327. worldTransformIT = worldTransform.Inverse();
  328. worldTransformIT = worldTransformIT.Transpose();
  329. // Populate the mesh data with vertex attributes and indices
  330. const FbxVector4* controlPoints = mesh->GetControlPoints();
  331. Vector<UINT32> indexOffsetPerSubmesh;
  332. indexOffsetPerSubmesh.resize(subMeshes.size(), 0);
  333. Vector<UINT32*> indices;
  334. indices.resize(subMeshes.size());
  335. for(UINT32 i = 0; i < (UINT32)indices.size(); i++)
  336. {
  337. indices[i] = meshData->getIndices32() + subMeshes[i].indexOffset;
  338. }
  339. Map<UINT32, UINT32> indexMap;
  340. UINT32 curVertexCount = 0;
  341. for (int polygonIndex = 0; polygonIndex < polygonCount; ++polygonIndex)
  342. {
  343. // The material for current face.
  344. int lMaterialIndex = 0;
  345. if (materialElementArray && materialMappingMode == FbxGeometryElement::eByPolygon)
  346. {
  347. lMaterialIndex = materialElementArray->GetAt(polygonIndex);
  348. }
  349. // Where should I save the vertex attribute index, according to the material
  350. int indexOffset = subMeshes[lMaterialIndex].indexOffset + indexOffsetPerSubmesh[lMaterialIndex];
  351. for (int vertexIndex = 0; vertexIndex < 3; ++vertexIndex)
  352. {
  353. int controlPointIndex = mesh->GetPolygonVertex(polygonIndex, vertexIndex);
  354. int triangleIndex = indexOffset + (2 - vertexIndex);
  355. auto findIter = indexMap.find(controlPointIndex);
  356. if (findIter != indexMap.end())
  357. {
  358. indices[lMaterialIndex][triangleIndex] = static_cast<unsigned int>(findIter->second);
  359. }
  360. else
  361. {
  362. indices[lMaterialIndex][triangleIndex] = static_cast<unsigned int>(curVertexCount);
  363. FbxVector4 currentVertex = controlPoints[controlPointIndex];
  364. currentVertex = worldTransform.MultT(currentVertex);
  365. Vector3 curPosValue;
  366. curPosValue[0] = static_cast<float>(currentVertex[0]);
  367. curPosValue[1] = static_cast<float>(currentVertex[1]);
  368. curPosValue[2] = static_cast<float>(currentVertex[2]);
  369. positions.addValue(curPosValue);
  370. indexMap[controlPointIndex] = curVertexCount;
  371. ++curVertexCount;
  372. if (hasColor)
  373. {
  374. int colorIdx = indexOffset + vertexIndex;
  375. if (colorMappingMode == FbxLayerElement::eByControlPoint)
  376. colorIdx = controlPointIndex;
  377. if (colorRefMode == FbxLayerElement::eIndexToDirect)
  378. colorIdx = colorElement->GetIndexArray().GetAt(colorIdx);
  379. FbxColor lCurrentColor = colorElement->GetDirectArray().GetAt(colorIdx);
  380. Color curColorValue;
  381. curColorValue[0] = static_cast<float>(lCurrentColor[0]);
  382. curColorValue[1] = static_cast<float>(lCurrentColor[1]);
  383. curColorValue[2] = static_cast<float>(lCurrentColor[2]);
  384. curColorValue[3] = static_cast<float>(lCurrentColor[3]);
  385. UINT32 color32 = curColorValue.getAsRGBA();
  386. colors.addValue(color32);
  387. }
  388. if (hasNormal)
  389. {
  390. int normalIdx = indexOffset + vertexIndex;
  391. if (normalMappingMode == FbxLayerElement::eByControlPoint)
  392. normalIdx = controlPointIndex;
  393. if (normalRefMode == FbxLayerElement::eIndexToDirect)
  394. normalIdx = normalElement->GetIndexArray().GetAt(normalIdx);
  395. FbxVector4 currentNormal = normalElement->GetDirectArray().GetAt(normalIdx);
  396. currentNormal = worldTransformIT.MultT(currentNormal);
  397. Vector3 curNormalValue;
  398. curNormalValue[0] = static_cast<float>(currentNormal[0]);
  399. curNormalValue[1] = static_cast<float>(currentNormal[1]);
  400. curNormalValue[2] = static_cast<float>(currentNormal[2]);
  401. normals.addValue(curNormalValue);
  402. }
  403. if (hasTangent)
  404. {
  405. int tangentIdx = indexOffset + vertexIndex;
  406. if (tangentMappingMode == FbxLayerElement::eByControlPoint)
  407. tangentIdx = controlPointIndex;
  408. if (tangentRefMode == FbxLayerElement::eIndexToDirect)
  409. tangentIdx = tangentElement->GetIndexArray().GetAt(tangentIdx);
  410. FbxVector4 currentTangent = tangentElement->GetDirectArray().GetAt(tangentIdx);
  411. currentTangent = worldTransformIT.MultT(currentTangent);
  412. Vector3 curTangentValue;
  413. curTangentValue[0] = static_cast<float>(currentTangent[0]);
  414. curTangentValue[1] = static_cast<float>(currentTangent[1]);
  415. curTangentValue[2] = static_cast<float>(currentTangent[2]);
  416. tangents.addValue(curTangentValue);
  417. }
  418. if (hasBitangent)
  419. {
  420. int bitangentIdx = indexOffset + vertexIndex;
  421. if (bitangentMappingMode == FbxLayerElement::eByControlPoint)
  422. bitangentIdx = controlPointIndex;
  423. if (bitangentRefMode == FbxLayerElement::eIndexToDirect)
  424. bitangentIdx = bitangentElement->GetIndexArray().GetAt(bitangentIdx);
  425. FbxVector4 currentBitangent = bitangentElement->GetDirectArray().GetAt(bitangentIdx);
  426. currentBitangent = worldTransformIT.MultT(currentBitangent);
  427. Vector3 curBitangentValue;
  428. curBitangentValue[0] = static_cast<float>(currentBitangent[0]);
  429. curBitangentValue[1] = static_cast<float>(currentBitangent[1]);
  430. curBitangentValue[2] = static_cast<float>(currentBitangent[2]);
  431. bitangents.addValue(curBitangentValue);
  432. }
  433. if (hasUV0)
  434. {
  435. int uv0Idx = indexOffset + vertexIndex;
  436. if (UVMappingMode0 == FbxLayerElement::eByControlPoint)
  437. uv0Idx = controlPointIndex;
  438. if (UVRefMode0 == FbxLayerElement::eIndexToDirect)
  439. uv0Idx = UVElement0->GetIndexArray().GetAt(uv0Idx);
  440. FbxVector4 currentUV = UVElement0->GetDirectArray().GetAt(uv0Idx);
  441. Vector2 curUV0Value;
  442. curUV0Value[0] = static_cast<float>(currentUV[0]);
  443. curUV0Value[1] = 1.0f - static_cast<float>(currentUV[1]);
  444. uv0.addValue(curUV0Value);
  445. }
  446. if (hasUV1)
  447. {
  448. int uv1Idx = indexOffset + vertexIndex;
  449. if (UVMappingMode1 == FbxLayerElement::eByControlPoint)
  450. uv1Idx = controlPointIndex;
  451. if (UVRefMode1 == FbxLayerElement::eIndexToDirect)
  452. uv1Idx = UVElement1->GetIndexArray().GetAt(uv1Idx);
  453. FbxVector4 currentUV = UVElement1->GetDirectArray().GetAt(uv1Idx);
  454. Vector2 curUV1Value;
  455. curUV1Value[0] = static_cast<float>(currentUV[0]);
  456. curUV1Value[1] = 1.0f - static_cast<float>(currentUV[1]);
  457. uv1.addValue(curUV1Value);
  458. }
  459. }
  460. }
  461. indexOffsetPerSubmesh[lMaterialIndex] += 3;
  462. }
  463. return meshData;
  464. }
  465. FbxAMatrix FBXImporter::computeWorldTransform(FbxNode* node)
  466. {
  467. FbxVector4 translation = node->GetGeometricTranslation(FbxNode::eSourcePivot);
  468. FbxVector4 rotation = node->GetGeometricRotation(FbxNode::eSourcePivot);
  469. FbxVector4 scaling = node->GetGeometricScaling(FbxNode::eSourcePivot);
  470. FbxAMatrix localTransform;
  471. localTransform.SetT(translation);
  472. localTransform.SetR(rotation);
  473. localTransform.SetS(scaling);
  474. FbxAMatrix& globalTransform = node->EvaluateGlobalTransform();
  475. FbxAMatrix worldTransform;
  476. worldTransform = globalTransform * localTransform;
  477. return worldTransform;
  478. }
  479. }