BsFBXImporter.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsFBXImporter.h"
  4. #include "Resources/BsResource.h"
  5. #include "BsCoreApplication.h"
  6. #include "Debug/BsDebug.h"
  7. #include "FileSystem/BsDataStream.h"
  8. #include "Mesh/BsMeshData.h"
  9. #include "Mesh/BsMesh.h"
  10. #include "Math/BsVector2.h"
  11. #include "Math/BsVector3.h"
  12. #include "Math/BsVector4.h"
  13. #include "RenderAPI/BsVertexDataDesc.h"
  14. #include "BsFBXUtility.h"
  15. #include "Mesh/BsMeshUtility.h"
  16. #include "Renderer/BsRendererMeshData.h"
  17. #include "Importer/BsMeshImportOptions.h"
  18. #include "Physics/BsPhysicsMesh.h"
  19. #include "Animation/BsAnimationCurve.h"
  20. #include "Animation/BsAnimationClip.h"
  21. #include "Animation/BsAnimationUtility.h"
  22. #include "Animation/BsSkeleton.h"
  23. #include "Animation/BsMorphShapes.h"
  24. #include "Physics/BsPhysics.h"
  25. namespace bs
  26. {
  27. Matrix4 FBXToNativeType(const FbxAMatrix& value)
  28. {
  29. Matrix4 native;
  30. for (UINT32 row = 0; row < 4; row++)
  31. for (UINT32 col = 0; col < 4; col++)
  32. native[row][col] = (float)value[col][row];
  33. return native;
  34. }
  35. Vector3 FBXToNativeType(const FbxVector4& value)
  36. {
  37. Vector3 native;
  38. native.x = (float)value[0];
  39. native.y = (float)value[1];
  40. native.z = (float)value[2];
  41. return native;
  42. }
  43. Vector3 FBXToNativeType(const FbxDouble3& value)
  44. {
  45. Vector3 native;
  46. native.x = (float)value[0];
  47. native.y = (float)value[1];
  48. native.z = (float)value[2];
  49. return native;
  50. }
  51. Vector2 FBXToNativeType(const FbxVector2& value)
  52. {
  53. Vector2 native;
  54. native.x = (float)value[0];
  55. native.y = (float)value[1];
  56. return native;
  57. }
  58. RGBA FBXToNativeType(const FbxColor& value)
  59. {
  60. Color native;
  61. native.r = (float)value[0];
  62. native.g = (float)value[1];
  63. native.b = (float)value[2];
  64. native.a = (float)value[3];
  65. return native.getAsRGBA();
  66. }
  67. FbxSurfaceMaterial* FBXToNativeType(FbxSurfaceMaterial* const& value)
  68. {
  69. return value;
  70. }
  71. int FBXToNativeType(const int & value)
  72. {
  73. return value;
  74. }
  75. FBXImporter::FBXImporter()
  76. :SpecificImporter(), mFBXManager(nullptr)
  77. {
  78. mExtensions.push_back(L"fbx");
  79. mExtensions.push_back(L"obj");
  80. mExtensions.push_back(L"dae");
  81. }
  82. FBXImporter::~FBXImporter()
  83. {
  84. }
  85. bool FBXImporter::isExtensionSupported(const WString& ext) const
  86. {
  87. WString lowerCaseExt = ext;
  88. StringUtil::toLowerCase(lowerCaseExt);
  89. return find(mExtensions.begin(), mExtensions.end(), lowerCaseExt) != mExtensions.end();
  90. }
  91. bool FBXImporter::isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const
  92. {
  93. return true; // FBX files can be plain-text so I don't even check for magic number
  94. }
  95. SPtr<ImportOptions> FBXImporter::createImportOptions() const
  96. {
  97. return bs_shared_ptr_new<MeshImportOptions>();
  98. }
  99. SPtr<Resource> FBXImporter::import(const Path& filePath, SPtr<const ImportOptions> importOptions)
  100. {
  101. MESH_DESC desc;
  102. Vector<FBXAnimationClipData> dummy;
  103. SPtr<RendererMeshData> rendererMeshData = importMeshData(filePath, importOptions, desc.subMeshes, dummy,
  104. desc.skeleton, desc.morphShapes);
  105. const MeshImportOptions* meshImportOptions = static_cast<const MeshImportOptions*>(importOptions.get());
  106. desc.usage = MU_STATIC;
  107. if (meshImportOptions->getCPUCached())
  108. desc.usage |= MU_CPUCACHED;
  109. SPtr<Mesh> mesh = Mesh::_createPtr(rendererMeshData->getData(), desc);
  110. WString fileName = filePath.getWFilename(false);
  111. mesh->setName(fileName);
  112. return mesh;
  113. }
  114. Vector<SubResourceRaw> FBXImporter::importAll(const Path& filePath, SPtr<const ImportOptions> importOptions)
  115. {
  116. MESH_DESC desc;
  117. Vector<FBXAnimationClipData> animationClips;
  118. SPtr<RendererMeshData> rendererMeshData = importMeshData(filePath, importOptions, desc.subMeshes, animationClips,
  119. desc.skeleton, desc.morphShapes);
  120. const MeshImportOptions* meshImportOptions = static_cast<const MeshImportOptions*>(importOptions.get());
  121. desc.usage = MU_STATIC;
  122. if (meshImportOptions->getCPUCached())
  123. desc.usage |= MU_CPUCACHED;
  124. SPtr<Mesh> mesh = Mesh::_createPtr(rendererMeshData->getData(), desc);
  125. WString fileName = filePath.getWFilename(false);
  126. mesh->setName(fileName);
  127. Vector<SubResourceRaw> output;
  128. if(mesh != nullptr)
  129. {
  130. output.push_back({ L"primary", mesh });
  131. CollisionMeshType collisionMeshType = meshImportOptions->getCollisionMeshType();
  132. if(collisionMeshType != CollisionMeshType::None)
  133. {
  134. if(Physics::isStarted())
  135. {
  136. PhysicsMeshType type = collisionMeshType == CollisionMeshType::Convex ?
  137. PhysicsMeshType::Convex : PhysicsMeshType::Triangle;
  138. SPtr<PhysicsMesh> physicsMesh = PhysicsMesh::_createPtr(rendererMeshData->getData(), type);
  139. output.push_back({ L"collision", physicsMesh });
  140. }
  141. else
  142. {
  143. LOGWRN("Cannot generate a collision mesh as the physics module was not started.");
  144. }
  145. }
  146. Vector<ImportedAnimationEvents> events = meshImportOptions->getAnimationEvents();
  147. for(auto& entry : animationClips)
  148. {
  149. SPtr<AnimationClip> clip = AnimationClip::_createPtr(entry.curves, entry.isAdditive, entry.sampleRate,
  150. entry.rootMotion);
  151. for(auto& eventsEntry : events)
  152. {
  153. if(entry.name == eventsEntry.name)
  154. {
  155. clip->setEvents(eventsEntry.events);
  156. break;
  157. }
  158. }
  159. output.push_back({ toWString(entry.name), clip });
  160. }
  161. }
  162. return output;
  163. }
  164. SPtr<RendererMeshData> FBXImporter::importMeshData(const Path& filePath, SPtr<const ImportOptions> importOptions,
  165. Vector<SubMesh>& subMeshes, Vector<FBXAnimationClipData>& animation, SPtr<Skeleton>& skeleton,
  166. SPtr<MorphShapes>& morphShapes)
  167. {
  168. FbxScene* fbxScene = nullptr;
  169. if (!startUpSdk(fbxScene))
  170. return nullptr;
  171. if (!loadFBXFile(fbxScene, filePath))
  172. return nullptr;
  173. const MeshImportOptions* meshImportOptions = static_cast<const MeshImportOptions*>(importOptions.get());
  174. FBXImportOptions fbxImportOptions;
  175. fbxImportOptions.importNormals = meshImportOptions->getImportNormals();
  176. fbxImportOptions.importTangents = meshImportOptions->getImportTangents();
  177. fbxImportOptions.importAnimation = meshImportOptions->getImportAnimation();
  178. fbxImportOptions.importBlendShapes = meshImportOptions->getImportBlendShapes();
  179. fbxImportOptions.importSkin = meshImportOptions->getImportSkin();
  180. fbxImportOptions.importScale = meshImportOptions->getImportScale();
  181. FBXImportScene importedScene;
  182. bakeTransforms(fbxScene);
  183. parseScene(fbxScene, fbxImportOptions, importedScene);
  184. if (fbxImportOptions.importBlendShapes)
  185. importBlendShapes(importedScene, fbxImportOptions);
  186. if (fbxImportOptions.importSkin)
  187. importSkin(importedScene, fbxImportOptions);
  188. if (fbxImportOptions.importAnimation)
  189. importAnimations(fbxScene, fbxImportOptions, importedScene);
  190. splitMeshVertices(importedScene);
  191. generateMissingTangentSpace(importedScene, fbxImportOptions);
  192. SPtr<RendererMeshData> rendererMeshData = generateMeshData(importedScene, fbxImportOptions, subMeshes);
  193. skeleton = createSkeleton(importedScene, subMeshes.size() > 1);
  194. morphShapes = createMorphShapes(importedScene);
  195. // Import animation clips
  196. if (!importedScene.clips.empty())
  197. {
  198. Vector<AnimationSplitInfo> splits = meshImportOptions->getAnimationClipSplits();
  199. convertAnimations(importedScene.clips, splits, skeleton, meshImportOptions->getImportRootMotion(), animation);
  200. }
  201. // TODO - Later: Optimize mesh: Remove bad and degenerate polygons, weld nearby vertices, optimize for vertex cache
  202. shutDownSdk();
  203. return rendererMeshData;
  204. }
  205. SPtr<Skeleton> FBXImporter::createSkeleton(const FBXImportScene& scene, bool sharedRoot)
  206. {
  207. Vector<BONE_DESC> allBones;
  208. UnorderedMap<FBXImportNode*, UINT32> boneMap;
  209. for (auto& mesh : scene.meshes)
  210. {
  211. // Create bones
  212. UINT32 numBones = (UINT32)mesh->bones.size();
  213. for (auto& fbxBone : mesh->bones)
  214. {
  215. UINT32 boneIdx = (UINT32)allBones.size();
  216. boneMap[fbxBone.node] = boneIdx;
  217. allBones.push_back(BONE_DESC());
  218. BONE_DESC& bone = allBones.back();
  219. bone.name = fbxBone.node->name;
  220. bone.invBindPose = fbxBone.bindPose;
  221. }
  222. }
  223. // Generate skeleton
  224. if (allBones.size() > 0)
  225. {
  226. // Find bone parents
  227. UINT32 numProcessedBones = 0;
  228. // Generate common root bone for all meshes
  229. UINT32 rootBoneIdx = (UINT32)-1;
  230. if (sharedRoot)
  231. {
  232. rootBoneIdx = (UINT32)allBones.size();
  233. allBones.push_back(BONE_DESC());
  234. BONE_DESC& bone = allBones.back();
  235. bone.name = "MultiMeshRoot";
  236. bone.invBindPose = Matrix4::IDENTITY;
  237. bone.parent = (UINT32)-1;
  238. numProcessedBones++;
  239. }
  240. Stack<std::pair<FBXImportNode*, UINT32>> todo;
  241. todo.push({ scene.rootNode, rootBoneIdx });
  242. while (!todo.empty())
  243. {
  244. auto entry = todo.top();
  245. todo.pop();
  246. FBXImportNode* node = entry.first;
  247. UINT32 parentBoneIdx = entry.second;
  248. auto boneIter = boneMap.find(node);
  249. if (boneIter != boneMap.end())
  250. {
  251. UINT32 boneIdx = boneIter->second;
  252. allBones[boneIdx].parent = parentBoneIdx;
  253. numProcessedBones++;
  254. parentBoneIdx = boneIdx;
  255. }
  256. for (auto& child : node->children)
  257. todo.push({ child, parentBoneIdx });
  258. }
  259. UINT32 numAllBones = (UINT32)allBones.size();
  260. if (numProcessedBones == numAllBones)
  261. return Skeleton::create(allBones.data(), numAllBones);
  262. LOGERR("Not all bones were found in the node hierarchy. Skeleton invalid.");
  263. }
  264. return nullptr;
  265. }
  266. SPtr<MorphShapes> FBXImporter::createMorphShapes(const FBXImportScene& scene)
  267. {
  268. // Combine morph shapes from all sub-meshes, and transform them
  269. struct RawMorphShape
  270. {
  271. String name;
  272. float weight;
  273. Vector<MorphVertex> vertices;
  274. };
  275. UnorderedMap<String, UnorderedMap<String, RawMorphShape>> allRawMorphShapes;
  276. UINT32 totalNumVertices = 0;
  277. // Note: Order in which we combine meshes must match the order in MeshData::combine
  278. for (auto& mesh : scene.meshes)
  279. {
  280. UINT32 numVertices = (UINT32)mesh->positions.size();
  281. UINT32 numNormals = (UINT32)mesh->normals.size();
  282. bool hasNormals = numVertices == numNormals;
  283. for (auto& node : mesh->referencedBy)
  284. {
  285. Matrix4 worldTransform = scene.globalScale * node->worldTransform * node->geomTransform;
  286. Matrix4 worldTransformIT = worldTransform.inverse();
  287. worldTransformIT = worldTransformIT.transpose();
  288. // Copy & transform positions
  289. for(auto& blendShape : mesh->blendShapes)
  290. {
  291. UnorderedMap<String, RawMorphShape>& channelShapes = allRawMorphShapes[blendShape.name];
  292. for(auto& blendFrame : blendShape.frames)
  293. {
  294. RawMorphShape& shape = channelShapes[blendFrame.name];
  295. shape.name = blendFrame.name;
  296. shape.weight = blendFrame.weight;
  297. UINT32 frameNumVertices = (UINT32)blendFrame.positions.size();
  298. if (frameNumVertices == numVertices)
  299. {
  300. for (UINT32 i = 0; i < numVertices; i++)
  301. {
  302. Vector3 meshPosition = worldTransform.multiplyAffine(mesh->positions[i]);
  303. Vector3 blendPosition = worldTransform.multiplyAffine(blendFrame.positions[i]);
  304. Vector3 positionDelta = blendPosition - meshPosition;
  305. Vector3 normalDelta;
  306. if (hasNormals)
  307. {
  308. Vector3 blendNormal = worldTransformIT.multiplyDirection(blendFrame.normals[i]);
  309. blendNormal = Vector3::normalize(blendNormal);
  310. Vector3 meshNormal = worldTransformIT.multiplyDirection(mesh->normals[i]);
  311. meshNormal = Vector3::normalize(meshNormal);
  312. normalDelta = blendNormal - meshNormal;
  313. }
  314. else
  315. normalDelta = Vector3::ZERO;
  316. if (positionDelta.squaredLength() > 0.000001f || normalDelta.squaredLength() > 0.0001f)
  317. shape.vertices.push_back(MorphVertex(positionDelta, normalDelta, totalNumVertices + i));
  318. }
  319. }
  320. else
  321. {
  322. LOGERR("Corrupt blend shape frame. Number of vertices doesn't match the number of mesh vertices.");
  323. }
  324. }
  325. }
  326. totalNumVertices += numVertices;
  327. }
  328. }
  329. // Create morph shape object from combined shape data
  330. SPtr<MorphShapes> morphShapes;
  331. Vector<SPtr<MorphChannel>> allChannels;
  332. for (auto& channel : allRawMorphShapes)
  333. {
  334. Vector<SPtr<MorphShape>> channelShapes;
  335. for (auto& entry : channel.second)
  336. {
  337. RawMorphShape& shape = entry.second;
  338. shape.vertices.shrink_to_fit();
  339. SPtr<MorphShape> morphShape = MorphShape::create(shape.name, shape.weight, shape.vertices);
  340. channelShapes.push_back(morphShape);
  341. }
  342. if(channelShapes.size() > 0)
  343. {
  344. SPtr<MorphChannel> morphChannel = MorphChannel::create(channel.first, channelShapes);
  345. allChannels.push_back(morphChannel);
  346. }
  347. }
  348. if (!allChannels.empty())
  349. return MorphShapes::create(allChannels, totalNumVertices);
  350. return morphShapes;
  351. }
  352. bool FBXImporter::startUpSdk(FbxScene*& scene)
  353. {
  354. mFBXManager = FbxManager::Create();
  355. if (mFBXManager == nullptr)
  356. {
  357. LOGERR("FBX import failed: FBX SDK failed to initialize. FbxManager::Create() failed.");
  358. return false;
  359. }
  360. FbxIOSettings* ios = FbxIOSettings::Create(mFBXManager, IOSROOT);
  361. mFBXManager->SetIOSettings(ios);
  362. scene = FbxScene::Create(mFBXManager, "Import Scene");
  363. if (scene == nullptr)
  364. {
  365. LOGWRN("FBX import failed: Failed to create FBX scene.");
  366. return false;
  367. }
  368. return true;
  369. }
  370. void FBXImporter::shutDownSdk()
  371. {
  372. mFBXManager->Destroy();
  373. mFBXManager = nullptr;
  374. }
  375. bool FBXImporter::loadFBXFile(FbxScene* scene, const Path& filePath)
  376. {
  377. int lFileMajor, lFileMinor, lFileRevision;
  378. int lSDKMajor, lSDKMinor, lSDKRevision;
  379. FbxManager::GetFileFormatVersion(lSDKMajor, lSDKMinor, lSDKRevision);
  380. FbxImporter* importer = FbxImporter::Create(mFBXManager, "");
  381. bool importStatus = importer->Initialize(filePath.toString().c_str(), -1, mFBXManager->GetIOSettings());
  382. importer->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);
  383. if(!importStatus)
  384. {
  385. LOGERR("FBX import failed: Call to FbxImporter::Initialize() failed.\n" +
  386. String("Error returned: %s\n\n") + String(importer->GetStatus().GetErrorString()));
  387. return false;
  388. }
  389. mFBXManager->GetIOSettings()->SetBoolProp(IMP_FBX_TEXTURE, false);
  390. mFBXManager->GetIOSettings()->SetBoolProp(IMP_FBX_GOBO, false);
  391. importStatus = importer->Import(scene);
  392. if(!importStatus)
  393. {
  394. importer->Destroy();
  395. LOGERR("FBX import failed: Call to FbxImporter::Import() failed.\n" +
  396. String("Error returned: %s\n\n") + String(importer->GetStatus().GetErrorString()));
  397. return false;
  398. }
  399. FbxAxisSystem fileCoordSystem = scene->GetGlobalSettings().GetAxisSystem();
  400. FbxAxisSystem bsCoordSystem(FbxAxisSystem::eYAxis, FbxAxisSystem::eParityOdd, FbxAxisSystem::eRightHanded);
  401. if (fileCoordSystem != bsCoordSystem)
  402. bsCoordSystem.ConvertScene(scene);
  403. importer->Destroy();
  404. return true;
  405. }
  406. void FBXImporter::parseScene(FbxScene* scene, const FBXImportOptions& options, FBXImportScene& outputScene)
  407. {
  408. // Scale from file units to engine units, and apply optional user scale
  409. float importScale = 1.0f;
  410. if (options.importScale > 0.0001f)
  411. importScale = options.importScale;
  412. FbxSystemUnit units = scene->GetGlobalSettings().GetSystemUnit();
  413. FbxSystemUnit bsScaledUnits(100.0f);
  414. outputScene.scaleFactor = (float)units.GetConversionFactorTo(bsScaledUnits) * importScale;
  415. outputScene.globalScale = Matrix4::scaling(outputScene.scaleFactor);
  416. outputScene.rootNode = createImportNode(outputScene, scene->GetRootNode(), nullptr);
  417. Stack<FbxNode*> todo;
  418. todo.push(scene->GetRootNode());
  419. while(!todo.empty())
  420. {
  421. FbxNode* curNode = todo.top();
  422. FBXImportNode* curImportNode = outputScene.nodeMap[curNode];
  423. todo.pop();
  424. FbxNodeAttribute* attrib = curNode->GetNodeAttribute();
  425. if(attrib != nullptr)
  426. {
  427. FbxNodeAttribute::EType attribType = attrib->GetAttributeType();
  428. switch(attribType)
  429. {
  430. case FbxNodeAttribute::eNurbs:
  431. case FbxNodeAttribute::eNurbsSurface:
  432. case FbxNodeAttribute::ePatch:
  433. {
  434. FbxGeometryConverter geomConverter(mFBXManager);
  435. attrib = geomConverter.Triangulate(attrib, true);
  436. if (attrib->GetAttributeType() == FbxNodeAttribute::eMesh)
  437. {
  438. FbxMesh* mesh = static_cast<FbxMesh*>(attrib);
  439. mesh->RemoveBadPolygons();
  440. parseMesh(mesh, curImportNode, options, outputScene);
  441. }
  442. }
  443. break;
  444. case FbxNodeAttribute::eMesh:
  445. {
  446. FbxMesh* mesh = static_cast<FbxMesh*>(attrib);
  447. mesh->RemoveBadPolygons();
  448. if(!mesh->IsTriangleMesh())
  449. {
  450. FbxGeometryConverter geomConverter(mFBXManager);
  451. geomConverter.Triangulate(mesh, true);
  452. attrib = curNode->GetNodeAttribute();
  453. mesh = static_cast<FbxMesh*>(attrib);
  454. }
  455. parseMesh(mesh, curImportNode, options, outputScene);
  456. }
  457. break;
  458. default:
  459. break;
  460. }
  461. }
  462. for (int i = 0; i < curNode->GetChildCount(); i++)
  463. {
  464. FbxNode* childNode = curNode->GetChild(i);
  465. createImportNode(outputScene, childNode, curImportNode);
  466. todo.push(childNode);
  467. }
  468. }
  469. }
  470. FBXImportNode* FBXImporter::createImportNode(FBXImportScene& scene, FbxNode* fbxNode, FBXImportNode* parent)
  471. {
  472. FBXImportNode* node = bs_new<FBXImportNode>();
  473. Vector3 translation = FBXToNativeType(fbxNode->EvaluateLocalTranslation(FbxTime(0)));
  474. Vector3 rotationEuler = FBXToNativeType(fbxNode->EvaluateLocalRotation(FbxTime(0)));
  475. Vector3 scale = FBXToNativeType(fbxNode->EvaluateLocalScaling(FbxTime(0)));
  476. Quaternion rotation((Degree)rotationEuler.x, (Degree)rotationEuler.y, (Degree)rotationEuler.z);
  477. Matrix4 localTransform = Matrix4::TRS(translation, rotation, scale);
  478. node->name = fbxNode->GetNameWithoutNameSpacePrefix().Buffer();
  479. node->fbxNode = fbxNode;
  480. if (parent != nullptr)
  481. {
  482. node->worldTransform = parent->worldTransform * localTransform;
  483. parent->children.push_back(node);
  484. }
  485. else
  486. node->worldTransform = localTransform;
  487. // Geometry transform is applied to geometry (mesh data) only, it is not inherited by children, so we store it
  488. // separately
  489. Vector3 geomTrans = FBXToNativeType(fbxNode->GeometricTranslation.Get());
  490. Vector3 geomRotEuler = FBXToNativeType(fbxNode->GeometricRotation.Get());
  491. Vector3 geomScale = FBXToNativeType(fbxNode->GeometricScaling.Get());
  492. Quaternion geomRotation((Degree)geomRotEuler.x, (Degree)geomRotEuler.y, (Degree)geomRotEuler.z);
  493. node->geomTransform = Matrix4::TRS(geomTrans, geomRotation, geomScale);
  494. scene.nodeMap.insert(std::make_pair(fbxNode, node));
  495. // Determine if geometry winding needs to be flipped to match the engine convention. This is true by default, but
  496. // each negative scaling factor changes the winding.
  497. if (parent != nullptr)
  498. node->flipWinding = parent->flipWinding;
  499. else
  500. node->flipWinding = true;
  501. for (UINT32 i = 0; i < 3; i++)
  502. {
  503. if (scale[i] < 0.0f) node->flipWinding = !node->flipWinding;
  504. if (geomScale[i] < 0.0f) node->flipWinding = !node->flipWinding;
  505. }
  506. return node;
  507. }
  508. void FBXImporter::splitMeshVertices(FBXImportScene& scene)
  509. {
  510. Vector<FBXImportMesh*> splitMeshes;
  511. for (auto& mesh : scene.meshes)
  512. {
  513. FBXImportMesh* splitMesh = bs_new<FBXImportMesh>();
  514. splitMesh->fbxMesh = mesh->fbxMesh;
  515. splitMesh->referencedBy = mesh->referencedBy;
  516. splitMesh->bones = mesh->bones;
  517. FBXUtility::splitVertices(*mesh, *splitMesh);
  518. splitMeshes.push_back(splitMesh);
  519. bs_delete(mesh);
  520. }
  521. scene.meshes = splitMeshes;
  522. }
  523. void FBXImporter::convertAnimations(const Vector<FBXAnimationClip>& clips, const Vector<AnimationSplitInfo>& splits,
  524. const SPtr<Skeleton>& skeleton, bool importRootMotion, Vector<FBXAnimationClipData>& output)
  525. {
  526. UnorderedSet<String> names;
  527. String rootBoneName;
  528. if (skeleton == nullptr)
  529. importRootMotion = false;
  530. else
  531. {
  532. UINT32 rootBoneIdx = skeleton->getRootBoneIndex();
  533. if (rootBoneIdx == (UINT32)-1)
  534. importRootMotion = false;
  535. else
  536. rootBoneName = skeleton->getBoneInfo(rootBoneIdx).name;
  537. }
  538. bool isFirstClip = true;
  539. for (auto& clip : clips)
  540. {
  541. SPtr<AnimationCurves> curves = bs_shared_ptr_new<AnimationCurves>();
  542. SPtr<RootMotion> rootMotion;
  543. // Find offset so animations start at time 0
  544. float animStart = std::numeric_limits<float>::infinity();
  545. for (auto& bone : clip.boneAnimations)
  546. {
  547. if(bone.translation.getNumKeyFrames() > 0)
  548. animStart = std::min(bone.translation.getKeyFrame(0).time, animStart);
  549. if (bone.rotation.getNumKeyFrames() > 0)
  550. animStart = std::min(bone.rotation.getKeyFrame(0).time, animStart);
  551. if (bone.scale.getNumKeyFrames() > 0)
  552. animStart = std::min(bone.scale.getKeyFrame(0).time, animStart);
  553. }
  554. for (auto& anim : clip.blendShapeAnimations)
  555. {
  556. if (anim.curve.getNumKeyFrames() > 0)
  557. animStart = std::min(anim.curve.getKeyFrame(0).time, animStart);
  558. }
  559. AnimationCurveFlags blendShapeFlags = AnimationCurveFlag::ImportedCurve | AnimationCurveFlag::MorphFrame;
  560. if (animStart != 0.0f && animStart != std::numeric_limits<float>::infinity())
  561. {
  562. for (auto& bone : clip.boneAnimations)
  563. {
  564. TAnimationCurve<Vector3> translation = AnimationUtility::offsetCurve(bone.translation, -animStart);
  565. TAnimationCurve<Quaternion> rotation = AnimationUtility::offsetCurve(bone.rotation, -animStart);
  566. TAnimationCurve<Vector3> scale = AnimationUtility::offsetCurve(bone.scale, -animStart);
  567. if(importRootMotion && bone.node->name == rootBoneName)
  568. rootMotion = bs_shared_ptr_new<RootMotion>(translation, rotation);
  569. else
  570. {
  571. curves->position.push_back({ bone.node->name, AnimationCurveFlag::ImportedCurve, translation });
  572. curves->rotation.push_back({ bone.node->name, AnimationCurveFlag::ImportedCurve, rotation });
  573. curves->scale.push_back({ bone.node->name, AnimationCurveFlag::ImportedCurve, scale });
  574. }
  575. }
  576. for (auto& anim : clip.blendShapeAnimations)
  577. {
  578. TAnimationCurve<float> curve = AnimationUtility::offsetCurve(anim.curve, -animStart);
  579. curves->generic.push_back({ anim.blendShape, blendShapeFlags, curve });
  580. }
  581. }
  582. else
  583. {
  584. for (auto& bone : clip.boneAnimations)
  585. {
  586. if (importRootMotion && bone.node->name == rootBoneName)
  587. rootMotion = bs_shared_ptr_new<RootMotion>(bone.translation, bone.rotation);
  588. else
  589. {
  590. curves->position.push_back({ bone.node->name, AnimationCurveFlag::ImportedCurve, bone.translation });
  591. curves->rotation.push_back({ bone.node->name, AnimationCurveFlag::ImportedCurve, bone.rotation });
  592. curves->scale.push_back({ bone.node->name, AnimationCurveFlag::ImportedCurve, bone.scale });
  593. }
  594. }
  595. for (auto& anim : clip.blendShapeAnimations)
  596. curves->generic.push_back({ anim.blendShape, blendShapeFlags, anim.curve });
  597. }
  598. // See if any splits are required. We only split the first clip as it is assumed if FBX has multiple clips the
  599. // user has the ability to split them externally.
  600. if(isFirstClip && !splits.empty())
  601. {
  602. float secondsPerFrame = 1.0f / clip.sampleRate;
  603. for(auto& split : splits)
  604. {
  605. SPtr<AnimationCurves> splitClipCurve = bs_shared_ptr_new<AnimationCurves>();
  606. SPtr<RootMotion> splitRootMotion;
  607. auto splitCurves = [&](auto& inCurves, auto& outCurves)
  608. {
  609. UINT32 numCurves = (UINT32)inCurves.size();
  610. outCurves.resize(numCurves);
  611. for (UINT32 i = 0; i < numCurves; i++)
  612. {
  613. auto& animCurve = inCurves[i].curve;
  614. outCurves[i].name = inCurves[i].name;
  615. UINT32 numFrames = animCurve.getNumKeyFrames();
  616. if (numFrames == 0)
  617. continue;
  618. float startTime = split.startFrame * secondsPerFrame;
  619. float endTime = split.endFrame * secondsPerFrame;
  620. outCurves[i].curve = inCurves[i].curve.split(startTime, endTime);
  621. if (split.isAdditive)
  622. outCurves[i].curve.makeAdditive();
  623. }
  624. };
  625. splitCurves(curves->position, splitClipCurve->position);
  626. splitCurves(curves->rotation, splitClipCurve->rotation);
  627. splitCurves(curves->scale, splitClipCurve->scale);
  628. splitCurves(curves->generic, splitClipCurve->generic);
  629. if(rootMotion != nullptr)
  630. {
  631. auto splitCurve = [&](auto& inCurve, auto& outCurve)
  632. {
  633. UINT32 numFrames = inCurve.getNumKeyFrames();
  634. if (numFrames > 0)
  635. {
  636. float startTime = split.startFrame * secondsPerFrame;
  637. float endTime = split.endFrame * secondsPerFrame;
  638. outCurve = inCurve.split(startTime, endTime);
  639. if (split.isAdditive)
  640. outCurve.makeAdditive();
  641. }
  642. };
  643. splitRootMotion = bs_shared_ptr_new<RootMotion>();
  644. splitCurve(rootMotion->position, splitRootMotion->position);
  645. splitCurve(rootMotion->rotation, splitRootMotion->rotation);
  646. }
  647. // Search for a unique name
  648. String name = split.name;
  649. UINT32 attemptIdx = 0;
  650. while (names.find(name) != names.end())
  651. {
  652. name = clip.name + "_" + toString(attemptIdx);
  653. attemptIdx++;
  654. }
  655. names.insert(name);
  656. output.push_back(FBXAnimationClipData(name, split.isAdditive, clip.sampleRate, splitClipCurve,
  657. splitRootMotion));
  658. }
  659. }
  660. else
  661. {
  662. // Search for a unique name
  663. String name = clip.name;
  664. UINT32 attemptIdx = 0;
  665. while(names.find(name) != names.end())
  666. {
  667. name = clip.name + "_" + toString(attemptIdx);
  668. attemptIdx++;
  669. }
  670. names.insert(name);
  671. output.push_back(FBXAnimationClipData(name, false, clip.sampleRate, curves, rootMotion));
  672. }
  673. isFirstClip = false;
  674. }
  675. }
  676. SPtr<RendererMeshData> FBXImporter::generateMeshData(const FBXImportScene& scene, const FBXImportOptions& options,
  677. Vector<SubMesh>& outputSubMeshes)
  678. {
  679. Vector<SPtr<MeshData>> allMeshData;
  680. Vector<Vector<SubMesh>> allSubMeshes;
  681. Vector<BONE_DESC> allBones;
  682. UnorderedMap<FBXImportNode*, UINT32> boneMap;
  683. UINT32 boneIndexOffset = 0;
  684. for (auto& mesh : scene.meshes)
  685. {
  686. Vector<Vector<UINT32>> indicesPerMaterial;
  687. for (UINT32 i = 0; i < (UINT32)mesh->indices.size(); i++)
  688. {
  689. while ((UINT32)mesh->materials[i] >= (UINT32)indicesPerMaterial.size())
  690. indicesPerMaterial.push_back(Vector<UINT32>());
  691. indicesPerMaterial[mesh->materials[i]].push_back(mesh->indices[i]);
  692. }
  693. UINT32* orderedIndices = (UINT32*)bs_alloc((UINT32)mesh->indices.size() * sizeof(UINT32));
  694. Vector<SubMesh> subMeshes;
  695. UINT32 currentIndex = 0;
  696. for (auto& subMeshIndices : indicesPerMaterial)
  697. {
  698. UINT32 indexCount = (UINT32)subMeshIndices.size();
  699. UINT32* dest = orderedIndices + currentIndex;
  700. memcpy(dest, subMeshIndices.data(), indexCount * sizeof(UINT32));
  701. subMeshes.push_back(SubMesh(currentIndex, indexCount, DOT_TRIANGLE_LIST));
  702. currentIndex += indexCount;
  703. }
  704. UINT32 vertexLayout = (UINT32)VertexLayout::Position;
  705. size_t numVertices = mesh->positions.size();
  706. bool hasColors = mesh->colors.size() == numVertices;
  707. bool hasNormals = mesh->normals.size() == numVertices;
  708. bool hasBoneInfluences = mesh->boneInfluences.size() == numVertices;
  709. if (hasColors)
  710. vertexLayout |= (UINT32)VertexLayout::Color;
  711. bool hasTangents = false;
  712. if (hasNormals)
  713. {
  714. vertexLayout |= (UINT32)VertexLayout::Normal;
  715. if (mesh->tangents.size() == numVertices &&
  716. mesh->bitangents.size() == numVertices)
  717. {
  718. vertexLayout |= (UINT32)VertexLayout::Tangent;
  719. hasTangents = true;
  720. }
  721. }
  722. if (hasBoneInfluences)
  723. vertexLayout |= (UINT32)VertexLayout::BoneWeights;
  724. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  725. {
  726. if (mesh->UV[i].size() == numVertices)
  727. {
  728. if (i == 0)
  729. vertexLayout |= (UINT32)VertexLayout::UV0;
  730. else if (i == 1)
  731. vertexLayout |= (UINT32)VertexLayout::UV1;
  732. }
  733. }
  734. UINT32 numIndices = (UINT32)mesh->indices.size();
  735. for (auto& node : mesh->referencedBy)
  736. {
  737. Matrix4 worldTransform = scene.globalScale * node->worldTransform * node->geomTransform;
  738. Matrix4 worldTransformIT = worldTransform.inverse();
  739. worldTransformIT = worldTransformIT.transpose();
  740. SPtr<RendererMeshData> meshData = RendererMeshData::create((UINT32)numVertices, numIndices, (VertexLayout)vertexLayout);
  741. // Copy indices
  742. if(!node->flipWinding)
  743. meshData->setIndices(orderedIndices, numIndices * sizeof(UINT32));
  744. else
  745. {
  746. UINT32* flippedIndices = bs_stack_alloc<UINT32>(numIndices);
  747. for (UINT32 i = 0; i < numIndices; i += 3)
  748. {
  749. flippedIndices[i + 0] = orderedIndices[i + 0];
  750. flippedIndices[i + 1] = orderedIndices[i + 2];
  751. flippedIndices[i + 2] = orderedIndices[i + 1];
  752. }
  753. meshData->setIndices(flippedIndices, numIndices * sizeof(UINT32));
  754. bs_stack_free(flippedIndices);
  755. }
  756. // Copy & transform positions
  757. UINT32 positionsSize = sizeof(Vector3) * (UINT32)numVertices;
  758. Vector3* transformedPositions = (Vector3*)bs_stack_alloc(positionsSize);
  759. for (UINT32 i = 0; i < (UINT32)numVertices; i++)
  760. transformedPositions[i] = worldTransform.multiplyAffine((Vector3)mesh->positions[i]);
  761. meshData->setPositions(transformedPositions, positionsSize);
  762. bs_stack_free(transformedPositions);
  763. // Copy & transform normals
  764. if (hasNormals)
  765. {
  766. UINT32 normalsSize = sizeof(Vector3) * (UINT32)numVertices;
  767. Vector3* transformedNormals = (Vector3*)bs_stack_alloc(normalsSize);
  768. // Copy, convert & transform tangents & bitangents
  769. if (hasTangents)
  770. {
  771. UINT32 tangentsSize = sizeof(Vector4) * (UINT32)numVertices;
  772. Vector4* transformedTangents = (Vector4*)bs_stack_alloc(tangentsSize);
  773. for (UINT32 i = 0; i < (UINT32)numVertices; i++)
  774. {
  775. Vector3 normal = (Vector3)mesh->normals[i];
  776. normal = worldTransformIT.multiplyDirection(normal);
  777. transformedNormals[i] = Vector3::normalize(normal);
  778. Vector3 tangent = (Vector3)mesh->tangents[i];
  779. tangent = Vector3::normalize(worldTransformIT.multiplyDirection(tangent));
  780. Vector3 bitangent = (Vector3)mesh->bitangents[i];
  781. bitangent = worldTransformIT.multiplyDirection(bitangent);
  782. Vector3 engineBitangent = Vector3::cross(normal, tangent);
  783. float sign = Vector3::dot(engineBitangent, bitangent);
  784. transformedTangents[i] = Vector4(tangent.x, tangent.y, tangent.z, sign > 0 ? 1.0f : -1.0f);
  785. }
  786. meshData->setTangents(transformedTangents, tangentsSize);
  787. bs_stack_free(transformedTangents);
  788. }
  789. else // Just normals
  790. {
  791. for (UINT32 i = 0; i < (UINT32)numVertices; i++)
  792. transformedNormals[i] = Vector3::normalize(worldTransformIT.multiplyDirection((Vector3)mesh->normals[i]));
  793. }
  794. meshData->setNormals(transformedNormals, normalsSize);
  795. bs_stack_free(transformedNormals);
  796. }
  797. // Copy colors
  798. if (hasColors)
  799. {
  800. meshData->setColors(mesh->colors.data(), sizeof(UINT32) * (UINT32)numVertices);
  801. }
  802. // Copy UV
  803. int writeUVIDx = 0;
  804. for (auto& uvLayer : mesh->UV)
  805. {
  806. if (uvLayer.size() == numVertices)
  807. {
  808. UINT32 size = sizeof(Vector2) * (UINT32)numVertices;
  809. Vector2* transformedUV = (Vector2*)bs_stack_alloc(size);
  810. UINT32 i = 0;
  811. for (auto& uv : uvLayer)
  812. {
  813. transformedUV[i] = uv;
  814. transformedUV[i].y = 1.0f - uv.y;
  815. i++;
  816. }
  817. if (writeUVIDx == 0)
  818. meshData->setUV0(transformedUV, size);
  819. else if (writeUVIDx == 1)
  820. meshData->setUV1(transformedUV, size);
  821. bs_stack_free(transformedUV);
  822. writeUVIDx++;
  823. }
  824. }
  825. // Copy bone influences
  826. if(hasBoneInfluences)
  827. {
  828. UINT32 bufferSize = sizeof(BoneWeight) * (UINT32)numVertices;
  829. BoneWeight* weights = (BoneWeight*)bs_stack_alloc(bufferSize);
  830. for(UINT32 i = 0; i < (UINT32)numVertices; i++)
  831. {
  832. weights[i].index0 = mesh->boneInfluences[i].indices[0] + boneIndexOffset;
  833. weights[i].index1 = mesh->boneInfluences[i].indices[1] + boneIndexOffset;
  834. weights[i].index2 = mesh->boneInfluences[i].indices[2] + boneIndexOffset;
  835. weights[i].index3 = mesh->boneInfluences[i].indices[3] + boneIndexOffset;
  836. weights[i].weight0 = mesh->boneInfluences[i].weights[0];
  837. weights[i].weight1 = mesh->boneInfluences[i].weights[1];
  838. weights[i].weight2 = mesh->boneInfluences[i].weights[2];
  839. weights[i].weight3 = mesh->boneInfluences[i].weights[3];
  840. }
  841. meshData->setBoneWeights(weights, bufferSize);
  842. bs_stack_free(weights);
  843. }
  844. allMeshData.push_back(meshData->getData());
  845. allSubMeshes.push_back(subMeshes);
  846. }
  847. bs_free(orderedIndices);
  848. UINT32 numBones = (UINT32)mesh->bones.size();
  849. boneIndexOffset += numBones;
  850. }
  851. if (allMeshData.size() > 1)
  852. {
  853. return RendererMeshData::create(MeshData::combine(allMeshData, allSubMeshes, outputSubMeshes));
  854. }
  855. else if (allMeshData.size() == 1)
  856. {
  857. outputSubMeshes = allSubMeshes[0];
  858. return RendererMeshData::create(allMeshData[0]);
  859. }
  860. return nullptr;
  861. }
  862. template<class TFBX, class TNative>
  863. class FBXDirectIndexer
  864. {
  865. public:
  866. FBXDirectIndexer(const FbxLayerElementTemplate<TFBX>& layer)
  867. :mElementArray(layer.GetDirectArray()),
  868. mElementCount(mElementArray.GetCount())
  869. {}
  870. bool get(int index, TNative& output) const
  871. {
  872. if (index < 0 || index >= mElementCount)
  873. return false;
  874. output = FBXToNativeType(mElementArray.GetAt(index));
  875. return true;
  876. }
  877. bool isEmpty() const
  878. {
  879. return mElementCount == 0;
  880. }
  881. private:
  882. const FbxLayerElementArrayTemplate<TFBX>& mElementArray;
  883. int mElementCount;
  884. };
  885. template<class TFBX, class TNative>
  886. class FBXIndexIndexer
  887. {
  888. public:
  889. FBXIndexIndexer(const FbxLayerElementTemplate<TFBX>& layer)
  890. :mElementArray(layer.GetDirectArray()),
  891. mIndexArray(layer.GetIndexArray()),
  892. mElementCount(mElementArray.GetCount()),
  893. mIndexCount(mIndexArray.GetCount())
  894. {}
  895. bool get(int index, TNative& output) const
  896. {
  897. if (index < 0 || index >= mIndexCount)
  898. return false;
  899. int actualIndex = mIndexArray.GetAt(index);
  900. if (actualIndex < 0 || actualIndex >= mElementCount)
  901. return false;
  902. output = FBXToNativeType(mElementArray.GetAt(actualIndex));
  903. return true;
  904. }
  905. bool isEmpty() const
  906. {
  907. return mElementCount == 0 || mIndexCount == 0;
  908. }
  909. private:
  910. const FbxLayerElementArrayTemplate<TFBX>& mElementArray;
  911. const FbxLayerElementArrayTemplate<int>& mIndexArray;
  912. int mElementCount;
  913. int mIndexCount;
  914. };
  915. template<class TFBX, class TNative, class TIndexer>
  916. void readLayerData(FbxLayerElementTemplate<TFBX>& layer, Vector<TNative>& output, const Vector<int>& indices)
  917. {
  918. TIndexer indexer(layer);
  919. if (indexer.isEmpty())
  920. return;
  921. output.resize(indices.size());
  922. FbxLayerElement::EMappingMode mappingMode = layer.GetMappingMode();
  923. UINT32 indexCount = (UINT32)indices.size();
  924. switch (mappingMode)
  925. {
  926. case FbxLayerElement::eByControlPoint:
  927. for (UINT32 i = 0; i < indexCount; i++)
  928. {
  929. int index = indices[i];
  930. indexer.get(index, output[i]);
  931. }
  932. break;
  933. case FbxLayerElement::eByPolygonVertex:
  934. for (UINT32 i = 0; i < indexCount; i++)
  935. indexer.get(i, output[i]);
  936. break;
  937. case FbxLayerElement::eByPolygon:
  938. // We expect mesh to be triangulated here
  939. {
  940. UINT32 polygonCount = indexCount / 3;
  941. UINT32 index = 0;
  942. for (UINT32 i = 0; i < polygonCount; i++)
  943. {
  944. TNative value;
  945. indexer.get(i, value);
  946. output[index++] = value;
  947. output[index++] = value;
  948. output[index++] = value;
  949. }
  950. }
  951. break;
  952. case FbxLayerElement::eAllSame:
  953. {
  954. TNative value;
  955. indexer.get(0, value);
  956. for (UINT32 i = 0; i < indexCount; i++)
  957. output[i] = value;
  958. }
  959. break;
  960. default:
  961. LOGWRN("FBX Import: Unsupported layer mapping mode.");
  962. break;
  963. }
  964. }
  965. template<class TFBX, class TNative>
  966. void readLayerData(FbxLayerElementTemplate<TFBX>& layer, Vector<TNative>& output, const Vector<int>& indices)
  967. {
  968. FbxLayerElement::EReferenceMode refMode = layer.GetReferenceMode();
  969. if (refMode == FbxLayerElement::eDirect)
  970. readLayerData<TFBX, TNative, FBXDirectIndexer<TFBX, TNative> >(layer, output, indices);
  971. else if (refMode == FbxLayerElement::eIndexToDirect)
  972. readLayerData<TFBX, TNative, FBXIndexIndexer<TFBX, TNative> >(layer, output, indices);
  973. else
  974. LOGWRN("FBX Import: Unsupported layer reference mode.");
  975. }
  976. void FBXImporter::parseMesh(FbxMesh* mesh, FBXImportNode* parentNode, const FBXImportOptions& options, FBXImportScene& outputScene)
  977. {
  978. // Check if valid
  979. if (!mesh->IsTriangleMesh())
  980. return;
  981. UINT32 vertexCount = mesh->GetControlPointsCount();
  982. UINT32 triangleCount = mesh->GetPolygonCount();
  983. if (vertexCount == 0 || triangleCount == 0)
  984. return;
  985. // Register in global mesh array
  986. FBXImportMesh* importMesh = nullptr;
  987. auto iterFindMesh = outputScene.meshMap.find(mesh);
  988. if (iterFindMesh != outputScene.meshMap.end())
  989. {
  990. UINT32 meshIdx = iterFindMesh->second;
  991. outputScene.meshes[meshIdx]->referencedBy.push_back(parentNode);
  992. return;
  993. }
  994. else
  995. {
  996. importMesh = bs_new<FBXImportMesh>();
  997. outputScene.meshes.push_back(importMesh);
  998. importMesh->referencedBy.push_back(parentNode);
  999. importMesh->fbxMesh = mesh;
  1000. outputScene.meshMap[mesh] = (UINT32)outputScene.meshes.size() - 1;
  1001. }
  1002. // Import vertices
  1003. importMesh->positions.resize(vertexCount);
  1004. FbxVector4* controlPoints = mesh->GetControlPoints();
  1005. for (UINT32 i = 0; i < vertexCount; i++)
  1006. importMesh->positions[i] = FBXToNativeType(controlPoints[i]);
  1007. // Import triangles
  1008. UINT32 indexCount = triangleCount * 3;
  1009. importMesh->indices.resize(indexCount);
  1010. int* fbxIndices = mesh->GetPolygonVertices();
  1011. importMesh->indices.assign(fbxIndices, fbxIndices + indexCount);
  1012. // Import UVs
  1013. Vector<FbxLayerElementUV*> fbxUVLayers;
  1014. //// Search the diffuse layers first
  1015. for (UINT32 i = 0; i < FBX_IMPORT_MAX_UV_LAYERS; i++)
  1016. {
  1017. FbxLayer* layer = mesh->GetLayer(i, FbxLayerElement::eUV);
  1018. if (layer == nullptr)
  1019. continue;
  1020. for (int j = FbxLayerElement::eTextureDiffuse; j < FbxLayerElement::eTypeCount; j++)
  1021. {
  1022. FbxLayerElementUV* uvLayer = layer->GetUVs((FbxLayerElement::EType)j);
  1023. if (uvLayer == nullptr)
  1024. continue;
  1025. fbxUVLayers.push_back(uvLayer);
  1026. if (fbxUVLayers.size() == FBX_IMPORT_MAX_UV_LAYERS)
  1027. break;
  1028. }
  1029. if (fbxUVLayers.size() == FBX_IMPORT_MAX_UV_LAYERS)
  1030. break;
  1031. }
  1032. //// If there's room, search all others too
  1033. if (fbxUVLayers.size() < FBX_IMPORT_MAX_UV_LAYERS)
  1034. {
  1035. UINT32 numLayers = mesh->GetLayerCount();
  1036. for (UINT32 i = 0; i < numLayers; i++)
  1037. {
  1038. FbxLayer* layer = mesh->GetLayer(i);
  1039. if (layer == nullptr)
  1040. continue;
  1041. for (int j = FbxLayerElement::eTextureDiffuse; j < FbxLayerElement::eTypeCount; j++)
  1042. {
  1043. FbxLayerElementUV* uvLayer = layer->GetUVs((FbxLayerElement::EType)j);
  1044. if (uvLayer == nullptr)
  1045. continue;
  1046. auto iterFind = std::find(fbxUVLayers.begin(), fbxUVLayers.end(), uvLayer);
  1047. if (iterFind != fbxUVLayers.end())
  1048. continue;
  1049. fbxUVLayers.push_back(uvLayer);
  1050. if (fbxUVLayers.size() == FBX_IMPORT_MAX_UV_LAYERS)
  1051. break;
  1052. }
  1053. if (fbxUVLayers.size() == FBX_IMPORT_MAX_UV_LAYERS)
  1054. break;
  1055. }
  1056. }
  1057. for (size_t i = 0; i < fbxUVLayers.size(); i++)
  1058. readLayerData(*fbxUVLayers[i], importMesh->UV[i], importMesh->indices);
  1059. FbxLayer* mainLayer = mesh->GetLayer(0);
  1060. if (mainLayer != nullptr)
  1061. {
  1062. // Import colors
  1063. if (mainLayer->GetVertexColors() != nullptr)
  1064. readLayerData(*mainLayer->GetVertexColors(), importMesh->colors, importMesh->indices);
  1065. // Import normals
  1066. if (options.importNormals)
  1067. {
  1068. bool hasNormals = mainLayer->GetNormals() != nullptr;
  1069. if (!hasNormals)
  1070. {
  1071. if (mainLayer->GetSmoothing() != nullptr)
  1072. {
  1073. FbxLayerElementSmoothing* smoothing = mainLayer->GetSmoothing();
  1074. if (smoothing->GetMappingMode() == FbxLayerElement::eByEdge)
  1075. {
  1076. FbxGeometryConverter converter(mFBXManager);
  1077. converter.ComputePolygonSmoothingFromEdgeSmoothing(mesh, 0);
  1078. }
  1079. readLayerData(*smoothing, importMesh->smoothingGroups, importMesh->indices);
  1080. if (!importMesh->smoothingGroups.empty())
  1081. {
  1082. FBXUtility::normalsFromSmoothing(importMesh->positions, importMesh->indices,
  1083. importMesh->smoothingGroups, importMesh->normals);
  1084. }
  1085. }
  1086. }
  1087. else
  1088. readLayerData(*mainLayer->GetNormals(), importMesh->normals, importMesh->indices);
  1089. }
  1090. // Import tangents
  1091. if (options.importTangents)
  1092. {
  1093. bool hasTangents = mainLayer->GetTangents() != nullptr && mainLayer->GetBinormals() != nullptr;
  1094. if (!hasTangents)
  1095. {
  1096. if (fbxUVLayers.size() > 0)
  1097. hasTangents = mesh->GenerateTangentsData(0, false);
  1098. }
  1099. if (hasTangents)
  1100. {
  1101. readLayerData(*mainLayer->GetTangents(), importMesh->tangents, importMesh->indices);
  1102. readLayerData(*mainLayer->GetBinormals(), importMesh->bitangents, importMesh->indices);
  1103. }
  1104. }
  1105. // Import material indexes
  1106. if (mainLayer->GetMaterials() != nullptr)
  1107. {
  1108. Vector<FbxSurfaceMaterial*> fbxMaterials;
  1109. readLayerData(*mainLayer->GetMaterials(), fbxMaterials, importMesh->indices);
  1110. UnorderedMap<FbxSurfaceMaterial*, int> materialLookup;
  1111. int nextMaterialIdx = 0;
  1112. for (UINT32 i = 0; i < (UINT32)fbxMaterials.size(); i++)
  1113. {
  1114. auto iterFind = materialLookup.find(fbxMaterials[i]);
  1115. int materialIdx = 0;
  1116. if (iterFind != materialLookup.end())
  1117. materialIdx = iterFind->second;
  1118. else
  1119. {
  1120. materialIdx = nextMaterialIdx++;
  1121. materialLookup[fbxMaterials[i]] = materialIdx;
  1122. }
  1123. importMesh->materials.push_back(materialIdx);
  1124. }
  1125. }
  1126. else
  1127. {
  1128. importMesh->materials.resize(importMesh->indices.size(), 0);
  1129. }
  1130. }
  1131. }
  1132. void FBXImporter::importBlendShapes(FBXImportScene& scene, const FBXImportOptions& options)
  1133. {
  1134. for (auto& mesh : scene.meshes)
  1135. {
  1136. FbxMesh* fbxMesh = mesh->fbxMesh;
  1137. UINT32 deformerCount = (UINT32)fbxMesh->GetDeformerCount(FbxDeformer::eBlendShape);
  1138. for (UINT32 i = 0; i < deformerCount; i++)
  1139. {
  1140. FbxBlendShape* deformer = static_cast<FbxBlendShape*>(fbxMesh->GetDeformer(i, FbxDeformer::eBlendShape));
  1141. UINT32 blendShapeChannelCount = (UINT32)deformer->GetBlendShapeChannelCount();
  1142. for (UINT32 j = 0; j < blendShapeChannelCount; ++j)
  1143. {
  1144. FbxBlendShapeChannel* channel = deformer->GetBlendShapeChannel(j);
  1145. double* weights = channel->GetTargetShapeFullWeights();
  1146. UINT32 frameCount = channel->GetTargetShapeCount();
  1147. if (frameCount == 0)
  1148. continue;
  1149. mesh->blendShapes.push_back(FBXBlendShape());
  1150. FBXBlendShape& blendShape = mesh->blendShapes.back();
  1151. blendShape.name = channel->GetName();
  1152. blendShape.frames.resize(frameCount);
  1153. // Get name without invalid characters
  1154. blendShape.name = StringUtil::replaceAll(blendShape.name, ".", "_");
  1155. blendShape.name = StringUtil::replaceAll(blendShape.name, "/", "_");
  1156. for (UINT32 k = 0; k < frameCount; k++)
  1157. {
  1158. FbxShape* fbxShape = channel->GetTargetShape(k);
  1159. FBXBlendShapeFrame& frame = blendShape.frames[k];
  1160. frame.name = fbxShape->GetName();
  1161. frame.weight = (float)(weights[k] / 100.0);
  1162. // Get name without invalid characters
  1163. frame.name = StringUtil::replaceAll(frame.name, ".", "_");
  1164. frame.name = StringUtil::replaceAll(frame.name, "/", "_");
  1165. importBlendShapeFrame(fbxShape, *mesh, options, frame);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. }
  1171. void FBXImporter::importBlendShapeFrame(FbxShape* shape, const FBXImportMesh& mesh, const FBXImportOptions& options, FBXBlendShapeFrame& outFrame)
  1172. {
  1173. UINT32 vertexCount = (UINT32)shape->GetControlPointsCount();
  1174. outFrame.positions.resize(vertexCount);
  1175. FbxVector4* controlPoints = shape->GetControlPoints();
  1176. for (UINT32 i = 0; i < vertexCount; i++)
  1177. outFrame.positions[i] = FBXToNativeType(controlPoints[i]);
  1178. FbxLayer* mainLayer = shape->GetLayer(0);
  1179. if (options.importNormals)
  1180. {
  1181. bool hasNormals = mainLayer->GetNormals() != nullptr;
  1182. if (!hasNormals)
  1183. {
  1184. if (!mesh.smoothingGroups.empty())
  1185. {
  1186. FBXUtility::normalsFromSmoothing(outFrame.positions, mesh.indices,
  1187. mesh.smoothingGroups, outFrame.normals);
  1188. }
  1189. }
  1190. else
  1191. readLayerData(*mainLayer->GetNormals(), outFrame.normals, mesh.indices);
  1192. }
  1193. if (options.importTangents)
  1194. {
  1195. bool hasTangents = mainLayer->GetTangents() != nullptr && mainLayer->GetBinormals() != nullptr;
  1196. if (hasTangents)
  1197. {
  1198. readLayerData(*mainLayer->GetTangents(), outFrame.tangents, mesh.indices);
  1199. readLayerData(*mainLayer->GetBinormals(), outFrame.bitangents, mesh.indices);
  1200. }
  1201. }
  1202. }
  1203. void FBXImporter::importSkin(FBXImportScene& scene, const FBXImportOptions& options)
  1204. {
  1205. for (auto& mesh : scene.meshes)
  1206. {
  1207. FbxMesh* fbxMesh = mesh->fbxMesh;
  1208. UINT32 deformerCount = (UINT32)fbxMesh->GetDeformerCount(FbxDeformer::eSkin);
  1209. if (deformerCount > 0)
  1210. {
  1211. // We ignore other deformers if there's more than one
  1212. FbxSkin* deformer = static_cast<FbxSkin*>(fbxMesh->GetDeformer(0, FbxDeformer::eSkin));
  1213. UINT32 boneCount = (UINT32)deformer->GetClusterCount();
  1214. if (boneCount == 0)
  1215. continue;
  1216. // If only one bone and it links to itself, ignore the bone
  1217. if (boneCount == 1)
  1218. {
  1219. FbxCluster* cluster = deformer->GetCluster(0);
  1220. if (mesh->referencedBy.size() == 1 && mesh->referencedBy[0]->fbxNode == cluster->GetLink())
  1221. continue;
  1222. }
  1223. importSkin(scene, deformer, *mesh, options);
  1224. }
  1225. }
  1226. }
  1227. void FBXImporter::importSkin(FBXImportScene& scene, FbxSkin* skin, FBXImportMesh& mesh, const FBXImportOptions& options)
  1228. {
  1229. Vector<FBXBoneInfluence>& influences = mesh.boneInfluences;
  1230. influences.resize(mesh.positions.size());
  1231. Matrix4 invGlobalScale = scene.globalScale.inverseAffine();
  1232. UnorderedSet<FbxNode*> existingBones;
  1233. UINT32 boneCount = (UINT32)skin->GetClusterCount();
  1234. for (UINT32 i = 0; i < boneCount; i++)
  1235. {
  1236. FbxCluster* cluster = skin->GetCluster(i);
  1237. FbxNode* link = cluster->GetLink();
  1238. // The bone node doesn't exist, skip it
  1239. auto iterFind = scene.nodeMap.find(link);
  1240. if (iterFind == scene.nodeMap.end())
  1241. continue;
  1242. mesh.bones.push_back(FBXBone());
  1243. FBXBone& bone = mesh.bones.back();
  1244. bone.node = iterFind->second;
  1245. if(mesh.referencedBy.size() > 1)
  1246. {
  1247. // Note: If this becomes a relevant issue (unlikely), then I will have to duplicate skeleton bones for
  1248. // each such mesh, since they will all require their own bind poses. Animation curves will also need to be
  1249. // handled specially (likely by allowing them to be applied to multiple bones at once). The other option is
  1250. // not to bake the node transform into mesh vertices and handle it on a Scene Object level.
  1251. LOGWRN("Skinned mesh has multiple different instances. This is not supported.");
  1252. }
  1253. // Calculate bind pose
  1254. FbxAMatrix clusterTransform;
  1255. cluster->GetTransformMatrix(clusterTransform);
  1256. FbxAMatrix linkTransform;
  1257. cluster->GetTransformLinkMatrix(linkTransform);
  1258. FbxAMatrix invLinkTransform = linkTransform.Inverse() * clusterTransform;
  1259. bone.bindPose = FBXToNativeType(invLinkTransform);
  1260. // Apply global scale to bind pose (we only apply the scale to translation portion because we scale the
  1261. // translation animation curves)
  1262. const Matrix4& nodeTfrm = iterFind->second->worldTransform;
  1263. Matrix4 nodeTfrmScaledTranslation = nodeTfrm;
  1264. nodeTfrmScaledTranslation[0][3] = nodeTfrmScaledTranslation[0][3] / scene.scaleFactor;
  1265. nodeTfrmScaledTranslation[1][3] = nodeTfrmScaledTranslation[1][3] / scene.scaleFactor;
  1266. nodeTfrmScaledTranslation[2][3] = nodeTfrmScaledTranslation[2][3] / scene.scaleFactor;
  1267. Matrix4 nodeTfrmInv = nodeTfrm.inverseAffine();
  1268. Matrix4 scaledTranslation = nodeTfrmInv * scene.globalScale * nodeTfrmScaledTranslation;
  1269. bone.bindPose = scaledTranslation * bone.bindPose * invGlobalScale;
  1270. bool isDuplicate = !existingBones.insert(link).second;
  1271. bool isAdditive = cluster->GetLinkMode() == FbxCluster::eAdditive;
  1272. // We avoid importing weights twice for duplicate bones and we don't
  1273. // support additive link mode.
  1274. bool importWeights = !isDuplicate && !isAdditive;
  1275. if (!importWeights)
  1276. continue;
  1277. double* weights = cluster->GetControlPointWeights();
  1278. INT32* indices = cluster->GetControlPointIndices();
  1279. UINT32 numIndices = (UINT32)cluster->GetControlPointIndicesCount();
  1280. INT32 numVertices = (INT32)influences.size();
  1281. // Add new weights while keeping them in order and removing the smallest ones
  1282. // if number of influences exceeds the set maximum value
  1283. for (UINT32 j = 0; j < numIndices; j++)
  1284. {
  1285. INT32 vertexIndex = indices[j];
  1286. float weight = (float)weights[j];
  1287. for (INT32 k = 0; k < FBX_IMPORT_MAX_BONE_INFLUENCES; k++)
  1288. {
  1289. if (vertexIndex < 0 || vertexIndex >= numVertices)
  1290. continue;
  1291. if (weight >= influences[vertexIndex].weights[k])
  1292. {
  1293. for (INT32 l = FBX_IMPORT_MAX_BONE_INFLUENCES - 2; l >= k; l--)
  1294. {
  1295. influences[vertexIndex].weights[l + 1] = influences[vertexIndex].weights[l];
  1296. influences[vertexIndex].indices[l + 1] = influences[vertexIndex].indices[l];
  1297. }
  1298. influences[vertexIndex].weights[k] = weight;
  1299. influences[vertexIndex].indices[k] = i;
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. }
  1305. if (mesh.bones.empty())
  1306. mesh.boneInfluences.clear();
  1307. UINT32 numBones = (UINT32)mesh.bones.size();
  1308. if (numBones > 256)
  1309. LOGWRN("A maximum of 256 bones per skeleton are supported. Imported skeleton has " + toString(numBones) + " bones");
  1310. // Normalize weights
  1311. UINT32 numInfluences = (UINT32)mesh.boneInfluences.size();
  1312. for (UINT32 i = 0; i < numInfluences; i++)
  1313. {
  1314. float sum = 0.0f;
  1315. for (UINT32 j = 0; j < FBX_IMPORT_MAX_BONE_INFLUENCES; j++)
  1316. sum += influences[i].weights[j];
  1317. float invSum = 1.0f / sum;
  1318. for (UINT32 j = 0; j < FBX_IMPORT_MAX_BONE_INFLUENCES; j++)
  1319. influences[i].weights[j] *= invSum;
  1320. }
  1321. }
  1322. void FBXImporter::generateMissingTangentSpace(FBXImportScene& scene, const FBXImportOptions& options)
  1323. {
  1324. for (auto& mesh : scene.meshes)
  1325. {
  1326. UINT32 numVertices = (UINT32)mesh->positions.size();
  1327. UINT32 numIndices = (UINT32)mesh->indices.size();
  1328. if ((options.importNormals || options.importTangents) && mesh->normals.empty())
  1329. {
  1330. mesh->normals.resize(numVertices);
  1331. MeshUtility::calculateNormals(mesh->positions.data(), (UINT8*)mesh->indices.data(), numVertices, numIndices, mesh->normals.data());
  1332. }
  1333. if (options.importTangents && !mesh->UV[0].empty() && (mesh->tangents.empty() || mesh->bitangents.empty()))
  1334. {
  1335. mesh->tangents.resize(numVertices);
  1336. mesh->bitangents.resize(numVertices);
  1337. MeshUtility::calculateTangents(mesh->positions.data(), mesh->normals.data(), mesh->UV[0].data(), (UINT8*)mesh->indices.data(),
  1338. numVertices, numIndices, mesh->tangents.data(), mesh->bitangents.data());
  1339. }
  1340. for (auto& shape : mesh->blendShapes)
  1341. {
  1342. for (auto& frame : shape.frames)
  1343. {
  1344. if ((options.importNormals || options.importTangents) && frame.normals.empty())
  1345. {
  1346. frame.normals.resize(numVertices);
  1347. MeshUtility::calculateNormals(mesh->positions.data(), (UINT8*)mesh->indices.data(), numVertices, numIndices, frame.normals.data());
  1348. }
  1349. if (options.importTangents && !mesh->UV[0].empty() && (frame.tangents.empty() || frame.bitangents.empty()))
  1350. {
  1351. frame.tangents.resize(numVertices);
  1352. frame.bitangents.resize(numVertices);
  1353. MeshUtility::calculateTangents(mesh->positions.data(), frame.normals.data(), mesh->UV[0].data(), (UINT8*)mesh->indices.data(),
  1354. numVertices, numIndices, frame.tangents.data(), frame.bitangents.data());
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. void FBXImporter::importAnimations(FbxScene* scene, FBXImportOptions& importOptions, FBXImportScene& importScene)
  1361. {
  1362. FbxNode* root = scene->GetRootNode();
  1363. UINT32 numAnimStacks = (UINT32)scene->GetSrcObjectCount<FbxAnimStack>();
  1364. for (UINT32 i = 0; i < numAnimStacks; i++)
  1365. {
  1366. FbxAnimStack* animStack = scene->GetSrcObject<FbxAnimStack>(i);
  1367. importScene.clips.push_back(FBXAnimationClip());
  1368. FBXAnimationClip& clip = importScene.clips.back();
  1369. clip.name = animStack->GetName();
  1370. FbxTimeSpan timeSpan = animStack->GetLocalTimeSpan();
  1371. clip.start = (float)timeSpan.GetStart().GetSecondDouble();
  1372. clip.end = (float)timeSpan.GetStop().GetSecondDouble();
  1373. clip.sampleRate = (UINT32)FbxTime::GetFrameRate(scene->GetGlobalSettings().GetTimeMode());
  1374. UINT32 layerCount = animStack->GetMemberCount<FbxAnimLayer>();
  1375. if (layerCount > 1)
  1376. {
  1377. FbxAnimEvaluator* evaluator = scene->GetAnimationEvaluator();
  1378. FbxTime startTime;
  1379. startTime.SetSecondDouble(clip.start);
  1380. FbxTime endTime;
  1381. endTime.SetSecondDouble(clip.end);
  1382. FbxTime sampleRate;
  1383. if (importOptions.animResample)
  1384. sampleRate.SetSecondDouble(importOptions.animSampleRate);
  1385. else
  1386. {
  1387. FbxTime::EMode timeMode = scene->GetGlobalSettings().GetTimeMode();
  1388. sampleRate.SetSecondDouble(1.0f / FbxTime::GetFrameRate(timeMode));
  1389. }
  1390. if (!animStack->BakeLayers(evaluator, startTime, endTime, sampleRate))
  1391. continue;
  1392. layerCount = animStack->GetMemberCount<FbxAnimLayer>();
  1393. }
  1394. if (layerCount == 1)
  1395. {
  1396. FbxAnimLayer* animLayer = animStack->GetMember<FbxAnimLayer>(0);
  1397. importAnimations(animLayer, root, importOptions, clip, importScene);
  1398. }
  1399. }
  1400. }
  1401. void FBXImporter::importAnimations(FbxAnimLayer* layer, FbxNode* node, FBXImportOptions& importOptions,
  1402. FBXAnimationClip& clip, FBXImportScene& importScene)
  1403. {
  1404. FbxAnimCurve* translation[3];
  1405. translation[0] = node->LclTranslation.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_X);
  1406. translation[1] = node->LclTranslation.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_Y);
  1407. translation[2] = node->LclTranslation.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_Z);
  1408. FbxAnimCurve* rotation[3];
  1409. rotation[0] = node->LclRotation.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_X);
  1410. rotation[1] = node->LclRotation.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_Y);
  1411. rotation[2] = node->LclRotation.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_Z);
  1412. FbxAnimCurve* scale[3];
  1413. scale[0] = node->LclScaling.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_X);
  1414. scale[1] = node->LclScaling.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_Y);
  1415. scale[2] = node->LclScaling.GetCurve(layer, FBXSDK_CURVENODE_COMPONENT_Z);
  1416. Vector3 defaultTranslation = FBXToNativeType(node->LclTranslation.Get());
  1417. Vector3 defaultRotation = FBXToNativeType(node->LclRotation.Get());
  1418. Vector3 defaultScale = FBXToNativeType(node->LclScaling.Get());
  1419. auto hasCurveValues = [](FbxAnimCurve* curves[3])
  1420. {
  1421. for (UINT32 i = 0; i < 3; i++)
  1422. {
  1423. if (curves[i] != nullptr && curves[i]->KeyGetCount() > 0)
  1424. return true;
  1425. }
  1426. return false;
  1427. };
  1428. bool hasBoneAnimation = hasCurveValues(translation) || hasCurveValues(rotation) || hasCurveValues(scale);
  1429. if (hasBoneAnimation)
  1430. {
  1431. clip.boneAnimations.push_back(FBXBoneAnimation());
  1432. FBXBoneAnimation& boneAnim = clip.boneAnimations.back();
  1433. boneAnim.node = importScene.nodeMap[node];
  1434. if (hasCurveValues(translation))
  1435. {
  1436. float defaultValues[3];
  1437. memcpy(defaultValues, &defaultTranslation, sizeof(defaultValues));
  1438. boneAnim.translation = importCurve<Vector3, 3>(translation, defaultValues, importOptions,
  1439. clip.start, clip.end);
  1440. }
  1441. else
  1442. {
  1443. Vector<TKeyframe<Vector3>> keyframes(1);
  1444. keyframes[0].value = defaultTranslation;
  1445. keyframes[0].inTangent = Vector3::ZERO;
  1446. keyframes[0].outTangent = Vector3::ZERO;
  1447. boneAnim.translation = TAnimationCurve<Vector3>(keyframes);
  1448. }
  1449. if (hasCurveValues(scale))
  1450. {
  1451. float defaultValues[3];
  1452. memcpy(defaultValues, &defaultScale, sizeof(defaultValues));
  1453. boneAnim.scale = importCurve<Vector3, 3>(scale, defaultValues, importOptions, clip.start, clip.end);
  1454. }
  1455. else
  1456. {
  1457. Vector<TKeyframe<Vector3>> keyframes(1);
  1458. keyframes[0].value = defaultScale;
  1459. keyframes[0].inTangent = Vector3::ZERO;
  1460. keyframes[0].outTangent = Vector3::ZERO;
  1461. boneAnim.scale = TAnimationCurve<Vector3>(keyframes);
  1462. }
  1463. SPtr<TAnimationCurve<Vector3>> eulerAnimation = bs_shared_ptr_new<TAnimationCurve<Vector3>>();
  1464. if (hasCurveValues(rotation))
  1465. {
  1466. float defaultValues[3];
  1467. memcpy(defaultValues, &defaultRotation, sizeof(defaultValues));
  1468. *eulerAnimation = importCurve<Vector3, 3>(rotation, defaultValues, importOptions, clip.start, clip.end);
  1469. }
  1470. else
  1471. {
  1472. Vector<TKeyframe<Vector3>> keyframes(1);
  1473. keyframes[0].value = defaultRotation;
  1474. keyframes[0].inTangent = Vector3::ZERO;
  1475. keyframes[0].outTangent = Vector3::ZERO;
  1476. *eulerAnimation = TAnimationCurve<Vector3>(keyframes);
  1477. }
  1478. if(importOptions.reduceKeyframes)
  1479. {
  1480. boneAnim.translation = reduceKeyframes(boneAnim.translation);
  1481. boneAnim.scale = reduceKeyframes(boneAnim.scale);
  1482. *eulerAnimation = reduceKeyframes(*eulerAnimation);
  1483. }
  1484. boneAnim.translation = AnimationUtility::scaleCurve(boneAnim.translation, importScene.scaleFactor);
  1485. boneAnim.rotation = *AnimationUtility::eulerToQuaternionCurve(eulerAnimation);
  1486. }
  1487. if (importOptions.importBlendShapes)
  1488. {
  1489. FbxMesh* fbxMesh = node->GetMesh();
  1490. if (fbxMesh != nullptr)
  1491. {
  1492. INT32 deformerCount = fbxMesh->GetDeformerCount(FbxDeformer::eBlendShape);
  1493. for (INT32 i = 0; i < deformerCount; i++)
  1494. {
  1495. FbxBlendShape* deformer = static_cast<FbxBlendShape*>(fbxMesh->GetDeformer(i, FbxDeformer::eBlendShape));
  1496. INT32 channelCount = deformer->GetBlendShapeChannelCount();
  1497. for (INT32 j = 0; j < channelCount; j++)
  1498. {
  1499. FbxBlendShapeChannel* channel = deformer->GetBlendShapeChannel(j);
  1500. FbxAnimCurve* curve = fbxMesh->GetShapeChannel(i, j, layer);
  1501. if (curve != nullptr && curve->KeyGetCount() > 0)
  1502. {
  1503. clip.blendShapeAnimations.push_back(FBXBlendShapeAnimation());
  1504. FBXBlendShapeAnimation& blendShapeAnim = clip.blendShapeAnimations.back();
  1505. blendShapeAnim.blendShape = channel->GetName();
  1506. // Get name without invalid characters
  1507. blendShapeAnim.blendShape = StringUtil::replaceAll(blendShapeAnim.blendShape, ".", "_");
  1508. blendShapeAnim.blendShape = StringUtil::replaceAll(blendShapeAnim.blendShape, "/", "_");
  1509. FbxAnimCurve* curves[1] = { curve };
  1510. float defaultValues[1] = { 0.0f };
  1511. blendShapeAnim.curve = importCurve<float, 1>(curves, defaultValues, importOptions, clip.start,
  1512. clip.end);
  1513. // FBX contains data in [0, 100] range, but we need it in [0, 1] range
  1514. blendShapeAnim.curve = AnimationUtility::scaleCurve(blendShapeAnim.curve, 0.01f);
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. UINT32 childCount = (UINT32)node->GetChildCount();
  1521. for (UINT32 i = 0; i < childCount; i++)
  1522. {
  1523. FbxNode* child = node->GetChild(i);
  1524. importAnimations(layer, child, importOptions, clip, importScene);
  1525. }
  1526. }
  1527. void FBXImporter::bakeTransforms(FbxScene* scene)
  1528. {
  1529. // FBX stores transforms in a more complex way than just translation-rotation-scale as used by Banshee.
  1530. // Instead they also support rotations offsets and pivots, scaling pivots and more. We wish to bake all this data
  1531. // into a standard transform so we can access it using node's local TRS properties (e.g. FbxNode::LclTranslation).
  1532. double frameRate = FbxTime::GetFrameRate(scene->GetGlobalSettings().GetTimeMode());
  1533. bs_frame_mark();
  1534. {
  1535. FrameStack<FbxNode*> todo;
  1536. todo.push(scene->GetRootNode());
  1537. while(todo.size() > 0)
  1538. {
  1539. FbxNode* node = todo.top();
  1540. todo.pop();
  1541. FbxVector4 zero(0, 0, 0);
  1542. FbxVector4 one(1, 1, 1);
  1543. // Activate pivot converting
  1544. node->SetPivotState(FbxNode::eSourcePivot, FbxNode::ePivotActive);
  1545. node->SetPivotState(FbxNode::eDestinationPivot, FbxNode::ePivotActive);
  1546. // We want to set all these to 0 (1 for scale) and bake them into the transforms
  1547. node->SetPostRotation(FbxNode::eDestinationPivot, zero);
  1548. node->SetPreRotation(FbxNode::eDestinationPivot, zero);
  1549. node->SetRotationOffset(FbxNode::eDestinationPivot, zero);
  1550. node->SetScalingOffset(FbxNode::eDestinationPivot, zero);
  1551. node->SetRotationPivot(FbxNode::eDestinationPivot, zero);
  1552. node->SetScalingPivot(FbxNode::eDestinationPivot, zero);
  1553. // We account for geometric properties separately during node traversal
  1554. node->SetGeometricTranslation(FbxNode::eDestinationPivot, node->GetGeometricTranslation(FbxNode::eSourcePivot));
  1555. node->SetGeometricRotation(FbxNode::eDestinationPivot, node->GetGeometricRotation(FbxNode::eSourcePivot));
  1556. node->SetGeometricScaling(FbxNode::eDestinationPivot, node->GetGeometricScaling(FbxNode::eSourcePivot));
  1557. // Banshee assumes euler angles are in YXZ order
  1558. node->SetRotationOrder(FbxNode::eDestinationPivot, FbxEuler::eOrderYXZ);
  1559. // Keep interpolation as is
  1560. node->SetQuaternionInterpolation(FbxNode::eDestinationPivot, node->GetQuaternionInterpolation(FbxNode::eSourcePivot));
  1561. for (int i = 0; i < node->GetChildCount(); i++)
  1562. {
  1563. FbxNode* childNode = node->GetChild(i);
  1564. todo.push(childNode);
  1565. }
  1566. }
  1567. scene->GetRootNode()->ConvertPivotAnimationRecursive(nullptr, FbxNode::eDestinationPivot, frameRate, false);
  1568. }
  1569. bs_frame_clear();
  1570. }
  1571. TAnimationCurve<Vector3> FBXImporter::reduceKeyframes(TAnimationCurve<Vector3>& curve)
  1572. {
  1573. UINT32 keyCount = curve.getNumKeyFrames();
  1574. Vector<TKeyframe<Vector3>> newKeyframes;
  1575. bool lastWasEqual = false;
  1576. for (UINT32 i = 0; i < keyCount; i++)
  1577. {
  1578. bool isEqual = true;
  1579. const TKeyframe<Vector3>& curKey = curve.getKeyFrame(i);
  1580. if (i > 0)
  1581. {
  1582. TKeyframe<Vector3>& prevKey = newKeyframes.back();
  1583. isEqual = Math::approxEquals(prevKey.value, curKey.value) &&
  1584. Math::approxEquals(prevKey.outTangent, curKey.inTangent) && isEqual;
  1585. }
  1586. else
  1587. isEqual = false;
  1588. // More than two keys in a row are equal, remove previous key by replacing it with this one
  1589. if (lastWasEqual && isEqual)
  1590. {
  1591. TKeyframe<Vector3>& prevKey = newKeyframes.back();
  1592. // Other properties are guaranteed unchanged
  1593. prevKey.time = curKey.time;
  1594. prevKey.outTangent = curKey.outTangent;
  1595. continue;
  1596. }
  1597. newKeyframes.push_back(curKey);
  1598. lastWasEqual = isEqual;
  1599. }
  1600. return TAnimationCurve<Vector3>(newKeyframes);
  1601. }
  1602. template<class T>
  1603. void setKeyframeValues(TKeyframe<T>& keyFrame, int idx, float value, float inTangent, float outTangent)
  1604. {
  1605. keyFrame.value = value;
  1606. keyFrame.inTangent = inTangent;
  1607. keyFrame.outTangent = outTangent;
  1608. }
  1609. template<>
  1610. void setKeyframeValues<Vector3>(TKeyframe<Vector3>& keyFrame, int idx, float value, float inTangent, float outTangent)
  1611. {
  1612. keyFrame.value[idx] = value;
  1613. keyFrame.inTangent[idx] = inTangent;
  1614. keyFrame.outTangent[idx] = outTangent;
  1615. }
  1616. template<class T, int C>
  1617. TAnimationCurve<T> FBXImporter::importCurve(FbxAnimCurve*(&fbxCurve)[C], float (&defaultValues)[C],
  1618. FBXImportOptions& importOptions, float start, float end)
  1619. {
  1620. int keyCounts[C];
  1621. for (int i = 0; i < C; i++)
  1622. {
  1623. if (fbxCurve[i] != nullptr)
  1624. keyCounts[i] = fbxCurve[i]->KeyGetCount();
  1625. else
  1626. keyCounts[i] = 0;
  1627. }
  1628. // If curve key-counts don't match, we need to force resampling
  1629. bool forceResample = false;
  1630. if (!forceResample)
  1631. {
  1632. for (int i = 1; i < C; i++)
  1633. {
  1634. forceResample |= keyCounts[i - 1] != keyCounts[i];
  1635. if (forceResample)
  1636. break;
  1637. }
  1638. }
  1639. // Read keys directly
  1640. if(!importOptions.animResample && !forceResample)
  1641. {
  1642. bool foundMismatch = false;
  1643. int keyCount = keyCounts[0];
  1644. Vector<TKeyframe<T>> keyframes;
  1645. for (int i = 0; i < keyCount; i++)
  1646. {
  1647. FbxTime fbxTime = fbxCurve[0]->KeyGetTime(i);
  1648. float time = (float)fbxTime.GetSecondDouble();
  1649. // Ensure times from other curves match
  1650. for (int j = 1; j < C; j++)
  1651. {
  1652. fbxTime = fbxCurve[j]->KeyGetTime(i);
  1653. float otherTime = (float)fbxTime.GetSecondDouble();
  1654. if (!Math::approxEquals(time, otherTime))
  1655. {
  1656. foundMismatch = true;
  1657. break;
  1658. }
  1659. }
  1660. if(foundMismatch)
  1661. break;
  1662. if (time < start || time > end)
  1663. continue;
  1664. keyframes.push_back(TKeyframe<T>());
  1665. TKeyframe<T>& keyFrame = keyframes.back();
  1666. keyFrame.time = time;
  1667. for (int j = 0; j < C; j++)
  1668. {
  1669. setKeyframeValues(keyFrame, j,
  1670. fbxCurve[j]->KeyGetValue(i),
  1671. fbxCurve[j]->KeyGetLeftDerivative(i),
  1672. fbxCurve[j]->KeyGetRightDerivative(i));
  1673. }
  1674. }
  1675. if (!foundMismatch)
  1676. return TAnimationCurve<T>(keyframes);
  1677. else
  1678. forceResample = true;
  1679. }
  1680. if (!importOptions.animResample && forceResample)
  1681. LOGWRN("Animation has different keyframes for different curve components, forcing resampling.");
  1682. // Resample keys
  1683. float curveStart = std::numeric_limits<float>::infinity();
  1684. float curveEnd = -std::numeric_limits<float>::infinity();
  1685. for (INT32 i = 0; i < C; i++)
  1686. {
  1687. if(fbxCurve[i] == nullptr)
  1688. {
  1689. curveStart = std::min(0.0f, curveStart);
  1690. curveEnd = std::max(0.0f, curveEnd);
  1691. continue;
  1692. }
  1693. int keyCount = keyCounts[i];
  1694. for (INT32 j = 0; j < keyCount; j++)
  1695. {
  1696. FbxTime fbxTime = fbxCurve[i]->KeyGetTime(j);
  1697. float time = (float)fbxTime.GetSecondDouble();
  1698. curveStart = std::min(time, curveStart);
  1699. curveEnd = std::max(time, curveEnd);
  1700. }
  1701. }
  1702. curveStart = Math::clamp(curveStart, start, end);
  1703. curveEnd = Math::clamp(curveEnd, start, end);
  1704. float curveLength = curveEnd - curveStart;
  1705. INT32 numSamples = Math::ceilToInt(curveLength / importOptions.animSampleRate);
  1706. // We don't use the exact provided sample rate but instead modify it slightly so it
  1707. // completely covers the curve range including start/end points while maintaining
  1708. // constant time step between keyframes.
  1709. float dt = curveLength / (float)numSamples;
  1710. INT32 lastKeyframe[] = { 0, 0, 0 };
  1711. INT32 lastLeftTangent[] = { 0, 0, 0 };
  1712. INT32 lastRightTangent[] = { 0, 0, 0 };
  1713. Vector<TKeyframe<T>> keyframes(numSamples);
  1714. for (INT32 i = 0; i < numSamples; i++)
  1715. {
  1716. float sampleTime = std::min(curveStart + i * dt, curveEnd);
  1717. FbxTime fbxSampleTime;
  1718. fbxSampleTime.SetSecondDouble(sampleTime);
  1719. TKeyframe<T>& keyFrame = keyframes[i];
  1720. keyFrame.time = sampleTime;
  1721. for (int j = 0; j < C; j++)
  1722. {
  1723. if (fbxCurve[j] != nullptr)
  1724. {
  1725. setKeyframeValues(keyFrame, j,
  1726. fbxCurve[j]->Evaluate(fbxSampleTime, &lastKeyframe[j]),
  1727. fbxCurve[j]->EvaluateLeftDerivative(fbxSampleTime, &lastLeftTangent[j]),
  1728. fbxCurve[j]->EvaluateRightDerivative(fbxSampleTime, &lastRightTangent[j]));
  1729. }
  1730. else
  1731. {
  1732. setKeyframeValues(keyFrame, j, defaultValues[C], 0.0f, 0.0f);
  1733. }
  1734. }
  1735. }
  1736. return TAnimationCurve<T>(keyframes);
  1737. }
  1738. }