skeleton_modifier_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 (!is_inside_tree() || !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. BIND_ENUM_CONSTANT(BONE_DIRECTION_PLUS_X);
  153. BIND_ENUM_CONSTANT(BONE_DIRECTION_MINUS_X);
  154. BIND_ENUM_CONSTANT(BONE_DIRECTION_PLUS_Y);
  155. BIND_ENUM_CONSTANT(BONE_DIRECTION_MINUS_Y);
  156. BIND_ENUM_CONSTANT(BONE_DIRECTION_PLUS_Z);
  157. BIND_ENUM_CONSTANT(BONE_DIRECTION_MINUS_Z);
  158. BIND_ENUM_CONSTANT(BONE_DIRECTION_FROM_PARENT);
  159. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_NONE);
  160. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_PLUS_X);
  161. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_MINUS_X);
  162. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_PLUS_Y);
  163. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_MINUS_Y);
  164. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_PLUS_Z);
  165. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_MINUS_Z);
  166. BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_CUSTOM);
  167. BIND_ENUM_CONSTANT(ROTATION_AXIS_X);
  168. BIND_ENUM_CONSTANT(ROTATION_AXIS_Y);
  169. BIND_ENUM_CONSTANT(ROTATION_AXIS_Z);
  170. BIND_ENUM_CONSTANT(ROTATION_AXIS_ALL);
  171. BIND_ENUM_CONSTANT(ROTATION_AXIS_CUSTOM);
  172. }
  173. Vector3 SkeletonModifier3D::get_vector_from_bone_axis(BoneAxis p_axis) {
  174. Vector3 ret;
  175. switch (p_axis) {
  176. case BONE_AXIS_PLUS_X: {
  177. ret = Vector3(1, 0, 0);
  178. } break;
  179. case BONE_AXIS_MINUS_X: {
  180. ret = Vector3(-1, 0, 0);
  181. } break;
  182. case BONE_AXIS_PLUS_Y: {
  183. ret = Vector3(0, 1, 0);
  184. } break;
  185. case BONE_AXIS_MINUS_Y: {
  186. ret = Vector3(0, -1, 0);
  187. } break;
  188. case BONE_AXIS_PLUS_Z: {
  189. ret = Vector3(0, 0, 1);
  190. } break;
  191. case BONE_AXIS_MINUS_Z: {
  192. ret = Vector3(0, 0, -1);
  193. } break;
  194. }
  195. return ret;
  196. }
  197. Vector3 SkeletonModifier3D::get_vector_from_axis(Vector3::Axis p_axis) {
  198. Vector3 ret;
  199. switch (p_axis) {
  200. case Vector3::AXIS_X: {
  201. ret = Vector3(1, 0, 0);
  202. } break;
  203. case Vector3::AXIS_Y: {
  204. ret = Vector3(0, 1, 0);
  205. } break;
  206. case Vector3::AXIS_Z: {
  207. ret = Vector3(0, 0, 1);
  208. } break;
  209. }
  210. return ret;
  211. }
  212. Vector3::Axis SkeletonModifier3D::get_axis_from_bone_axis(BoneAxis p_axis) {
  213. Vector3::Axis ret = Vector3::AXIS_X;
  214. switch (p_axis) {
  215. case BONE_AXIS_PLUS_X:
  216. case BONE_AXIS_MINUS_X: {
  217. ret = Vector3::AXIS_X;
  218. } break;
  219. case BONE_AXIS_PLUS_Y:
  220. case BONE_AXIS_MINUS_Y: {
  221. ret = Vector3::AXIS_Y;
  222. } break;
  223. case BONE_AXIS_PLUS_Z:
  224. case BONE_AXIS_MINUS_Z: {
  225. ret = Vector3::AXIS_Z;
  226. } break;
  227. }
  228. return ret;
  229. }
  230. Vector3 SkeletonModifier3D::limit_length(const Vector3 &p_origin, const Vector3 &p_destination, float p_length) {
  231. return p_origin + (p_destination - p_origin).normalized() * p_length;
  232. }
  233. Quaternion SkeletonModifier3D::get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation) {
  234. int parent = p_skeleton->get_bone_parent(p_bone);
  235. if (parent < 0) {
  236. return p_global_pose_rotation;
  237. }
  238. return (p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion().inverse() * p_global_pose_rotation).normalized();
  239. }
  240. Quaternion SkeletonModifier3D::get_from_to_rotation(const Vector3 &p_from, const Vector3 &p_to, const Quaternion &p_prev_rot) {
  241. if (Math::is_equal_approx((float)p_from.dot(p_to), -1.0f)) {
  242. return p_prev_rot; // For preventing to glitch, checking dot for detecting flip is more accurate than checking cross.
  243. }
  244. Vector3 axis = p_from.cross(p_to);
  245. if (axis.is_zero_approx()) {
  246. return p_prev_rot;
  247. }
  248. float angle = p_from.angle_to(p_to);
  249. if (Math::is_zero_approx(angle)) {
  250. angle = 0.0;
  251. }
  252. return Quaternion(axis.normalized(), angle);
  253. }
  254. Quaternion SkeletonModifier3D::get_from_to_rotation_by_axis(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_axis) {
  255. const double ALMOST_ONE = 1.0 - CMP_EPSILON;
  256. double dot = p_from.dot(p_to);
  257. if (dot > ALMOST_ONE) {
  258. return Quaternion();
  259. }
  260. if (dot < -ALMOST_ONE) {
  261. return Quaternion(p_axis, Math::PI);
  262. }
  263. double angle = p_from.angle_to(p_to);
  264. Vector3 cross = p_from.cross(p_to);
  265. if (std::signbit(cross.dot(p_axis))) {
  266. angle = -angle;
  267. }
  268. return Quaternion(p_axis, angle);
  269. }
  270. Quaternion SkeletonModifier3D::get_swing(const Quaternion &p_rotation, const Vector3 &p_axis) {
  271. if (p_axis.is_zero_approx()) {
  272. return p_rotation;
  273. }
  274. Quaternion rot = p_rotation;
  275. if (!rot.is_normalized()) {
  276. rot.normalize();
  277. }
  278. Vector3 axis = p_axis.normalized();
  279. const Vector3 v(rot.x, rot.y, rot.z);
  280. const real_t proj_len = v.dot(axis);
  281. const Vector3 twist_vec = axis * proj_len;
  282. Quaternion twist(twist_vec.x, twist_vec.y, twist_vec.z, rot.w);
  283. if (!twist.is_normalized()) {
  284. if (Math::is_zero_approx(twist.length_squared())) {
  285. return rot;
  286. }
  287. twist.normalize();
  288. }
  289. Quaternion swing = rot * twist.inverse();
  290. swing.normalize();
  291. return swing;
  292. }
  293. Vector3 SkeletonModifier3D::snap_vector_to_plane(const Vector3 &p_plane_normal, const Vector3 &p_vector) {
  294. if (Math::is_zero_approx(p_plane_normal.length_squared())) {
  295. return p_vector;
  296. }
  297. double length = p_vector.length();
  298. Vector3 normalized_vec = p_vector.normalized();
  299. Vector3 normal = p_plane_normal.normalized();
  300. return normalized_vec.slide(normal) * length;
  301. }
  302. double SkeletonModifier3D::symmetrize_angle(double p_angle) {
  303. double angle = Math::fposmod(p_angle, Math::TAU);
  304. return angle > Math::PI ? angle - Math::TAU : angle;
  305. }
  306. double SkeletonModifier3D::get_roll_angle(const Quaternion &p_rotation, const Vector3 &p_roll_axis) {
  307. // Ensure roll axis is normalized.
  308. Vector3 roll_axis = p_roll_axis.normalized();
  309. // Project the quaternion rotation onto the roll axis.
  310. // This gives us the component of rotation around that axis.
  311. double dot = p_rotation.x * roll_axis.x +
  312. p_rotation.y * roll_axis.y +
  313. p_rotation.z * roll_axis.z;
  314. // Create a quaternion representing just the roll component.
  315. Quaternion roll_component;
  316. roll_component.x = roll_axis.x * dot;
  317. roll_component.y = roll_axis.y * dot;
  318. roll_component.z = roll_axis.z * dot;
  319. roll_component.w = p_rotation.w;
  320. // Normalize this component.
  321. double length = roll_component.length();
  322. if (length > CMP_EPSILON) {
  323. roll_component = roll_component / length;
  324. } else {
  325. return 0.0;
  326. }
  327. // Extract the angle.
  328. double angle = 2.0 * Math::acos(CLAMP(roll_component.w, -1.0, 1.0));
  329. // Determine the sign.
  330. double direction = (roll_component.x * roll_axis.x + roll_component.y * roll_axis.y + roll_component.z * roll_axis.z > 0) ? 1.0 : -1.0;
  331. return symmetrize_angle(angle * direction);
  332. }
  333. Vector3 SkeletonModifier3D::get_projected_normal(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_point) {
  334. // Get nearest normal vector to p_point from the infinite line p_a to p_b.
  335. const Vector3 dir = p_b - p_a;
  336. const real_t denom = dir.length_squared();
  337. if (Math::is_zero_approx(denom)) {
  338. return Vector3();
  339. }
  340. const Vector3 w = p_point - p_a;
  341. const real_t t = w.dot(dir) / denom;
  342. const Vector3 h = p_a + dir * t;
  343. return (p_point - h).normalized();
  344. }
  345. SkeletonModifier3D::SkeletonModifier3D() {
  346. }