fbx_skeleton.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*************************************************************************/
  2. /* fbx_skeleton.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "fbx_skeleton.h"
  31. #include "import_state.h"
  32. #include "tools/import_utils.h"
  33. void FBXSkeleton::init_skeleton(const ImportState &state) {
  34. int skeleton_bone_count = skeleton_bones.size();
  35. if (skeleton == nullptr && skeleton_bone_count > 0) {
  36. skeleton = memnew(Skeleton3D);
  37. if (fbx_node.is_valid()) {
  38. // cache skeleton attachment for later during node creation
  39. // can't be done until after node hierarchy is built
  40. if (fbx_node->godot_node != state.root) {
  41. fbx_node->skeleton_node = Ref<FBXSkeleton>(this);
  42. print_verbose("cached armature skeleton attachment for node " + fbx_node->node_name);
  43. } else {
  44. // root node must never be a skeleton to prevent cyclic skeletons from being allowed (skeleton in a skeleton)
  45. fbx_node->godot_node->add_child(skeleton);
  46. skeleton->set_owner(state.root_owner);
  47. skeleton->set_name("Skeleton3D");
  48. print_verbose("created armature skeleton for root");
  49. }
  50. } else {
  51. memfree(skeleton);
  52. skeleton = nullptr;
  53. print_error("[doc] skeleton has no valid node to parent nodes to - erasing");
  54. skeleton_bones.clear();
  55. return;
  56. }
  57. }
  58. // Make the bone name uniques.
  59. for (int x = 0; x < skeleton_bone_count; x++) {
  60. Ref<FBXBone> bone = skeleton_bones[x];
  61. if (bone.is_valid()) {
  62. // Make sure the bone name is unique.
  63. const String bone_name = bone->bone_name;
  64. int same_name_count = 0;
  65. for (int y = x; y < skeleton_bone_count; y++) {
  66. Ref<FBXBone> other_bone = skeleton_bones[y];
  67. if (other_bone.is_valid()) {
  68. if (other_bone->bone_name == bone_name) {
  69. same_name_count += 1;
  70. other_bone->bone_name += "_" + itos(same_name_count);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. Map<int, Ref<FBXBone>> bone_map;
  77. // implement fbx cluster skin logic here this is where it goes
  78. int bone_count = 0;
  79. for (int x = 0; x < skeleton_bone_count; x++) {
  80. Ref<FBXBone> bone = skeleton_bones[x];
  81. if (bone.is_valid()) {
  82. skeleton->add_bone(bone->bone_name);
  83. bone->godot_bone_id = bone_count;
  84. bone->fbx_skeleton = Ref<FBXSkeleton>(this);
  85. bone_map.insert(bone_count, bone);
  86. print_verbose("added bone " + itos(bone->bone_id) + " " + bone->bone_name);
  87. bone_count++;
  88. }
  89. }
  90. ERR_FAIL_COND_MSG(skeleton->get_bone_count() != bone_count, "Not all bones got added, is the file corrupted?");
  91. for (Map<int, Ref<FBXBone>>::Element *bone_element = bone_map.front(); bone_element; bone_element = bone_element->next()) {
  92. const Ref<FBXBone> bone = bone_element->value();
  93. int bone_index = bone_element->key();
  94. print_verbose("working on bone: " + itos(bone_index) + " bone name:" + bone->bone_name);
  95. skeleton->set_bone_rest(bone->godot_bone_id, get_unscaled_transform(bone->node->pivot_transform->LocalTransform, state.scale));
  96. // lookup parent ID
  97. if (bone->valid_parent && state.fbx_bone_map.has(bone->parent_bone_id)) {
  98. Ref<FBXBone> parent_bone = state.fbx_bone_map[bone->parent_bone_id];
  99. int bone_id = skeleton->find_bone(parent_bone->bone_name);
  100. if (bone_id != -1) {
  101. skeleton->set_bone_parent(bone_index, bone_id);
  102. } else {
  103. print_error("invalid bone parent: " + parent_bone->bone_name);
  104. }
  105. } else {
  106. if (bone->godot_bone_id != -1) {
  107. skeleton->set_bone_parent(bone_index, -1); // no parent for this bone
  108. }
  109. }
  110. }
  111. }