editor_scene_importer_fbx.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* editor_scene_importer_fbx.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. #ifndef EDITOR_SCENE_IMPORTER_FBX_H
  31. #define EDITOR_SCENE_IMPORTER_FBX_H
  32. #ifdef TOOLS_ENABLED
  33. #include "data/import_state.h"
  34. #include "tools/import_utils.h"
  35. #include "core/bind/core_bind.h"
  36. #include "core/dictionary.h"
  37. #include "core/io/resource_importer.h"
  38. #include "core/local_vector.h"
  39. #include "core/ustring.h"
  40. #include "core/vector.h"
  41. #include "editor/import/resource_importer_scene.h"
  42. #include "editor/project_settings_editor.h"
  43. #include "scene/3d/mesh_instance.h"
  44. #include "scene/3d/skeleton.h"
  45. #include "scene/3d/spatial.h"
  46. #include "scene/animation/animation_player.h"
  47. #include "scene/resources/animation.h"
  48. #include "scene/resources/surface_tool.h"
  49. #include "fbx_parser/FBXDocument.h"
  50. #include "fbx_parser/FBXImportSettings.h"
  51. #include "fbx_parser/FBXMeshGeometry.h"
  52. #include "fbx_parser/FBXUtil.h"
  53. #define CONVERT_FBX_TIME(time) static_cast<double>(time) / 46186158000LL
  54. class EditorSceneImporterFBX : public EditorSceneImporter {
  55. private:
  56. GDCLASS(EditorSceneImporterFBX, EditorSceneImporter);
  57. struct AssetImportAnimation {
  58. enum Interpolation {
  59. INTERP_LINEAR,
  60. INTERP_STEP,
  61. INTERP_CATMULLROMSPLINE,
  62. INTERP_CUBIC_SPLINE
  63. };
  64. };
  65. // ------------------------------------------------------------------------------------------------
  66. template <typename T>
  67. const T *ProcessDOMConnection(
  68. const FBXDocParser::Document *doc,
  69. uint64_t current_element,
  70. bool reverse_lookup = false) {
  71. const std::vector<const FBXDocParser::Connection *> &conns = reverse_lookup ? doc->GetConnectionsByDestinationSequenced(current_element) : doc->GetConnectionsBySourceSequenced(current_element);
  72. //print_verbose("[doc] looking for " + String(element_to_find));
  73. // using the temp pattern here so we can debug before it returns
  74. // in some cases we return too early, with 'deformer object base class' in wrong place
  75. // in assimp this means we can accidentally return too early...
  76. const T *return_obj = nullptr;
  77. for (const FBXDocParser::Connection *con : conns) {
  78. const FBXDocParser::Object *source_object = con->SourceObject();
  79. const FBXDocParser::Object *dest_object = con->DestinationObject();
  80. if (source_object && dest_object != nullptr) {
  81. //print_verbose("[doc] connection name: " + String(source_object->Name().c_str()) + ", dest: " + String(dest_object->Name().c_str()));
  82. const T *temp = dynamic_cast<const T *>(reverse_lookup ? source_object : dest_object);
  83. if (temp) {
  84. return_obj = temp;
  85. }
  86. }
  87. }
  88. if (return_obj != nullptr) {
  89. //print_verbose("[doc] returned valid element");
  90. //print_verbose("Found object for bone");
  91. return return_obj;
  92. }
  93. // safe to return nothing, need to use nullptr here as nullptr is used internally for FBX document.
  94. return nullptr;
  95. }
  96. void BuildDocumentBones(Ref<FBXBone> p_parent_bone,
  97. ImportState &state, const FBXDocParser::Document *p_doc,
  98. uint64_t p_id);
  99. void BuildDocumentNodes(Ref<PivotTransform> parent_transform, ImportState &state, const FBXDocParser::Document *doc, uint64_t id, Ref<FBXNode> fbx_parent);
  100. Spatial *_generate_scene(const String &p_path, const FBXDocParser::Document *p_document,
  101. const uint32_t p_flags,
  102. int p_bake_fps,
  103. const uint32_t p_compress_flags,
  104. const int32_t p_max_bone_weights,
  105. bool p_is_blender_fbx);
  106. template <class T>
  107. T _interpolate_track(const Vector<float> &p_times, const Vector<T> &p_values, float p_time, AssetImportAnimation::Interpolation p_interp);
  108. void _register_project_setting_import(const String generic, const String import_setting_string, const Vector<String> &exts, List<String> *r_extensions, const bool p_enabled) const;
  109. public:
  110. EditorSceneImporterFBX() {}
  111. ~EditorSceneImporterFBX() {}
  112. virtual void get_extensions(List<String> *r_extensions) const;
  113. virtual uint32_t get_import_flags() const;
  114. virtual Node *import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, uint32_t p_compress_flags, List<String> *r_missing_deps, Error *r_err = nullptr);
  115. void create_mesh_data_skin(ImportState &state, const Ref<FBXNode> &fbx_node, uint64_t mesh_id);
  116. };
  117. #endif // TOOLS_ENABLED
  118. #endif // EDITOR_SCENE_IMPORTER_FBX_H