import_state.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* import_state.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_IMPORT_STATE_H
  31. #define EDITOR_SCENE_IMPORT_STATE_H
  32. #include "core/bind/core_bind.h"
  33. #include "core/io/resource_importer.h"
  34. #include "core/vector.h"
  35. #include "editor/import/resource_importer_scene.h"
  36. #include "editor/project_settings_editor.h"
  37. #include "scene/3d/mesh_instance.h"
  38. #include "scene/3d/skeleton.h"
  39. #include "scene/3d/spatial.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/resources/animation.h"
  42. #include "scene/resources/surface_tool.h"
  43. #include <assimp/matrix4x4.h>
  44. #include <assimp/scene.h>
  45. #include <assimp/types.h>
  46. #include <assimp/DefaultLogger.hpp>
  47. #include <assimp/LogStream.hpp>
  48. #include <assimp/Logger.hpp>
  49. namespace AssimpImporter {
  50. /** Import state is for global scene import data
  51. * This makes the code simpler and contains useful lookups.
  52. */
  53. struct ImportState {
  54. String path;
  55. Spatial *root;
  56. const aiScene *assimp_scene;
  57. uint32_t max_bone_weights;
  58. Map<String, Ref<Mesh> > mesh_cache;
  59. Map<int, Ref<Material> > material_cache;
  60. Map<String, int> light_cache;
  61. Map<String, int> camera_cache;
  62. // very useful for when you need to ask assimp for the bone mesh
  63. Map<const aiNode *, Node *> assimp_node_map;
  64. Map<String, Ref<Image> > path_to_image_cache;
  65. // Generation 3 - determinisitic iteration
  66. // to lower potential recursion errors
  67. List<const aiNode *> nodes;
  68. Map<const aiNode *, Spatial *> flat_node_map;
  69. AnimationPlayer *animation_player;
  70. // Generation 3 - deterministic armatures
  71. // list of armature nodes - flat and simple to parse
  72. // assimp node, node in godot
  73. List<aiNode *> armature_nodes;
  74. Map<const aiNode *, Skeleton *> armature_skeletons;
  75. Map<aiBone *, Skeleton *> skeleton_bone_map;
  76. // Generation 3 - deterministic bone handling
  77. // bones from the stack are popped when found
  78. // this means we can detect
  79. // what bones are for other armatures
  80. List<aiBone *> bone_stack;
  81. };
  82. struct AssimpImageData {
  83. Ref<Image> raw_image;
  84. Ref<ImageTexture> texture;
  85. aiTextureMapMode map_mode[2];
  86. };
  87. /** Recursive state is used to push state into functions instead of specifying them
  88. * This makes the code easier to handle too and add extra arguments without breaking things
  89. */
  90. struct RecursiveState {
  91. RecursiveState() {} // do not construct :)
  92. RecursiveState(
  93. Transform &_node_transform,
  94. Skeleton *_skeleton,
  95. Spatial *_new_node,
  96. String &_node_name,
  97. aiNode *_assimp_node,
  98. Node *_parent_node,
  99. aiBone *_bone) :
  100. node_transform(_node_transform),
  101. skeleton(_skeleton),
  102. new_node(_new_node),
  103. node_name(_node_name),
  104. assimp_node(_assimp_node),
  105. parent_node(_parent_node),
  106. bone(_bone) {}
  107. Transform node_transform;
  108. Skeleton *skeleton = NULL;
  109. Spatial *new_node = NULL;
  110. String node_name;
  111. aiNode *assimp_node = NULL;
  112. Node *parent_node = NULL;
  113. aiBone *bone = NULL;
  114. };
  115. } // namespace AssimpImporter
  116. #endif // EDITOR_SCENE_IMPORT_STATE_H