BsFBXImporter.cpp 68 KB

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