BsFBXImporter.cpp 19 KB

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