2
0

skeleton_modifier_3d.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**************************************************************************/
  2. /* skeleton_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 "skeleton_modifier_3d.h"
  31. PackedStringArray SkeletonModifier3D::get_configuration_warnings() const {
  32. PackedStringArray warnings = Node3D::get_configuration_warnings();
  33. if (skeleton_id.is_null()) {
  34. warnings.push_back(RTR("Skeleton3D node not set! SkeletonModifier3D must be child of Skeleton3D."));
  35. }
  36. return warnings;
  37. }
  38. /* Skeleton3D */
  39. Skeleton3D *SkeletonModifier3D::get_skeleton() const {
  40. return ObjectDB::get_instance<Skeleton3D>(skeleton_id);
  41. }
  42. void SkeletonModifier3D::_update_skeleton_path() {
  43. skeleton_id = ObjectID();
  44. // Make sure parent is a Skeleton3D.
  45. Skeleton3D *sk = Object::cast_to<Skeleton3D>(get_parent());
  46. if (sk) {
  47. skeleton_id = sk->get_instance_id();
  48. }
  49. }
  50. void SkeletonModifier3D::_update_skeleton() {
  51. if (!is_inside_tree()) {
  52. return;
  53. }
  54. Skeleton3D *old_sk = get_skeleton();
  55. _update_skeleton_path();
  56. Skeleton3D *new_sk = get_skeleton();
  57. if (old_sk != new_sk) {
  58. _skeleton_changed(old_sk, new_sk);
  59. }
  60. if (new_sk) {
  61. _validate_bone_names();
  62. }
  63. update_configuration_warnings();
  64. }
  65. void SkeletonModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
  66. GDVIRTUAL_CALL(_skeleton_changed, p_old, p_new);
  67. }
  68. void SkeletonModifier3D::_validate_bone_names() {
  69. GDVIRTUAL_CALL(_validate_bone_names);
  70. }
  71. void SkeletonModifier3D::_force_update_skeleton_skin() {
  72. if (!is_inside_tree()) {
  73. return;
  74. }
  75. Skeleton3D *skeleton = get_skeleton();
  76. if (!skeleton) {
  77. return;
  78. }
  79. skeleton->force_update_deferred();
  80. }
  81. /* Process */
  82. void SkeletonModifier3D::set_active(bool p_active) {
  83. if (active == p_active) {
  84. return;
  85. }
  86. active = p_active;
  87. _set_active(active);
  88. _force_update_skeleton_skin();
  89. }
  90. bool SkeletonModifier3D::is_active() const {
  91. return active;
  92. }
  93. void SkeletonModifier3D::_set_active(bool p_active) {
  94. //
  95. }
  96. void SkeletonModifier3D::set_influence(real_t p_influence) {
  97. influence = p_influence;
  98. }
  99. real_t SkeletonModifier3D::get_influence() const {
  100. return influence;
  101. }
  102. void SkeletonModifier3D::process_modification(double p_delta) {
  103. if (!active) {
  104. return;
  105. }
  106. _process_modification(p_delta);
  107. emit_signal(SNAME("modification_processed"));
  108. }
  109. void SkeletonModifier3D::_process_modification(double p_delta) {
  110. if (GDVIRTUAL_CALL(_process_modification_with_delta, p_delta)) {
  111. return;
  112. }
  113. #ifndef DISABLE_DEPRECATED
  114. if (GDVIRTUAL_CALL(_process_modification)) {
  115. return;
  116. }
  117. #endif // DISABLE_DEPRECATED
  118. }
  119. void SkeletonModifier3D::_notification(int p_what) {
  120. switch (p_what) {
  121. case NOTIFICATION_ENTER_TREE:
  122. case NOTIFICATION_PARENTED: {
  123. _update_skeleton();
  124. } break;
  125. case NOTIFICATION_EXIT_TREE:
  126. case NOTIFICATION_UNPARENTED: {
  127. _force_update_skeleton_skin();
  128. } break;
  129. }
  130. }
  131. void SkeletonModifier3D::_bind_methods() {
  132. ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModifier3D::get_skeleton);
  133. ClassDB::bind_method(D_METHOD("set_active", "active"), &SkeletonModifier3D::set_active);
  134. ClassDB::bind_method(D_METHOD("is_active"), &SkeletonModifier3D::is_active);
  135. ClassDB::bind_method(D_METHOD("set_influence", "influence"), &SkeletonModifier3D::set_influence);
  136. ClassDB::bind_method(D_METHOD("get_influence"), &SkeletonModifier3D::get_influence);
  137. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  138. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "influence", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_influence", "get_influence");
  139. ADD_SIGNAL(MethodInfo("modification_processed"));
  140. GDVIRTUAL_BIND(_process_modification_with_delta, "delta");
  141. #ifndef DISABLE_DEPRECATED
  142. GDVIRTUAL_BIND(_process_modification);
  143. #endif
  144. GDVIRTUAL_BIND(_skeleton_changed, "old_skeleton", "new_skeleton");
  145. GDVIRTUAL_BIND(_validate_bone_names);
  146. BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_X);
  147. BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_X);
  148. BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_Y);
  149. BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_Y);
  150. BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_Z);
  151. BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_Z);
  152. }
  153. Vector3 SkeletonModifier3D::get_vector_from_bone_axis(BoneAxis p_axis) {
  154. Vector3 ret;
  155. switch (p_axis) {
  156. case BONE_AXIS_PLUS_X: {
  157. ret = Vector3(1, 0, 0);
  158. } break;
  159. case BONE_AXIS_MINUS_X: {
  160. ret = Vector3(-1, 0, 0);
  161. } break;
  162. case BONE_AXIS_PLUS_Y: {
  163. ret = Vector3(0, 1, 0);
  164. } break;
  165. case BONE_AXIS_MINUS_Y: {
  166. ret = Vector3(0, -1, 0);
  167. } break;
  168. case BONE_AXIS_PLUS_Z: {
  169. ret = Vector3(0, 0, 1);
  170. } break;
  171. case BONE_AXIS_MINUS_Z: {
  172. ret = Vector3(0, 0, -1);
  173. } break;
  174. }
  175. return ret;
  176. }
  177. Vector3 SkeletonModifier3D::get_vector_from_axis(Vector3::Axis p_axis) {
  178. Vector3 ret;
  179. switch (p_axis) {
  180. case Vector3::AXIS_X: {
  181. ret = Vector3(1, 0, 0);
  182. } break;
  183. case Vector3::AXIS_Y: {
  184. ret = Vector3(0, 1, 0);
  185. } break;
  186. case Vector3::AXIS_Z: {
  187. ret = Vector3(0, 0, 1);
  188. } break;
  189. }
  190. return ret;
  191. }
  192. Vector3::Axis SkeletonModifier3D::get_axis_from_bone_axis(BoneAxis p_axis) {
  193. Vector3::Axis ret = Vector3::AXIS_X;
  194. switch (p_axis) {
  195. case BONE_AXIS_PLUS_X:
  196. case BONE_AXIS_MINUS_X: {
  197. ret = Vector3::AXIS_X;
  198. } break;
  199. case BONE_AXIS_PLUS_Y:
  200. case BONE_AXIS_MINUS_Y: {
  201. ret = Vector3::AXIS_Y;
  202. } break;
  203. case BONE_AXIS_PLUS_Z:
  204. case BONE_AXIS_MINUS_Z: {
  205. ret = Vector3::AXIS_Z;
  206. } break;
  207. }
  208. return ret;
  209. }
  210. Vector3 SkeletonModifier3D::limit_length(const Vector3 &p_origin, const Vector3 &p_destination, float p_length) {
  211. return p_origin + (p_destination - p_origin).normalized() * p_length;
  212. }
  213. Quaternion SkeletonModifier3D::get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation) {
  214. int parent = p_skeleton->get_bone_parent(p_bone);
  215. if (parent < 0) {
  216. return p_global_pose_rotation;
  217. }
  218. return p_skeleton->get_bone_global_pose(parent).basis.orthonormalized().inverse() * p_global_pose_rotation;
  219. }
  220. Quaternion SkeletonModifier3D::get_from_to_rotation(const Vector3 &p_from, const Vector3 &p_to, const Quaternion &p_prev_rot) {
  221. if (Math::is_equal_approx((float)p_from.dot(p_to), -1.0f)) {
  222. return p_prev_rot; // For preventing to glitch, checking dot for detecting flip is more accurate than checking cross.
  223. }
  224. Vector3 axis = p_from.cross(p_to);
  225. if (axis.is_zero_approx()) {
  226. return p_prev_rot;
  227. }
  228. float angle = p_from.angle_to(p_to);
  229. if (Math::is_zero_approx(angle)) {
  230. angle = 0.0;
  231. }
  232. return Quaternion(axis.normalized(), angle);
  233. }
  234. Vector3 SkeletonModifier3D::snap_vector_to_plane(const Vector3 &p_plane_normal, const Vector3 &p_vector) {
  235. if (Math::is_zero_approx(p_plane_normal.length_squared())) {
  236. return p_vector;
  237. }
  238. double length = p_vector.length();
  239. Vector3 normalized_vec = p_vector.normalized();
  240. Vector3 normal = p_plane_normal.normalized();
  241. return normalized_vec.slide(normal) * length;
  242. }
  243. SkeletonModifier3D::SkeletonModifier3D() {
  244. }