FBXDocument.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*************************************************************************/
  2. /* FBXDocument.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. /** @file FBXDocument.h
  31. * @brief FBX DOM
  32. */
  33. #ifndef FBX_DOCUMENT_H
  34. #define FBX_DOCUMENT_H
  35. #include "FBXCommon.h"
  36. #include "FBXParser.h"
  37. #include "FBXProperties.h"
  38. #include "core/math/transform_3d.h"
  39. #include "core/math/vector2.h"
  40. #include "core/math/vector3.h"
  41. #include "core/string/print_string.h"
  42. #include <stdint.h>
  43. #include <numeric>
  44. #define _AI_CONCAT(a, b) a##b
  45. #define AI_CONCAT(a, b) _AI_CONCAT(a, b)
  46. namespace FBXDocParser {
  47. class Parser;
  48. class Object;
  49. struct ImportSettings;
  50. class Connection;
  51. class PropertyTable;
  52. class Document;
  53. class Material;
  54. class ShapeGeometry;
  55. class LineGeometry;
  56. class Geometry;
  57. class Video;
  58. class AnimationCurve;
  59. class AnimationCurveNode;
  60. class AnimationLayer;
  61. class AnimationStack;
  62. class BlendShapeChannel;
  63. class BlendShape;
  64. class Skin;
  65. class Cluster;
  66. typedef Object *ObjectPtr;
  67. #define new_Object new Object
  68. /** Represents a delay-parsed FBX objects. Many objects in the scene
  69. * are not needed by assimp, so it makes no sense to parse them
  70. * upfront. */
  71. class LazyObject {
  72. public:
  73. LazyObject(uint64_t id, const ElementPtr element, const Document &doc);
  74. ~LazyObject();
  75. ObjectPtr LoadObject();
  76. /* Casting weak pointers to their templated type safely and preserving ref counting and safety
  77. * with lock() keyword to prevent leaking memory
  78. */
  79. template <typename T>
  80. const T *Get() {
  81. ObjectPtr ob = LoadObject();
  82. return dynamic_cast<const T *>(ob);
  83. }
  84. uint64_t ID() const {
  85. return id;
  86. }
  87. bool IsBeingConstructed() const {
  88. return (flags & BEING_CONSTRUCTED) != 0;
  89. }
  90. bool FailedToConstruct() const {
  91. return (flags & FAILED_TO_CONSTRUCT) != 0;
  92. }
  93. ElementPtr GetElement() const {
  94. return element;
  95. }
  96. const Document &GetDocument() const {
  97. return doc;
  98. }
  99. private:
  100. const Document &doc;
  101. ElementPtr element = nullptr;
  102. std::shared_ptr<Object> object = nullptr;
  103. const uint64_t id = 0;
  104. enum Flags {
  105. BEING_CONSTRUCTED = 0x1,
  106. FAILED_TO_CONSTRUCT = 0x2
  107. };
  108. unsigned int flags = 0;
  109. };
  110. /** Base class for in-memory (DOM) representations of FBX objects */
  111. class Object : public PropertyTable {
  112. public:
  113. Object(uint64_t id, const ElementPtr element, const std::string &name);
  114. virtual ~Object();
  115. ElementPtr SourceElement() const {
  116. return element;
  117. }
  118. const std::string &Name() const {
  119. return name;
  120. }
  121. uint64_t ID() const {
  122. return id;
  123. }
  124. protected:
  125. const ElementPtr element = nullptr;
  126. const std::string name;
  127. const uint64_t id;
  128. };
  129. /** DOM class for generic FBX NoteAttribute blocks. NoteAttribute's just hold a property table,
  130. * fixed members are added by deriving classes. */
  131. class NodeAttribute : public Object {
  132. public:
  133. NodeAttribute(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  134. virtual ~NodeAttribute();
  135. };
  136. /** DOM base class for FBX camera settings attached to a node */
  137. class CameraSwitcher : public NodeAttribute {
  138. public:
  139. CameraSwitcher(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  140. virtual ~CameraSwitcher();
  141. int CameraID() const {
  142. return cameraId;
  143. }
  144. const std::string &CameraName() const {
  145. return cameraName;
  146. }
  147. const std::string &CameraIndexName() const {
  148. return cameraIndexName;
  149. }
  150. private:
  151. int cameraId = 0;
  152. std::string cameraName;
  153. std::string cameraIndexName;
  154. };
  155. #define fbx_stringize(a) #a
  156. #define fbx_simple_property(name, type, default_value) \
  157. type name() const { \
  158. return PropertyGet<type>(this, fbx_stringize(name), (default_value)); \
  159. }
  160. // XXX improve logging
  161. #define fbx_simple_enum_property(name, type, default_value) \
  162. type name() const { \
  163. const int ival = PropertyGet<int>(this, fbx_stringize(name), static_cast<int>(default_value)); \
  164. if (ival < 0 || ival >= AI_CONCAT(type, _MAX)) { \
  165. return static_cast<type>(default_value); \
  166. } \
  167. return static_cast<type>(ival); \
  168. }
  169. class FbxPoseNode;
  170. class FbxPose : public Object {
  171. public:
  172. FbxPose(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  173. const std::vector<FbxPoseNode *> &GetBindPoses() const {
  174. return pose_nodes;
  175. }
  176. virtual ~FbxPose();
  177. private:
  178. std::vector<FbxPoseNode *> pose_nodes;
  179. };
  180. class FbxPoseNode {
  181. public:
  182. FbxPoseNode(const ElementPtr element, const Document &doc, const std::string &name) {
  183. const ScopePtr sc = GetRequiredScope(element);
  184. // get pose node transform
  185. const ElementPtr Transform = GetRequiredElement(sc, "Matrix", element);
  186. transform = ReadMatrix(Transform);
  187. // get node id this pose node is for
  188. const ElementPtr NodeId = sc->GetElement("Node3D");
  189. if (NodeId) {
  190. target_id = ParseTokenAsInt64(GetRequiredToken(NodeId, 0));
  191. }
  192. print_verbose("added posenode " + itos(target_id) + " transform: " + transform);
  193. }
  194. virtual ~FbxPoseNode() {
  195. }
  196. uint64_t GetNodeID() const {
  197. return target_id;
  198. }
  199. Transform3D GetBindPose() const {
  200. return transform;
  201. }
  202. private:
  203. uint64_t target_id = 0;
  204. Transform3D transform;
  205. };
  206. /** DOM base class for FBX cameras attached to a node */
  207. class Camera : public NodeAttribute {
  208. public:
  209. Camera(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  210. virtual ~Camera();
  211. fbx_simple_property(Position, Vector3, Vector3(0, 0, 0));
  212. fbx_simple_property(UpVector, Vector3, Vector3(0, 1, 0));
  213. fbx_simple_property(InterestPosition, Vector3, Vector3(0, 0, 0));
  214. fbx_simple_property(AspectWidth, float, 1.0f);
  215. fbx_simple_property(AspectHeight, float, 1.0f);
  216. fbx_simple_property(FilmWidth, float, 1.0f);
  217. fbx_simple_property(FilmHeight, float, 1.0f);
  218. fbx_simple_property(NearPlane, float, 0.1f);
  219. fbx_simple_property(FarPlane, float, 100.0f);
  220. fbx_simple_property(FilmAspectRatio, float, 1.0f);
  221. fbx_simple_property(ApertureMode, int, 0);
  222. fbx_simple_property(FieldOfView, float, 1.0f);
  223. fbx_simple_property(FocalLength, float, 1.0f);
  224. };
  225. /** DOM base class for FBX null markers attached to a node */
  226. class Null : public NodeAttribute {
  227. public:
  228. Null(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  229. virtual ~Null();
  230. };
  231. /** DOM base class for FBX limb node markers attached to a node */
  232. class LimbNode : public NodeAttribute {
  233. public:
  234. LimbNode(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  235. virtual ~LimbNode();
  236. };
  237. /** DOM base class for FBX lights attached to a node */
  238. class Light : public NodeAttribute {
  239. public:
  240. Light(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  241. virtual ~Light();
  242. enum Type {
  243. Type_Point,
  244. Type_Directional,
  245. Type_Spot,
  246. Type_Area,
  247. Type_Volume,
  248. Type_MAX // end-of-enum sentinel
  249. };
  250. enum Decay {
  251. Decay_None,
  252. Decay_Linear,
  253. Decay_Quadratic,
  254. Decay_Cubic,
  255. Decay_MAX // end-of-enum sentinel
  256. };
  257. fbx_simple_property(Color, Vector3, Vector3(1, 1, 1));
  258. fbx_simple_enum_property(LightType, Type, 0);
  259. fbx_simple_property(CastLightOnObject, bool, false);
  260. fbx_simple_property(DrawVolumetricLight, bool, true);
  261. fbx_simple_property(DrawGroundProjection, bool, true);
  262. fbx_simple_property(DrawFrontFacingVolumetricLight, bool, false);
  263. fbx_simple_property(Intensity, float, 100.0f);
  264. fbx_simple_property(InnerAngle, float, 0.0f);
  265. fbx_simple_property(OuterAngle, float, 45.0f);
  266. fbx_simple_property(Fog, int, 50);
  267. fbx_simple_enum_property(DecayType, Decay, 2);
  268. fbx_simple_property(DecayStart, float, 1.0f);
  269. fbx_simple_property(FileName, std::string, "");
  270. fbx_simple_property(EnableNearAttenuation, bool, false);
  271. fbx_simple_property(NearAttenuationStart, float, 0.0f);
  272. fbx_simple_property(NearAttenuationEnd, float, 0.0f);
  273. fbx_simple_property(EnableFarAttenuation, bool, false);
  274. fbx_simple_property(FarAttenuationStart, float, 0.0f);
  275. fbx_simple_property(FarAttenuationEnd, float, 0.0f);
  276. fbx_simple_property(CastShadows, bool, true);
  277. fbx_simple_property(ShadowColor, Vector3, Vector3(0, 0, 0));
  278. fbx_simple_property(AreaLightShape, int, 0);
  279. fbx_simple_property(LeftBarnDoor, float, 20.0f);
  280. fbx_simple_property(RightBarnDoor, float, 20.0f);
  281. fbx_simple_property(TopBarnDoor, float, 20.0f);
  282. fbx_simple_property(BottomBarnDoor, float, 20.0f);
  283. fbx_simple_property(EnableBarnDoor, bool, true);
  284. };
  285. class Model;
  286. typedef Model *ModelPtr;
  287. #define new_Model new Model
  288. /** DOM base class for FBX models (even though its semantics are more "node" than "model" */
  289. class Model : public Object {
  290. public:
  291. enum RotOrder {
  292. RotOrder_EulerXYZ = 0,
  293. RotOrder_EulerXZY,
  294. RotOrder_EulerYZX,
  295. RotOrder_EulerYXZ,
  296. RotOrder_EulerZXY,
  297. RotOrder_EulerZYX,
  298. RotOrder_SphericXYZ,
  299. RotOrder_MAX // end-of-enum sentinel
  300. };
  301. Model(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  302. virtual ~Model();
  303. fbx_simple_property(QuaternionInterpolate, int, 0);
  304. fbx_simple_property(RotationOffset, Vector3, Vector3());
  305. fbx_simple_property(RotationPivot, Vector3, Vector3());
  306. fbx_simple_property(ScalingOffset, Vector3, Vector3());
  307. fbx_simple_property(ScalingPivot, Vector3, Vector3());
  308. fbx_simple_property(TranslationActive, bool, false);
  309. fbx_simple_property(TranslationMin, Vector3, Vector3());
  310. fbx_simple_property(TranslationMax, Vector3, Vector3());
  311. fbx_simple_property(TranslationMinX, bool, false);
  312. fbx_simple_property(TranslationMaxX, bool, false);
  313. fbx_simple_property(TranslationMinY, bool, false);
  314. fbx_simple_property(TranslationMaxY, bool, false);
  315. fbx_simple_property(TranslationMinZ, bool, false);
  316. fbx_simple_property(TranslationMaxZ, bool, false);
  317. fbx_simple_enum_property(RotationOrder, RotOrder, 0);
  318. fbx_simple_property(RotationSpaceForLimitOnly, bool, false);
  319. fbx_simple_property(RotationStiffnessX, float, 0.0f);
  320. fbx_simple_property(RotationStiffnessY, float, 0.0f);
  321. fbx_simple_property(RotationStiffnessZ, float, 0.0f);
  322. fbx_simple_property(AxisLen, float, 0.0f);
  323. fbx_simple_property(PreRotation, Vector3, Vector3());
  324. fbx_simple_property(PostRotation, Vector3, Vector3());
  325. fbx_simple_property(RotationActive, bool, false);
  326. fbx_simple_property(RotationMin, Vector3, Vector3());
  327. fbx_simple_property(RotationMax, Vector3, Vector3());
  328. fbx_simple_property(RotationMinX, bool, false);
  329. fbx_simple_property(RotationMaxX, bool, false);
  330. fbx_simple_property(RotationMinY, bool, false);
  331. fbx_simple_property(RotationMaxY, bool, false);
  332. fbx_simple_property(RotationMinZ, bool, false);
  333. fbx_simple_property(RotationMaxZ, bool, false);
  334. fbx_simple_enum_property(InheritType, TransformInheritance, 0);
  335. fbx_simple_property(ScalingActive, bool, false);
  336. fbx_simple_property(ScalingMin, Vector3, Vector3());
  337. fbx_simple_property(ScalingMax, Vector3, Vector3(1, 1, 1));
  338. fbx_simple_property(ScalingMinX, bool, false);
  339. fbx_simple_property(ScalingMaxX, bool, false);
  340. fbx_simple_property(ScalingMinY, bool, false);
  341. fbx_simple_property(ScalingMaxY, bool, false);
  342. fbx_simple_property(ScalingMinZ, bool, false);
  343. fbx_simple_property(ScalingMaxZ, bool, false);
  344. fbx_simple_property(GeometricTranslation, Vector3, Vector3());
  345. fbx_simple_property(GeometricRotation, Vector3, Vector3());
  346. fbx_simple_property(GeometricScaling, Vector3, Vector3(1, 1, 1));
  347. fbx_simple_property(MinDampRangeX, float, 0.0f);
  348. fbx_simple_property(MinDampRangeY, float, 0.0f);
  349. fbx_simple_property(MinDampRangeZ, float, 0.0f);
  350. fbx_simple_property(MaxDampRangeX, float, 0.0f);
  351. fbx_simple_property(MaxDampRangeY, float, 0.0f);
  352. fbx_simple_property(MaxDampRangeZ, float, 0.0f);
  353. fbx_simple_property(MinDampStrengthX, float, 0.0f);
  354. fbx_simple_property(MinDampStrengthY, float, 0.0f);
  355. fbx_simple_property(MinDampStrengthZ, float, 0.0f);
  356. fbx_simple_property(MaxDampStrengthX, float, 0.0f);
  357. fbx_simple_property(MaxDampStrengthY, float, 0.0f);
  358. fbx_simple_property(MaxDampStrengthZ, float, 0.0f);
  359. fbx_simple_property(PreferredAngleX, float, 0.0f);
  360. fbx_simple_property(PreferredAngleY, float, 0.0f);
  361. fbx_simple_property(PreferredAngleZ, float, 0.0f);
  362. fbx_simple_property(Show, bool, true);
  363. fbx_simple_property(LODBox, bool, false);
  364. fbx_simple_property(Freeze, bool, false);
  365. const std::string &Shading() const {
  366. return shading;
  367. }
  368. const std::string &Culling() const {
  369. return culling;
  370. }
  371. /** Get material links */
  372. const std::vector<const Material *> &GetMaterials() const {
  373. return materials;
  374. }
  375. /** Get geometry links */
  376. const std::vector<const Geometry *> &GetGeometry() const {
  377. return geometry;
  378. }
  379. /** Get node attachments */
  380. const std::vector<const NodeAttribute *> &GetAttributes() const {
  381. return attributes;
  382. }
  383. /** convenience method to check if the node has a Null node marker */
  384. bool IsNull() const;
  385. private:
  386. void ResolveLinks(const ElementPtr element, const Document &doc);
  387. private:
  388. std::vector<const Material *> materials;
  389. std::vector<const Geometry *> geometry;
  390. std::vector<const NodeAttribute *> attributes;
  391. std::string shading;
  392. std::string culling;
  393. };
  394. class ModelLimbNode : public Model {
  395. public:
  396. ModelLimbNode(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  397. virtual ~ModelLimbNode();
  398. };
  399. /** DOM class for generic FBX textures */
  400. class Texture : public Object {
  401. public:
  402. Texture(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  403. virtual ~Texture();
  404. const std::string &Type() const {
  405. return type;
  406. }
  407. const std::string &FileName() const {
  408. return fileName;
  409. }
  410. const std::string &RelativeFilename() const {
  411. return relativeFileName;
  412. }
  413. const std::string &AlphaSource() const {
  414. return alphaSource;
  415. }
  416. const Vector2 &UVTranslation() const {
  417. return uvTrans;
  418. }
  419. const Vector2 &UVScaling() const {
  420. return uvScaling;
  421. }
  422. // return a 4-tuple
  423. const unsigned int *Crop() const {
  424. return crop;
  425. }
  426. const Video *Media() const {
  427. return media;
  428. }
  429. private:
  430. Vector2 uvTrans;
  431. Vector2 uvScaling;
  432. std::string type;
  433. std::string relativeFileName;
  434. std::string fileName;
  435. std::string alphaSource;
  436. unsigned int crop[4] = { 0 };
  437. const Video *media = nullptr;
  438. };
  439. /** DOM class for layered FBX textures */
  440. class LayeredTexture : public Object {
  441. public:
  442. LayeredTexture(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  443. virtual ~LayeredTexture();
  444. // Can only be called after construction of the layered texture object due to construction flag.
  445. void fillTexture(const Document &doc);
  446. enum BlendMode {
  447. BlendMode_Translucent,
  448. BlendMode_Additive,
  449. BlendMode_Modulate,
  450. BlendMode_Modulate2,
  451. BlendMode_Over,
  452. BlendMode_Normal,
  453. BlendMode_Dissolve,
  454. BlendMode_Darken,
  455. BlendMode_ColorBurn,
  456. BlendMode_LinearBurn,
  457. BlendMode_DarkerColor,
  458. BlendMode_Lighten,
  459. BlendMode_Screen,
  460. BlendMode_ColorDodge,
  461. BlendMode_LinearDodge,
  462. BlendMode_LighterColor,
  463. BlendMode_SoftLight,
  464. BlendMode_HardLight,
  465. BlendMode_VividLight,
  466. BlendMode_LinearLight,
  467. BlendMode_PinLight,
  468. BlendMode_HardMix,
  469. BlendMode_Difference,
  470. BlendMode_Exclusion,
  471. BlendMode_Subtract,
  472. BlendMode_Divide,
  473. BlendMode_Hue,
  474. BlendMode_Saturation,
  475. BlendMode_Color,
  476. BlendMode_Luminosity,
  477. BlendMode_Overlay,
  478. BlendMode_BlendModeCount
  479. };
  480. const Texture *getTexture(int index = 0) const {
  481. return textures[index];
  482. }
  483. int textureCount() const {
  484. return static_cast<int>(textures.size());
  485. }
  486. BlendMode GetBlendMode() const {
  487. return blendMode;
  488. }
  489. float Alpha() {
  490. return alpha;
  491. }
  492. private:
  493. std::vector<const Texture *> textures;
  494. BlendMode blendMode = BlendMode::BlendMode_Additive;
  495. float alpha = 0;
  496. };
  497. typedef std::map<std::string, const Texture *> TextureMap;
  498. typedef std::map<std::string, const LayeredTexture *> LayeredTextureMap;
  499. /** DOM class for generic FBX videos */
  500. class Video : public Object {
  501. public:
  502. Video(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  503. virtual ~Video();
  504. const std::string &Type() const {
  505. return type;
  506. }
  507. bool IsEmbedded() const {
  508. return contentLength > 0;
  509. }
  510. const std::string &FileName() const {
  511. return fileName;
  512. }
  513. const std::string &RelativeFilename() const {
  514. return relativeFileName;
  515. }
  516. const uint8_t *Content() const {
  517. return content;
  518. }
  519. uint64_t ContentLength() const {
  520. return contentLength;
  521. }
  522. uint8_t *RelinquishContent() {
  523. uint8_t *ptr = content;
  524. content = nullptr;
  525. return ptr;
  526. }
  527. bool operator==(const Video &other) const {
  528. return (
  529. type == other.type && relativeFileName == other.relativeFileName && fileName == other.fileName);
  530. }
  531. bool operator<(const Video &other) const {
  532. return std::tie(type, relativeFileName, fileName) < std::tie(other.type, other.relativeFileName, other.fileName);
  533. }
  534. private:
  535. std::string type;
  536. std::string relativeFileName;
  537. std::string fileName;
  538. uint64_t contentLength = 0;
  539. uint8_t *content = nullptr;
  540. };
  541. /** DOM class for generic FBX materials */
  542. class Material : public Object {
  543. public:
  544. Material(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  545. virtual ~Material();
  546. const std::string &GetShadingModel() const {
  547. return shading;
  548. }
  549. bool IsMultilayer() const {
  550. return multilayer;
  551. }
  552. const TextureMap &Textures() const {
  553. return textures;
  554. }
  555. const LayeredTextureMap &LayeredTextures() const {
  556. return layeredTextures;
  557. }
  558. private:
  559. std::string shading;
  560. bool multilayer = false;
  561. TextureMap textures;
  562. LayeredTextureMap layeredTextures;
  563. };
  564. // signed int keys (this can happen!)
  565. typedef std::vector<int64_t> KeyTimeList;
  566. typedef std::vector<float> KeyValueList;
  567. /** Represents a FBX animation curve (i.e. a 1-dimensional set of keyframes and values therefore) */
  568. class AnimationCurve : public Object {
  569. public:
  570. AnimationCurve(uint64_t id, const ElementPtr element, const std::string &name, const Document &doc);
  571. virtual ~AnimationCurve();
  572. /** get list of keyframe positions (time).
  573. * Invariant: |GetKeys()| > 0 */
  574. const KeyTimeList &GetKeys() const {
  575. return keys;
  576. }
  577. /** get list of keyframe values.
  578. * Invariant: |GetKeys()| == |GetValues()| && |GetKeys()| > 0*/
  579. const KeyValueList &GetValues() const {
  580. return values;
  581. }
  582. const std::map<int64_t, float> &GetValueTimeTrack() const {
  583. return keyvalues;
  584. }
  585. const std::vector<float> &GetAttributes() const {
  586. return attributes;
  587. }
  588. const std::vector<unsigned int> &GetFlags() const {
  589. return flags;
  590. }
  591. private:
  592. KeyTimeList keys;
  593. KeyValueList values;
  594. std::vector<float> attributes;
  595. std::map<int64_t, float> keyvalues;
  596. std::vector<unsigned int> flags;
  597. };
  598. /* Typedef for pointers for the animation handler */
  599. typedef std::shared_ptr<AnimationCurve> AnimationCurvePtr;
  600. typedef std::weak_ptr<AnimationCurve> AnimationCurveWeakPtr;
  601. typedef std::map<std::string, const AnimationCurve *> AnimationMap;
  602. /* Animation Curve node ptr */
  603. typedef std::shared_ptr<AnimationCurveNode> AnimationCurveNodePtr;
  604. typedef std::weak_ptr<AnimationCurveNode> AnimationCurveNodeWeakPtr;
  605. /** Represents a FBX animation curve (i.e. a mapping from single animation curves to nodes) */
  606. class AnimationCurveNode : public Object {
  607. public:
  608. /* the optional white list specifies a list of property names for which the caller
  609. wants animations for. If the curve node does not match one of these, std::range_error
  610. will be thrown. */
  611. AnimationCurveNode(uint64_t id, const ElementPtr element, const std::string &name, const Document &doc,
  612. const char *const *target_prop_whitelist = nullptr, size_t whitelist_size = 0);
  613. virtual ~AnimationCurveNode();
  614. const AnimationMap &Curves() const;
  615. /** Object the curve is assigned to, this can be nullptr if the
  616. * target object has no DOM representation or could not
  617. * be read for other reasons.*/
  618. Object *Target() const {
  619. return target;
  620. }
  621. Model *TargetAsModel() const {
  622. return dynamic_cast<Model *>(target);
  623. }
  624. NodeAttribute *TargetAsNodeAttribute() const {
  625. return dynamic_cast<NodeAttribute *>(target);
  626. }
  627. /** Property of Target() that is being animated*/
  628. const std::string &TargetProperty() const {
  629. return prop;
  630. }
  631. private:
  632. Object *target = nullptr;
  633. mutable AnimationMap curves;
  634. std::string prop;
  635. const Document &doc;
  636. };
  637. typedef std::vector<const AnimationCurveNode *> AnimationCurveNodeList;
  638. typedef std::shared_ptr<AnimationLayer> AnimationLayerPtr;
  639. typedef std::weak_ptr<AnimationLayer> AnimationLayerWeakPtr;
  640. typedef std::vector<const AnimationLayer *> AnimationLayerList;
  641. /** Represents a FBX animation layer (i.e. a list of node animations) */
  642. class AnimationLayer : public Object {
  643. public:
  644. AnimationLayer(uint64_t id, const ElementPtr element, const std::string &name, const Document &doc);
  645. virtual ~AnimationLayer();
  646. /* the optional white list specifies a list of property names for which the caller
  647. wants animations for. Curves not matching this list will not be added to the
  648. animation layer. */
  649. const AnimationCurveNodeList Nodes(const char *const *target_prop_whitelist = nullptr, size_t whitelist_size = 0) const;
  650. private:
  651. const Document &doc;
  652. };
  653. /** Represents a FBX animation stack (i.e. a list of animation layers) */
  654. class AnimationStack : public Object {
  655. public:
  656. AnimationStack(uint64_t id, const ElementPtr element, const std::string &name, const Document &doc);
  657. virtual ~AnimationStack();
  658. fbx_simple_property(LocalStart, int64_t, 0L);
  659. fbx_simple_property(LocalStop, int64_t, 0L);
  660. fbx_simple_property(ReferenceStart, int64_t, 0L);
  661. fbx_simple_property(ReferenceStop, int64_t, 0L);
  662. const AnimationLayerList &Layers() const {
  663. return layers;
  664. }
  665. private:
  666. AnimationLayerList layers;
  667. };
  668. /** DOM class for deformers */
  669. class Deformer : public Object {
  670. public:
  671. Deformer(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  672. virtual ~Deformer();
  673. };
  674. /** Constraints are from Maya they can help us with BoneAttachments :) **/
  675. class Constraint : public Object {
  676. public:
  677. Constraint(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  678. virtual ~Constraint();
  679. };
  680. typedef std::vector<float> WeightArray;
  681. typedef std::vector<unsigned int> WeightIndexArray;
  682. /** DOM class for BlendShapeChannel deformers */
  683. class BlendShapeChannel : public Deformer {
  684. public:
  685. BlendShapeChannel(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  686. virtual ~BlendShapeChannel();
  687. float DeformPercent() const {
  688. return percent;
  689. }
  690. const WeightArray &GetFullWeights() const {
  691. return fullWeights;
  692. }
  693. const std::vector<const ShapeGeometry *> &GetShapeGeometries() const {
  694. return shapeGeometries;
  695. }
  696. private:
  697. float percent = 0;
  698. WeightArray fullWeights;
  699. std::vector<const ShapeGeometry *> shapeGeometries;
  700. };
  701. /** DOM class for BlendShape deformers */
  702. class BlendShape : public Deformer {
  703. public:
  704. BlendShape(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  705. virtual ~BlendShape();
  706. const std::vector<const BlendShapeChannel *> &BlendShapeChannels() const {
  707. return blendShapeChannels;
  708. }
  709. private:
  710. std::vector<const BlendShapeChannel *> blendShapeChannels;
  711. };
  712. /** DOM class for skin deformer clusters (aka sub-deformers) */
  713. class Cluster : public Deformer {
  714. public:
  715. Cluster(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  716. virtual ~Cluster();
  717. /** get the list of deformer weights associated with this cluster.
  718. * Use #GetIndices() to get the associated vertices. Both arrays
  719. * have the same size (and may also be empty). */
  720. const std::vector<float> &GetWeights() const {
  721. return weights;
  722. }
  723. /** get indices into the vertex data of the geometry associated
  724. * with this cluster. Use #GetWeights() to get the associated weights.
  725. * Both arrays have the same size (and may also be empty). */
  726. const std::vector<unsigned int> &GetIndices() const {
  727. return indices;
  728. }
  729. /** */
  730. const Transform3D &GetTransform() const {
  731. return transform;
  732. }
  733. const Transform3D &TransformLink() const {
  734. return transformLink;
  735. }
  736. const Model *TargetNode() const {
  737. return node;
  738. }
  739. const Transform3D &TransformAssociateModel() const {
  740. return transformAssociateModel;
  741. }
  742. bool TransformAssociateModelValid() const {
  743. return valid_transformAssociateModel;
  744. }
  745. // property is not in the fbx file
  746. // if the cluster has an associate model
  747. // we then have an additive type
  748. enum SkinLinkMode {
  749. SkinLinkMode_Normalized = 0,
  750. SkinLinkMode_Additive = 1
  751. };
  752. SkinLinkMode GetLinkMode() {
  753. return link_mode;
  754. }
  755. private:
  756. std::vector<float> weights;
  757. std::vector<unsigned int> indices;
  758. Transform3D transform;
  759. Transform3D transformLink;
  760. Transform3D transformAssociateModel;
  761. SkinLinkMode link_mode;
  762. bool valid_transformAssociateModel = false;
  763. const Model *node = nullptr;
  764. };
  765. /** DOM class for skin deformers */
  766. class Skin : public Deformer {
  767. public:
  768. Skin(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name);
  769. virtual ~Skin();
  770. float DeformAccuracy() const {
  771. return accuracy;
  772. }
  773. const std::vector<const Cluster *> &Clusters() const {
  774. return clusters;
  775. }
  776. enum SkinType {
  777. Skin_Rigid = 0,
  778. Skin_Linear,
  779. Skin_DualQuaternion,
  780. Skin_Blend
  781. };
  782. const SkinType &GetSkinType() const {
  783. return skinType;
  784. }
  785. private:
  786. float accuracy = 0;
  787. SkinType skinType = SkinType::Skin_Linear;
  788. std::vector<const Cluster *> clusters;
  789. };
  790. /** Represents a link between two FBX objects. */
  791. class Connection {
  792. public:
  793. Connection(uint64_t insertionOrder, uint64_t src, uint64_t dest, const std::string &prop, const Document &doc);
  794. ~Connection();
  795. // note: a connection ensures that the source and dest objects exist, but
  796. // not that they have DOM representations, so the return value of one of
  797. // these functions can still be nullptr.
  798. Object *SourceObject() const;
  799. Object *DestinationObject() const;
  800. // these, however, are always guaranteed to be valid
  801. LazyObject *LazySourceObject() const;
  802. LazyObject *LazyDestinationObject() const;
  803. /** return the name of the property the connection is attached to.
  804. * this is an empty string for object to object (OO) connections. */
  805. const std::string &PropertyName() const {
  806. return prop;
  807. }
  808. uint64_t InsertionOrder() const {
  809. return insertionOrder;
  810. }
  811. int CompareTo(const Connection *c) const {
  812. //ai_assert(nullptr != c);
  813. // note: can't subtract because this would overflow uint64_t
  814. if (InsertionOrder() > c->InsertionOrder()) {
  815. return 1;
  816. } else if (InsertionOrder() < c->InsertionOrder()) {
  817. return -1;
  818. }
  819. return 0;
  820. }
  821. bool Compare(const Connection *c) const {
  822. //ai_assert(nullptr != c);
  823. return InsertionOrder() < c->InsertionOrder();
  824. }
  825. public:
  826. uint64_t insertionOrder = 0;
  827. const std::string prop;
  828. uint64_t src = 0, dest = 0;
  829. const Document &doc;
  830. };
  831. // XXX again, unique_ptr would be useful. shared_ptr is too
  832. // bloated since the objects have a well-defined single owner
  833. // during their entire lifetime (Document). FBX files have
  834. // up to many thousands of objects (most of which we never use),
  835. // so the memory overhead for them should be kept at a minimum.
  836. typedef std::map<uint64_t, LazyObject *> ObjectMap;
  837. typedef std::map<std::string, const PropertyTable *> PropertyTemplateMap;
  838. typedef std::multimap<uint64_t, const Connection *> ConnectionMap;
  839. /** DOM class for global document settings, a single instance per document can
  840. * be accessed via Document.Globals(). */
  841. class FileGlobalSettings : public PropertyTable {
  842. public:
  843. FileGlobalSettings(const Document &doc);
  844. virtual ~FileGlobalSettings();
  845. const Document &GetDocument() const {
  846. return doc;
  847. }
  848. fbx_simple_property(UpAxis, int, 1);
  849. fbx_simple_property(UpAxisSign, int, 1);
  850. fbx_simple_property(FrontAxis, int, 2);
  851. fbx_simple_property(FrontAxisSign, int, 1);
  852. fbx_simple_property(CoordAxis, int, 0);
  853. fbx_simple_property(CoordAxisSign, int, 1);
  854. fbx_simple_property(OriginalUpAxis, int, 0);
  855. fbx_simple_property(OriginalUpAxisSign, int, 1);
  856. fbx_simple_property(UnitScaleFactor, float, 1);
  857. fbx_simple_property(OriginalUnitScaleFactor, float, 1);
  858. fbx_simple_property(AmbientColor, Vector3, Vector3(0, 0, 0));
  859. fbx_simple_property(DefaultCamera, std::string, "");
  860. enum FrameRate {
  861. FrameRate_DEFAULT = 0,
  862. FrameRate_120 = 1,
  863. FrameRate_100 = 2,
  864. FrameRate_60 = 3,
  865. FrameRate_50 = 4,
  866. FrameRate_48 = 5,
  867. FrameRate_30 = 6,
  868. FrameRate_30_DROP = 7,
  869. FrameRate_NTSC_DROP_FRAME = 8,
  870. FrameRate_NTSC_FULL_FRAME = 9,
  871. FrameRate_PAL = 10,
  872. FrameRate_CINEMA = 11,
  873. FrameRate_1000 = 12,
  874. FrameRate_CINEMA_ND = 13,
  875. FrameRate_CUSTOM = 14,
  876. FrameRate_MAX // end-of-enum sentinel
  877. };
  878. fbx_simple_enum_property(TimeMode, FrameRate, FrameRate_DEFAULT);
  879. fbx_simple_property(TimeSpanStart, uint64_t, 0L);
  880. fbx_simple_property(TimeSpanStop, uint64_t, 0L);
  881. fbx_simple_property(CustomFrameRate, float, -1.0f);
  882. private:
  883. const Document &doc;
  884. };
  885. /** DOM root for a FBX file */
  886. class Document {
  887. public:
  888. Document(const Parser &parser, const ImportSettings &settings);
  889. ~Document();
  890. LazyObject *GetObject(uint64_t id) const;
  891. bool IsSafeToImport() const {
  892. return SafeToImport;
  893. }
  894. bool IsBinary() const {
  895. return parser.IsBinary();
  896. }
  897. unsigned int FBXVersion() const {
  898. return fbxVersion;
  899. }
  900. const std::string &Creator() const {
  901. return creator;
  902. }
  903. // elements (in this order): Year, Month, Day, Hour, Second, Millisecond
  904. const unsigned int *CreationTimeStamp() const {
  905. return creationTimeStamp;
  906. }
  907. const FileGlobalSettings *GlobalSettingsPtr() const {
  908. return globals.get();
  909. }
  910. const PropertyTable &GetMetadataProperties() const {
  911. return metadata_properties;
  912. }
  913. const PropertyTemplateMap &Templates() const {
  914. return templates;
  915. }
  916. const ObjectMap &Objects() const {
  917. return objects;
  918. }
  919. const ImportSettings &Settings() const {
  920. return settings;
  921. }
  922. const ConnectionMap &ConnectionsBySource() const {
  923. return src_connections;
  924. }
  925. const ConnectionMap &ConnectionsByDestination() const {
  926. return dest_connections;
  927. }
  928. // note: the implicit rule in all DOM classes is to always resolve
  929. // from destination to source (since the FBX object hierarchy is,
  930. // with very few exceptions, a DAG, this avoids cycles). In all
  931. // cases that may involve back-facing edges in the object graph,
  932. // use LazyObject::IsBeingConstructed() to check.
  933. std::vector<const Connection *> GetConnectionsBySourceSequenced(uint64_t source) const;
  934. std::vector<const Connection *> GetConnectionsByDestinationSequenced(uint64_t dest) const;
  935. std::vector<const Connection *> GetConnectionsBySourceSequenced(uint64_t source, const char *classname) const;
  936. std::vector<const Connection *> GetConnectionsByDestinationSequenced(uint64_t dest, const char *classname) const;
  937. std::vector<const Connection *> GetConnectionsBySourceSequenced(uint64_t source,
  938. const char *const *classnames, size_t count) const;
  939. std::vector<const Connection *> GetConnectionsByDestinationSequenced(uint64_t dest,
  940. const char *const *classnames,
  941. size_t count) const;
  942. const std::vector<const AnimationStack *> &AnimationStacks() const;
  943. const std::vector<uint64_t> &GetAnimationStackIDs() const {
  944. return animationStacks;
  945. }
  946. const std::vector<uint64_t> &GetConstraintStackIDs() const {
  947. return constraints;
  948. }
  949. const std::vector<uint64_t> &GetBindPoseIDs() const {
  950. return bind_poses;
  951. };
  952. const std::vector<uint64_t> &GetMaterialIDs() const {
  953. return materials;
  954. };
  955. const std::vector<uint64_t> &GetSkinIDs() const {
  956. return skins;
  957. }
  958. private:
  959. std::vector<const Connection *> GetConnectionsSequenced(uint64_t id, const ConnectionMap &) const;
  960. std::vector<const Connection *> GetConnectionsSequenced(uint64_t id, bool is_src,
  961. const ConnectionMap &,
  962. const char *const *classnames,
  963. size_t count) const;
  964. bool ReadHeader();
  965. void ReadObjects();
  966. void ReadPropertyTemplates();
  967. void ReadConnections();
  968. void ReadGlobalSettings();
  969. private:
  970. const ImportSettings &settings;
  971. ObjectMap objects;
  972. const Parser &parser;
  973. bool SafeToImport = false;
  974. PropertyTemplateMap templates;
  975. ConnectionMap src_connections;
  976. ConnectionMap dest_connections;
  977. unsigned int fbxVersion = 0;
  978. std::string creator;
  979. unsigned int creationTimeStamp[7] = { 0 };
  980. std::vector<uint64_t> animationStacks;
  981. std::vector<uint64_t> bind_poses;
  982. // constraints aren't in the tree / at least they are not easy to access.
  983. std::vector<uint64_t> constraints;
  984. std::vector<uint64_t> materials;
  985. std::vector<uint64_t> skins;
  986. mutable std::vector<const AnimationStack *> animationStacksResolved;
  987. PropertyTable metadata_properties;
  988. std::shared_ptr<FileGlobalSettings> globals = nullptr;
  989. };
  990. } // namespace FBXDocParser
  991. namespace std {
  992. template <>
  993. struct hash<const FBXDocParser::Video> {
  994. std::size_t operator()(const FBXDocParser::Video &video) const {
  995. using std::hash;
  996. using std::size_t;
  997. using std::string;
  998. size_t res = 17;
  999. res = res * 31 + hash<string>()(video.Name());
  1000. res = res * 31 + hash<string>()(video.RelativeFilename());
  1001. res = res * 31 + hash<string>()(video.Type());
  1002. return res;
  1003. }
  1004. };
  1005. } // namespace std
  1006. #endif // FBX_DOCUMENT_H