ik_modifier_3d.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**************************************************************************/
  2. /* ik_modifier_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "ik_modifier_3d.h"
  31. void IKModifier3D::_notification(int p_what) {
  32. switch (p_what) {
  33. case NOTIFICATION_ENTER_TREE: {
  34. #ifdef TOOLS_ENABLED
  35. if (Engine::get_singleton()->is_editor_hint()) {
  36. set_notify_local_transform(true); // Used for updating gizmo in editor.
  37. }
  38. _update_mutable_info();
  39. _make_gizmo_dirty();
  40. #endif // TOOLS_ENABLED
  41. _make_all_joints_dirty();
  42. } break;
  43. #ifdef TOOLS_ENABLED
  44. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  45. _make_gizmo_dirty();
  46. } break;
  47. #endif // TOOLS_ENABLED
  48. }
  49. }
  50. void IKModifier3D::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("set_setting_count", "count"), &IKModifier3D::set_setting_count);
  52. ClassDB::bind_method(D_METHOD("get_setting_count"), &IKModifier3D::get_setting_count);
  53. ClassDB::bind_method(D_METHOD("clear_settings"), &IKModifier3D::clear_settings);
  54. ClassDB::bind_method(D_METHOD("set_mutable_bone_axes", "enabled"), &IKModifier3D::set_mutable_bone_axes);
  55. ClassDB::bind_method(D_METHOD("are_bone_axes_mutable"), &IKModifier3D::are_bone_axes_mutable);
  56. // To process manually.
  57. ClassDB::bind_method(D_METHOD("reset"), &IKModifier3D::reset);
  58. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mutable_bone_axes"), "set_mutable_bone_axes", "are_bone_axes_mutable");
  59. }
  60. void IKModifier3D::_set_active(bool p_active) {
  61. if (p_active) {
  62. reset();
  63. }
  64. }
  65. void IKModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
  66. if (p_old && p_old->is_connected(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated))) {
  67. p_old->disconnect(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated));
  68. }
  69. if (p_new && !p_new->is_connected(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated))) {
  70. p_new->connect(SNAME("rest_updated"), callable_mp(this, &IKModifier3D::_rest_updated));
  71. }
  72. _make_all_joints_dirty();
  73. }
  74. void IKModifier3D::_validate_bone_names() {
  75. //
  76. }
  77. void IKModifier3D::_rest_updated() {
  78. _make_all_joints_dirty();
  79. if (is_inside_tree()) {
  80. Skeleton3D *skeleton = get_skeleton();
  81. if (skeleton) {
  82. for (uint32_t i = 0; i < settings.size(); i++) {
  83. _init_joints(skeleton, i);
  84. }
  85. }
  86. }
  87. #ifdef TOOLS_ENABLED
  88. _update_mutable_info();
  89. _make_gizmo_dirty();
  90. #endif // TOOLS_ENABLED
  91. }
  92. void IKModifier3D::_make_all_joints_dirty() {
  93. //
  94. }
  95. void IKModifier3D::_init_joints(Skeleton3D *p_skeleton, int p_index) {
  96. //
  97. }
  98. void IKModifier3D::_update_joints(int p_index) {
  99. //
  100. }
  101. void IKModifier3D::_make_simulation_dirty(int p_index) {
  102. //
  103. }
  104. void IKModifier3D::_process_modification(double p_delta) {
  105. Skeleton3D *skeleton = get_skeleton();
  106. if (!skeleton) {
  107. return;
  108. }
  109. _process_ik(skeleton, p_delta);
  110. }
  111. void IKModifier3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) {
  112. //
  113. }
  114. void IKModifier3D::_update_bone_axis(Skeleton3D *p_skeleton, int p_index) {
  115. //
  116. }
  117. #ifdef TOOLS_ENABLED
  118. void IKModifier3D::_make_gizmo_dirty() {
  119. if (gizmo_dirty) {
  120. return;
  121. }
  122. gizmo_dirty = true;
  123. callable_mp(this, &IKModifier3D::_redraw_gizmo).call_deferred();
  124. }
  125. void IKModifier3D::_update_mutable_info() {
  126. //
  127. }
  128. void IKModifier3D::_redraw_gizmo() {
  129. update_gizmos();
  130. gizmo_dirty = false;
  131. }
  132. #endif // TOOLS_ENABLED
  133. void IKModifier3D::set_mutable_bone_axes(bool p_enabled) {
  134. mutable_bone_axes = p_enabled;
  135. for (uint32_t i = 0; i < settings.size(); i++) {
  136. _make_simulation_dirty(i);
  137. }
  138. #ifdef TOOLS_ENABLED
  139. _update_mutable_info();
  140. #endif // TOOLS_ENABLED
  141. }
  142. bool IKModifier3D::are_bone_axes_mutable() const {
  143. return mutable_bone_axes;
  144. }
  145. Quaternion IKModifier3D::get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation) {
  146. int parent = p_skeleton->get_bone_parent(p_bone);
  147. if (parent < 0) {
  148. return p_global_pose_rotation;
  149. }
  150. return p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion().inverse() * p_global_pose_rotation;
  151. }
  152. Vector3 IKModifier3D::get_bone_axis(Skeleton3D *p_skeleton, int p_end_bone, BoneDirection p_direction, bool p_mutable_bone_axes) {
  153. if (!p_skeleton || !p_skeleton->is_inside_tree()) {
  154. return Vector3();
  155. }
  156. Vector3 axis;
  157. if (p_direction == BONE_DIRECTION_FROM_PARENT) {
  158. if (p_skeleton) {
  159. axis = p_skeleton->get_bone_rest(p_end_bone).basis.xform_inv(p_mutable_bone_axes ? p_skeleton->get_bone_pose(p_end_bone).origin : p_skeleton->get_bone_rest(p_end_bone).origin);
  160. axis.normalize();
  161. }
  162. } else {
  163. axis = get_vector_from_bone_axis(static_cast<BoneAxis>((int)p_direction));
  164. }
  165. return axis;
  166. }
  167. int IKModifier3D::get_setting_count() const {
  168. return settings.size();
  169. }
  170. void IKModifier3D::reset() {
  171. Skeleton3D *skeleton = get_skeleton();
  172. if (!skeleton) {
  173. return;
  174. }
  175. for (uint32_t i = 0; i < settings.size(); i++) {
  176. _make_simulation_dirty(i);
  177. _init_joints(skeleton, i);
  178. }
  179. }
  180. IKModifier3D::~IKModifier3D() {
  181. clear_settings();
  182. }