skeleton_editor_plugin.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*************************************************************************/
  2. /* skeleton_editor_plugin.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 "skeleton_editor_plugin.h"
  31. #include "scene/3d/collision_shape.h"
  32. #include "scene/3d/physics_body.h"
  33. #include "scene/3d/physics_joint.h"
  34. #include "scene/resources/capsule_shape.h"
  35. #include "scene/resources/sphere_shape.h"
  36. #include "spatial_editor_plugin.h"
  37. void SkeletonEditor::_on_click_option(int p_option) {
  38. if (!skeleton) {
  39. return;
  40. }
  41. switch (p_option) {
  42. case MENU_OPTION_CREATE_PHYSICAL_SKELETON: {
  43. create_physical_skeleton();
  44. } break;
  45. }
  46. }
  47. void SkeletonEditor::create_physical_skeleton() {
  48. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  49. Node *owner = skeleton == get_tree()->get_edited_scene_root() ? skeleton : skeleton->get_owner();
  50. const int bc = skeleton->get_bone_count();
  51. if (!bc) {
  52. return;
  53. }
  54. Vector<BoneInfo> bones_infos;
  55. bones_infos.resize(bc);
  56. for (int bone_id = 0; bc > bone_id; ++bone_id) {
  57. const int parent = skeleton->get_bone_parent(bone_id);
  58. if (parent < 0) {
  59. bones_infos.write[bone_id].relative_rest = skeleton->get_bone_rest(bone_id);
  60. } else {
  61. const int parent_parent = skeleton->get_bone_parent(parent);
  62. bones_infos.write[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id);
  63. /// create physical bone on parent
  64. if (!bones_infos[parent].physical_bone) {
  65. bones_infos.write[parent].physical_bone = create_physical_bone(parent, bone_id, bones_infos);
  66. ur->create_action(TTR("Create physical bones"));
  67. ur->add_do_method(skeleton, "add_child", bones_infos[parent].physical_bone);
  68. ur->add_do_reference(bones_infos[parent].physical_bone);
  69. ur->add_undo_method(skeleton, "remove_child", bones_infos[parent].physical_bone);
  70. ur->commit_action();
  71. bones_infos[parent].physical_bone->set_bone_name(skeleton->get_bone_name(parent));
  72. bones_infos[parent].physical_bone->set_owner(owner);
  73. bones_infos[parent].physical_bone->get_child(0)->set_owner(owner); // set shape owner
  74. /// Create joint between parent of parent
  75. if (-1 != parent_parent) {
  76. bones_infos[parent].physical_bone->set_joint_type(PhysicalBone::JOINT_TYPE_PIN);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. PhysicalBone *SkeletonEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos) {
  83. real_t half_height(skeleton->get_bone_rest(bone_child_id).origin.length() * 0.5);
  84. real_t radius(half_height * 0.2);
  85. CapsuleShape *bone_shape_capsule = memnew(CapsuleShape);
  86. bone_shape_capsule->set_height((half_height - radius) * 2);
  87. bone_shape_capsule->set_radius(radius);
  88. CollisionShape *bone_shape = memnew(CollisionShape);
  89. bone_shape->set_shape(bone_shape_capsule);
  90. Transform capsule_transform;
  91. capsule_transform.basis = Basis(Vector3(1, 0, 0), Vector3(0, 0, 1), Vector3(0, -1, 0));
  92. bone_shape->set_transform(capsule_transform);
  93. Transform body_transform;
  94. body_transform.origin = Vector3(0, 0, -half_height);
  95. Transform joint_transform;
  96. joint_transform.origin = Vector3(0, 0, half_height);
  97. PhysicalBone *physical_bone = memnew(PhysicalBone);
  98. physical_bone->add_child(bone_shape);
  99. physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id));
  100. physical_bone->set_body_offset(body_transform);
  101. physical_bone->set_joint_offset(joint_transform);
  102. return physical_bone;
  103. }
  104. void SkeletonEditor::edit(Skeleton *p_node) {
  105. skeleton = p_node;
  106. }
  107. void SkeletonEditor::_notification(int p_what) {
  108. if (p_what == NOTIFICATION_ENTER_TREE) {
  109. get_tree()->connect("node_removed", this, "_node_removed");
  110. }
  111. }
  112. void SkeletonEditor::_node_removed(Node *p_node) {
  113. if (p_node == skeleton) {
  114. skeleton = NULL;
  115. options->hide();
  116. }
  117. }
  118. void SkeletonEditor::_bind_methods() {
  119. ClassDB::bind_method("_on_click_option", &SkeletonEditor::_on_click_option);
  120. ClassDB::bind_method("_node_removed", &SkeletonEditor::_node_removed);
  121. }
  122. SkeletonEditor::SkeletonEditor() {
  123. skeleton = NULL;
  124. options = memnew(MenuButton);
  125. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  126. options->set_text(TTR("Skeleton"));
  127. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Skeleton", "EditorIcons"));
  128. options->get_popup()->add_item(TTR("Create physical skeleton"), MENU_OPTION_CREATE_PHYSICAL_SKELETON);
  129. options->get_popup()->connect("id_pressed", this, "_on_click_option");
  130. options->hide();
  131. }
  132. SkeletonEditor::~SkeletonEditor() {}
  133. void SkeletonEditorPlugin::edit(Object *p_object) {
  134. skeleton_editor->edit(Object::cast_to<Skeleton>(p_object));
  135. }
  136. bool SkeletonEditorPlugin::handles(Object *p_object) const {
  137. return p_object->is_class("Skeleton");
  138. }
  139. void SkeletonEditorPlugin::make_visible(bool p_visible) {
  140. if (p_visible) {
  141. skeleton_editor->options->show();
  142. } else {
  143. skeleton_editor->options->hide();
  144. skeleton_editor->edit(NULL);
  145. }
  146. }
  147. SkeletonEditorPlugin::SkeletonEditorPlugin(EditorNode *p_node) {
  148. editor = p_node;
  149. skeleton_editor = memnew(SkeletonEditor);
  150. editor->get_viewport()->add_child(skeleton_editor);
  151. }
  152. SkeletonEditorPlugin::~SkeletonEditorPlugin() {}