FBXConverter.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXDConverter.h
  34. * @brief FBX DOM to aiScene conversion
  35. */
  36. #ifndef INCLUDED_AI_FBX_CONVERTER_H
  37. #define INCLUDED_AI_FBX_CONVERTER_H
  38. #include "FBXParser.h"
  39. #include "FBXMeshGeometry.h"
  40. #include "FBXDocument.h"
  41. #include "FBXUtil.h"
  42. #include "FBXProperties.h"
  43. #include "FBXImporter.h"
  44. #include <assimp/anim.h>
  45. #include <assimp/material.h>
  46. #include <assimp/light.h>
  47. #include <assimp/texture.h>
  48. #include <assimp/camera.h>
  49. #include <assimp/StringComparison.h>
  50. #include <unordered_map>
  51. #include <unordered_set>
  52. struct aiScene;
  53. struct aiNode;
  54. struct aiMaterial;
  55. struct morphKeyData {
  56. std::vector<unsigned int> values;
  57. std::vector<float> weights;
  58. };
  59. using morphAnimData = std::map<int64_t, morphKeyData*> ;
  60. namespace Assimp {
  61. namespace FBX {
  62. class MeshGeometry;
  63. using SkeletonBoneArray = std::vector<aiSkeletonBone *>;
  64. using SkeletonBoneToMesh = std::map<aiMesh*, SkeletonBoneArray*>;
  65. struct SkeletonBoneContainer {
  66. std::vector<aiMesh *> MeshArray;
  67. SkeletonBoneToMesh SkeletonBoneToMeshLookup;
  68. };
  69. class Document;
  70. /**
  71. * Convert a FBX #Document to #aiScene
  72. * @param out Empty scene to be populated
  73. * @param doc Parsed FBX document
  74. * @param removeEmptyBones Will remove bones, which do not have any references to vertices.
  75. */
  76. void ConvertToAssimpScene(aiScene* out, const Document& doc, bool removeEmptyBones);
  77. /** Dummy class to encapsulate the conversion process */
  78. class FBXConverter {
  79. public:
  80. /**
  81. * The different parts that make up the final local transformation of a fbx-node
  82. */
  83. enum TransformationComp {
  84. TransformationComp_GeometricScalingInverse = 0,
  85. TransformationComp_GeometricRotationInverse,
  86. TransformationComp_GeometricTranslationInverse,
  87. TransformationComp_Translation,
  88. TransformationComp_RotationOffset,
  89. TransformationComp_RotationPivot,
  90. TransformationComp_PreRotation,
  91. TransformationComp_Rotation,
  92. TransformationComp_PostRotation,
  93. TransformationComp_RotationPivotInverse,
  94. TransformationComp_ScalingOffset,
  95. TransformationComp_ScalingPivot,
  96. TransformationComp_Scaling,
  97. TransformationComp_ScalingPivotInverse,
  98. TransformationComp_GeometricTranslation,
  99. TransformationComp_GeometricRotation,
  100. TransformationComp_GeometricScaling,
  101. TransformationComp_MAXIMUM
  102. };
  103. public:
  104. FBXConverter(aiScene* out, const Document& doc, bool removeEmptyBones);
  105. ~FBXConverter();
  106. private:
  107. // ------------------------------------------------------------------------------------------------
  108. // find scene root and trigger recursive scene conversion
  109. void ConvertRootNode();
  110. // ------------------------------------------------------------------------------------------------
  111. // collect and assign child nodes
  112. void ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node, const aiMatrix4x4& parent_transform = aiMatrix4x4());
  113. // ------------------------------------------------------------------------------------------------
  114. void ConvertLights(const Model& model, const std::string &orig_name );
  115. // ------------------------------------------------------------------------------------------------
  116. void ConvertCameras(const Model& model, const std::string &orig_name );
  117. // ------------------------------------------------------------------------------------------------
  118. void ConvertLight( const Light& light, const std::string &orig_name );
  119. // ------------------------------------------------------------------------------------------------
  120. void ConvertCamera( const Camera& cam, const std::string &orig_name );
  121. // ------------------------------------------------------------------------------------------------
  122. void GetUniqueName( const std::string &name, std::string& uniqueName );
  123. // ------------------------------------------------------------------------------------------------
  124. // this returns unified names usable within assimp identifiers (i.e. no space characters -
  125. // while these would be allowed, they are a potential trouble spot so better not use them).
  126. const char* NameTransformationComp(TransformationComp comp);
  127. // ------------------------------------------------------------------------------------------------
  128. // Returns an unique name for a node or traverses up a hierarchy until a non-empty name is found and
  129. // then makes this name unique
  130. std::string MakeUniqueNodeName(const Model* const model, const aiNode& parent);
  131. // ------------------------------------------------------------------------------------------------
  132. // note: this returns the REAL fbx property names
  133. const char* NameTransformationCompProperty(TransformationComp comp);
  134. // ------------------------------------------------------------------------------------------------
  135. aiVector3D TransformationCompDefaultValue(TransformationComp comp);
  136. // ------------------------------------------------------------------------------------------------
  137. void GetRotationMatrix(Model::RotOrder mode, const aiVector3D& rotation, aiMatrix4x4& out);
  138. // ------------------------------------------------------------------------------------------------
  139. /**
  140. * checks if a node has more than just scaling, rotation and translation components
  141. */
  142. bool NeedsComplexTransformationChain(const Model& model);
  143. // ------------------------------------------------------------------------------------------------
  144. // note: name must be a FixNodeName() result
  145. std::string NameTransformationChainNode(const std::string& name, TransformationComp comp);
  146. // ------------------------------------------------------------------------------------------------
  147. /**
  148. * note: memory for output_nodes is managed by the caller, via the PotentialNode struct.
  149. */
  150. struct PotentialNode;
  151. bool GenerateTransformationNodeChain(const Model& model, const std::string& name, std::vector<PotentialNode>& output_nodes, std::vector<PotentialNode>& post_output_nodes);
  152. // ------------------------------------------------------------------------------------------------
  153. void SetupNodeMetadata(const Model& model, aiNode& nd);
  154. // ------------------------------------------------------------------------------------------------
  155. void ConvertModel(const Model &model, aiNode *parent, aiNode *root_node, const aiMatrix4x4 &absolute_transform);
  156. // ------------------------------------------------------------------------------------------------
  157. // MeshGeometry -> aiMesh, return mesh index + 1 or 0 if the conversion failed
  158. std::vector<unsigned int>
  159. ConvertMesh(const MeshGeometry &mesh, const Model &model, aiNode *parent, aiNode *root_node, const aiMatrix4x4 &absolute_transform);
  160. // ------------------------------------------------------------------------------------------------
  161. std::vector<unsigned int> ConvertLine(const LineGeometry& line, aiNode *root_node);
  162. // ------------------------------------------------------------------------------------------------
  163. aiMesh* SetupEmptyMesh(const Geometry& mesh, aiNode *parent);
  164. // ------------------------------------------------------------------------------------------------
  165. unsigned int ConvertMeshSingleMaterial(const MeshGeometry &mesh, const Model &model, const aiMatrix4x4 &absolute_transform,
  166. aiNode *parent, aiNode *root_node);
  167. // ------------------------------------------------------------------------------------------------
  168. std::vector<unsigned int>
  169. ConvertMeshMultiMaterial(const MeshGeometry &mesh, const Model &model, const aiMatrix4x4 &absolute_transform, aiNode *parent, aiNode *root_node);
  170. // ------------------------------------------------------------------------------------------------
  171. unsigned int ConvertMeshMultiMaterial(const MeshGeometry &mesh, const Model &model, const aiMatrix4x4 &absolute_transform, MatIndexArray::value_type index,
  172. aiNode *parent, aiNode *root_node);
  173. // ------------------------------------------------------------------------------------------------
  174. static const unsigned int NO_MATERIAL_SEPARATION = /* std::numeric_limits<unsigned int>::max() */
  175. static_cast<unsigned int>(-1);
  176. // ------------------------------------------------------------------------------------------------
  177. /**
  178. * - if materialIndex == NO_MATERIAL_SEPARATION, materials are not taken into
  179. * account when determining which weights to include.
  180. * - outputVertStartIndices is only used when a material index is specified, it gives for
  181. * each output vertex the DOM index it maps to.
  182. */
  183. void ConvertWeights(aiMesh *out, const MeshGeometry &geo, const aiMatrix4x4 &absolute_transform, aiNode *parent = nullptr,
  184. unsigned int materialIndex = NO_MATERIAL_SEPARATION,
  185. std::vector<unsigned int> *outputVertStartIndices = nullptr);
  186. // ------------------------------------------------------------------------------------------------
  187. void ConvertWeightsToSkeleton(aiMesh *out, const MeshGeometry &geo, const aiMatrix4x4 &absolute_transform,
  188. aiNode *parent, unsigned int materialIndex, std::vector<unsigned int> *outputVertStartIndices,
  189. SkeletonBoneContainer &skeletonContainer);
  190. // ------------------------------------------------------------------------------------------------
  191. void ConvertCluster(std::vector<aiBone *> &local_mesh_bones, const Cluster *cl,
  192. std::vector<size_t> &out_indices, std::vector<size_t> &index_out_indices,
  193. std::vector<size_t> &count_out_indices, const aiMatrix4x4 &absolute_transform, aiNode *parent);
  194. // ------------------------------------------------------------------------------------------------
  195. void ConvertMaterialForMesh(aiMesh* out, const Model& model, const MeshGeometry& geo,
  196. MatIndexArray::value_type materialIndex);
  197. // ------------------------------------------------------------------------------------------------
  198. unsigned int GetDefaultMaterial();
  199. // ------------------------------------------------------------------------------------------------
  200. // Material -> aiMaterial
  201. unsigned int ConvertMaterial(const Material& material, const MeshGeometry* const mesh);
  202. // ------------------------------------------------------------------------------------------------
  203. // Video -> aiTexture
  204. unsigned int ConvertVideo(const Video& video);
  205. // ------------------------------------------------------------------------------------------------
  206. // convert embedded texture if necessary and return actual texture path
  207. aiString GetTexturePath(const Texture* tex);
  208. // ------------------------------------------------------------------------------------------------
  209. void TrySetTextureProperties(aiMaterial* out_mat, const TextureMap& textures,
  210. const std::string& propName,
  211. aiTextureType target, const MeshGeometry* const mesh);
  212. // ------------------------------------------------------------------------------------------------
  213. void TrySetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures,
  214. const std::string& propName,
  215. aiTextureType target, const MeshGeometry* const mesh);
  216. // ------------------------------------------------------------------------------------------------
  217. void SetTextureProperties(aiMaterial* out_mat, const TextureMap& textures, const MeshGeometry* const mesh);
  218. // ------------------------------------------------------------------------------------------------
  219. void SetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures, const MeshGeometry* const mesh);
  220. // ------------------------------------------------------------------------------------------------
  221. aiColor3D GetColorPropertyFromMaterial(const PropertyTable& props, const std::string& baseName,
  222. bool& result);
  223. aiColor3D GetColorPropertyFactored(const PropertyTable& props, const std::string& colorName,
  224. const std::string& factorName, bool& result, bool useTemplate = true);
  225. aiColor3D GetColorProperty(const PropertyTable& props, const std::string& colorName,
  226. bool& result, bool useTemplate = true);
  227. // ------------------------------------------------------------------------------------------------
  228. void SetShadingPropertiesCommon(aiMaterial* out_mat, const PropertyTable& props);
  229. void SetShadingPropertiesRaw(aiMaterial* out_mat, const PropertyTable& props, const TextureMap& textures, const MeshGeometry* const mesh);
  230. // ------------------------------------------------------------------------------------------------
  231. // get the number of fps for a FrameRate enumerated value
  232. static double FrameRateToDouble(FileGlobalSettings::FrameRate fp, double customFPSVal = -1.0);
  233. // ------------------------------------------------------------------------------------------------
  234. // convert animation data to aiAnimation et al
  235. void ConvertAnimations();
  236. // ------------------------------------------------------------------------------------------------
  237. // takes a fbx node name and returns the identifier to be used in the assimp output scene.
  238. // the function is guaranteed to provide consistent results over multiple invocations
  239. // UNLESS RenameNode() is called for a particular node name.
  240. std::string FixNodeName(const std::string& name);
  241. std::string FixAnimMeshName(const std::string& name);
  242. typedef std::map<const AnimationCurveNode*, const AnimationLayer*> LayerMap;
  243. // XXX: better use multi_map ..
  244. typedef std::map<std::string, std::vector<const AnimationCurveNode*> > NodeMap;
  245. // ------------------------------------------------------------------------------------------------
  246. void ConvertAnimationStack(const AnimationStack& st);
  247. // ------------------------------------------------------------------------------------------------
  248. void ProcessMorphAnimDatas(std::map<std::string, morphAnimData*>* morphAnimDatas,
  249. const BlendShapeChannel* bsc, const AnimationCurveNode* node);
  250. // ------------------------------------------------------------------------------------------------
  251. void GenerateNodeAnimations(std::vector<aiNodeAnim*>& node_anims,
  252. const std::string& fixed_name,
  253. const std::vector<const AnimationCurveNode*>& curves,
  254. const LayerMap& layer_map,
  255. int64_t start, int64_t stop,
  256. double& max_time,
  257. double& min_time);
  258. // ------------------------------------------------------------------------------------------------
  259. bool IsRedundantAnimationData(const Model& target,
  260. TransformationComp comp,
  261. const std::vector<const AnimationCurveNode*>& curves);
  262. // ------------------------------------------------------------------------------------------------
  263. aiNodeAnim* GenerateRotationNodeAnim(const std::string& name,
  264. const Model& target,
  265. const std::vector<const AnimationCurveNode*>& curves,
  266. const LayerMap& layer_map,
  267. int64_t start, int64_t stop,
  268. double& max_time,
  269. double& min_time);
  270. // ------------------------------------------------------------------------------------------------
  271. aiNodeAnim* GenerateScalingNodeAnim(const std::string& name,
  272. const Model& /*target*/,
  273. const std::vector<const AnimationCurveNode*>& curves,
  274. const LayerMap& layer_map,
  275. int64_t start, int64_t stop,
  276. double& max_time,
  277. double& min_time);
  278. // ------------------------------------------------------------------------------------------------
  279. aiNodeAnim* GenerateTranslationNodeAnim(const std::string& name,
  280. const Model& /*target*/,
  281. const std::vector<const AnimationCurveNode*>& curves,
  282. const LayerMap& layer_map,
  283. int64_t start, int64_t stop,
  284. double& max_time,
  285. double& min_time,
  286. bool inverse = false);
  287. // ------------------------------------------------------------------------------------------------
  288. // generate node anim, extracting only Rotation, Scaling and Translation from the given chain
  289. aiNodeAnim* GenerateSimpleNodeAnim(const std::string& name,
  290. const Model& target,
  291. NodeMap::const_iterator chain[TransformationComp_MAXIMUM],
  292. NodeMap::const_iterator iterEnd,
  293. int64_t start, int64_t stop,
  294. double& maxTime,
  295. double& minTime);
  296. // key (time), value, mapto (component index)
  297. typedef std::tuple<std::shared_ptr<KeyTimeList>, std::shared_ptr<KeyValueList>, unsigned int > KeyFrameList;
  298. typedef std::vector<KeyFrameList> KeyFrameListList;
  299. // ------------------------------------------------------------------------------------------------
  300. KeyFrameListList GetKeyframeList(const std::vector<const AnimationCurveNode*>& nodes, int64_t start, int64_t stop);
  301. KeyFrameListList GetRotationKeyframeList(const std::vector<const AnimationCurveNode*>& nodes, int64_t start, int64_t stop);
  302. // ------------------------------------------------------------------------------------------------
  303. KeyTimeList GetKeyTimeList(const KeyFrameListList& inputs);
  304. // ------------------------------------------------------------------------------------------------
  305. void InterpolateKeys(aiVectorKey* valOut, const KeyTimeList& keys, const KeyFrameListList& inputs,
  306. const aiVector3D& def_value,
  307. double& max_time,
  308. double& min_time);
  309. // ------------------------------------------------------------------------------------------------
  310. void InterpolateKeys(aiQuatKey* valOut, const KeyTimeList& keys, const KeyFrameListList& inputs,
  311. const aiVector3D& def_value,
  312. double& maxTime,
  313. double& minTime,
  314. Model::RotOrder order);
  315. // ------------------------------------------------------------------------------------------------
  316. // euler xyz -> quat
  317. aiQuaternion EulerToQuaternion(const aiVector3D& rot, Model::RotOrder order);
  318. // ------------------------------------------------------------------------------------------------
  319. void ConvertScaleKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes, const LayerMap& /*layers*/,
  320. int64_t start, int64_t stop,
  321. double& maxTime,
  322. double& minTime);
  323. // ------------------------------------------------------------------------------------------------
  324. void ConvertTranslationKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes,
  325. const LayerMap& /*layers*/,
  326. int64_t start, int64_t stop,
  327. double& maxTime,
  328. double& minTime);
  329. // ------------------------------------------------------------------------------------------------
  330. void ConvertRotationKeys(aiNodeAnim* na, const std::vector<const AnimationCurveNode*>& nodes,
  331. const LayerMap& /*layers*/,
  332. int64_t start, int64_t stop,
  333. double& maxTime,
  334. double& minTime,
  335. Model::RotOrder order);
  336. // ------------------------------------------------------------------------------------------------
  337. // Copy global geometric data and some information about the source asset into scene metadata.
  338. void ConvertGlobalSettings();
  339. // ------------------------------------------------------------------------------------------------
  340. // copy generated meshes, animations, lights, cameras and textures to the output scene
  341. void TransferDataToScene();
  342. // ------------------------------------------------------------------------------------------------
  343. // FBX file could have embedded textures not connected to anything
  344. void ConvertOrphanedEmbeddedTextures();
  345. private:
  346. // 0: not assigned yet, others: index is value - 1
  347. unsigned int defaultMaterialIndex;
  348. std::vector<aiMesh*> mMeshes;
  349. std::vector<aiMaterial*> materials;
  350. std::vector<aiAnimation*> animations;
  351. std::vector<aiLight*> lights;
  352. std::vector<aiCamera*> cameras;
  353. std::vector<aiTexture*> textures;
  354. using MaterialMap = std::fbx_unordered_map<const Material*, unsigned int>;
  355. MaterialMap materials_converted;
  356. using VideoMap = std::fbx_unordered_map<const Video*, unsigned int>;
  357. VideoMap textures_converted;
  358. using MeshMap = std::fbx_unordered_map<const Geometry*, std::vector<unsigned int> >;
  359. MeshMap meshes_converted;
  360. // fixed node name -> which trafo chain components have animations?
  361. using NodeAnimBitMap = std::fbx_unordered_map<std::string, unsigned int> ;
  362. NodeAnimBitMap node_anim_chain_bits;
  363. // number of nodes with the same name
  364. using NodeNameCache = std::fbx_unordered_map<std::string, unsigned int>;
  365. NodeNameCache mNodeNames;
  366. // Deformer name is not the same as a bone name - it does contain the bone name though :)
  367. // Deformer names in FBX are always unique in an FBX file.
  368. std::map<const std::string, aiBone *> bone_map;
  369. double anim_fps;
  370. std::vector<aiSkeleton *> mSkeletons;
  371. aiScene* const mSceneOut;
  372. const FBX::Document& doc;
  373. bool mRemoveEmptyBones;
  374. static void BuildBoneList(aiNode *current_node, const aiNode *root_node, const aiScene *scene,
  375. std::vector<aiBone*>& bones);
  376. void BuildBoneStack(aiNode *current_node, const aiNode *root_node, const aiScene *scene,
  377. const std::vector<aiBone *> &bones,
  378. std::map<aiBone *, aiNode *> &bone_stack,
  379. std::vector<aiNode*> &node_stack );
  380. static void BuildNodeList(aiNode *current_node, std::vector<aiNode *> &nodes);
  381. static aiNode *GetNodeFromStack(const aiString &node_name, std::vector<aiNode *> &nodes);
  382. static aiNode *GetArmatureRoot(aiNode *bone_node, std::vector<aiBone*> &bone_list);
  383. static bool IsBoneNode(const aiString &bone_name, std::vector<aiBone *> &bones);
  384. };
  385. }
  386. }
  387. #endif // INCLUDED_AI_FBX_CONVERTER_H