BsFBXImporter.cpp 61 KB

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