skeleton_modification_3d_lookat.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*************************************************************************/
  2. /* skeleton_modification_3d_lookat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "scene/resources/skeleton_modification_3d_lookat.h"
  31. #include "scene/3d/skeleton_3d.h"
  32. #include "scene/resources/skeleton_modification_3d.h"
  33. bool SkeletonModification3DLookAt::_set(const StringName &p_path, const Variant &p_value) {
  34. if (p_path == "lock_rotation_to_plane") {
  35. set_lock_rotation_to_plane(p_value);
  36. } else if (p_path == "lock_rotation_plane") {
  37. set_lock_rotation_plane(p_value);
  38. } else if (p_path == "additional_rotation") {
  39. Vector3 tmp = p_value;
  40. tmp.x = Math::deg_to_rad(tmp.x);
  41. tmp.y = Math::deg_to_rad(tmp.y);
  42. tmp.z = Math::deg_to_rad(tmp.z);
  43. set_additional_rotation(tmp);
  44. }
  45. return true;
  46. }
  47. bool SkeletonModification3DLookAt::_get(const StringName &p_path, Variant &r_ret) const {
  48. if (p_path == "lock_rotation_to_plane") {
  49. r_ret = get_lock_rotation_to_plane();
  50. } else if (p_path == "lock_rotation_plane") {
  51. r_ret = get_lock_rotation_plane();
  52. } else if (p_path == "additional_rotation") {
  53. Vector3 tmp = get_additional_rotation();
  54. tmp.x = Math::rad_to_deg(tmp.x);
  55. tmp.y = Math::rad_to_deg(tmp.y);
  56. tmp.z = Math::rad_to_deg(tmp.z);
  57. r_ret = tmp;
  58. }
  59. return true;
  60. }
  61. void SkeletonModification3DLookAt::_get_property_list(List<PropertyInfo> *p_list) const {
  62. p_list->push_back(PropertyInfo(Variant::BOOL, "lock_rotation_to_plane", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  63. if (lock_rotation_to_plane) {
  64. p_list->push_back(PropertyInfo(Variant::INT, "lock_rotation_plane", PROPERTY_HINT_ENUM, "X plane, Y plane, Z plane", PROPERTY_USAGE_DEFAULT));
  65. }
  66. p_list->push_back(PropertyInfo(Variant::VECTOR3, "additional_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  67. }
  68. void SkeletonModification3DLookAt::_execute(real_t p_delta) {
  69. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  70. "Modification is not setup and therefore cannot execute!");
  71. if (!enabled) {
  72. return;
  73. }
  74. if (target_node_cache.is_null()) {
  75. _print_execution_error(true, "Target cache is out of date. Attempting to update...");
  76. update_cache();
  77. return;
  78. }
  79. if (bone_idx <= -2) {
  80. bone_idx = stack->skeleton->find_bone(bone_name);
  81. }
  82. Node3D *target = Object::cast_to<Node3D>(ObjectDB::get_instance(target_node_cache));
  83. if (_print_execution_error(!target || !target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!")) {
  84. return;
  85. }
  86. if (_print_execution_error(bone_idx <= -1, "Bone index is invalid. Cannot execute modification!")) {
  87. return;
  88. }
  89. Transform3D new_bone_trans = stack->skeleton->get_bone_local_pose_override(bone_idx);
  90. if (new_bone_trans == Transform3D()) {
  91. new_bone_trans = stack->skeleton->get_bone_pose(bone_idx);
  92. }
  93. Vector3 target_pos = stack->skeleton->global_pose_to_local_pose(bone_idx, stack->skeleton->world_transform_to_global_pose(target->get_global_transform())).origin;
  94. // Lock the rotation to a plane relative to the bone by changing the target position
  95. if (lock_rotation_to_plane) {
  96. if (lock_rotation_plane == ROTATION_PLANE::ROTATION_PLANE_X) {
  97. target_pos.x = new_bone_trans.origin.x;
  98. } else if (lock_rotation_plane == ROTATION_PLANE::ROTATION_PLANE_Y) {
  99. target_pos.y = new_bone_trans.origin.y;
  100. } else if (lock_rotation_plane == ROTATION_PLANE::ROTATION_PLANE_Z) {
  101. target_pos.z = new_bone_trans.origin.z;
  102. }
  103. }
  104. // Look at the target!
  105. new_bone_trans = new_bone_trans.looking_at(target_pos, Vector3(0, 1, 0));
  106. // Convert from Z-forward to whatever direction the bone faces.
  107. stack->skeleton->update_bone_rest_forward_vector(bone_idx);
  108. new_bone_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(bone_idx, new_bone_trans.basis);
  109. // Apply additional rotation
  110. new_bone_trans.basis.rotate_local(Vector3(1, 0, 0), additional_rotation.x);
  111. new_bone_trans.basis.rotate_local(Vector3(0, 1, 0), additional_rotation.y);
  112. new_bone_trans.basis.rotate_local(Vector3(0, 0, 1), additional_rotation.z);
  113. stack->skeleton->set_bone_local_pose_override(bone_idx, new_bone_trans, stack->strength, true);
  114. stack->skeleton->force_update_bone_children_transforms(bone_idx);
  115. // If we completed it successfully, then we can set execution_error_found to false
  116. execution_error_found = false;
  117. }
  118. void SkeletonModification3DLookAt::_setup_modification(SkeletonModificationStack3D *p_stack) {
  119. stack = p_stack;
  120. if (stack != nullptr) {
  121. is_setup = true;
  122. execution_error_found = false;
  123. update_cache();
  124. }
  125. }
  126. void SkeletonModification3DLookAt::set_bone_name(String p_name) {
  127. bone_name = p_name;
  128. if (stack) {
  129. if (stack->skeleton) {
  130. bone_idx = stack->skeleton->find_bone(bone_name);
  131. }
  132. }
  133. execution_error_found = false;
  134. notify_property_list_changed();
  135. }
  136. String SkeletonModification3DLookAt::get_bone_name() const {
  137. return bone_name;
  138. }
  139. int SkeletonModification3DLookAt::get_bone_index() const {
  140. return bone_idx;
  141. }
  142. void SkeletonModification3DLookAt::set_bone_index(int p_bone_idx) {
  143. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  144. bone_idx = p_bone_idx;
  145. if (stack) {
  146. if (stack->skeleton) {
  147. bone_name = stack->skeleton->get_bone_name(p_bone_idx);
  148. }
  149. }
  150. execution_error_found = false;
  151. notify_property_list_changed();
  152. }
  153. void SkeletonModification3DLookAt::update_cache() {
  154. if (!is_setup || !stack) {
  155. _print_execution_error(true, "Cannot update target cache: modification is not properly setup!");
  156. return;
  157. }
  158. target_node_cache = ObjectID();
  159. if (stack->skeleton) {
  160. if (stack->skeleton->is_inside_tree()) {
  161. if (stack->skeleton->has_node(target_node)) {
  162. Node *node = stack->skeleton->get_node(target_node);
  163. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  164. "Cannot update target cache: Node is this modification's skeleton or cannot be found!");
  165. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  166. "Cannot update target cache: Node is not in the scene tree!");
  167. target_node_cache = node->get_instance_id();
  168. execution_error_found = false;
  169. }
  170. }
  171. }
  172. }
  173. void SkeletonModification3DLookAt::set_target_node(const NodePath &p_target_node) {
  174. target_node = p_target_node;
  175. update_cache();
  176. }
  177. NodePath SkeletonModification3DLookAt::get_target_node() const {
  178. return target_node;
  179. }
  180. Vector3 SkeletonModification3DLookAt::get_additional_rotation() const {
  181. return additional_rotation;
  182. }
  183. void SkeletonModification3DLookAt::set_additional_rotation(Vector3 p_offset) {
  184. additional_rotation = p_offset;
  185. }
  186. bool SkeletonModification3DLookAt::get_lock_rotation_to_plane() const {
  187. return lock_rotation_plane;
  188. }
  189. void SkeletonModification3DLookAt::set_lock_rotation_to_plane(bool p_lock_rotation) {
  190. lock_rotation_to_plane = p_lock_rotation;
  191. notify_property_list_changed();
  192. }
  193. int SkeletonModification3DLookAt::get_lock_rotation_plane() const {
  194. return lock_rotation_plane;
  195. }
  196. void SkeletonModification3DLookAt::set_lock_rotation_plane(int p_plane) {
  197. lock_rotation_plane = p_plane;
  198. }
  199. void SkeletonModification3DLookAt::_bind_methods() {
  200. ClassDB::bind_method(D_METHOD("set_bone_name", "name"), &SkeletonModification3DLookAt::set_bone_name);
  201. ClassDB::bind_method(D_METHOD("get_bone_name"), &SkeletonModification3DLookAt::get_bone_name);
  202. ClassDB::bind_method(D_METHOD("set_bone_index", "bone_idx"), &SkeletonModification3DLookAt::set_bone_index);
  203. ClassDB::bind_method(D_METHOD("get_bone_index"), &SkeletonModification3DLookAt::get_bone_index);
  204. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DLookAt::set_target_node);
  205. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DLookAt::get_target_node);
  206. ClassDB::bind_method(D_METHOD("set_additional_rotation", "additional_rotation"), &SkeletonModification3DLookAt::set_additional_rotation);
  207. ClassDB::bind_method(D_METHOD("get_additional_rotation"), &SkeletonModification3DLookAt::get_additional_rotation);
  208. ClassDB::bind_method(D_METHOD("set_lock_rotation_to_plane", "lock_to_plane"), &SkeletonModification3DLookAt::set_lock_rotation_to_plane);
  209. ClassDB::bind_method(D_METHOD("get_lock_rotation_to_plane"), &SkeletonModification3DLookAt::get_lock_rotation_to_plane);
  210. ClassDB::bind_method(D_METHOD("set_lock_rotation_plane", "plane"), &SkeletonModification3DLookAt::set_lock_rotation_plane);
  211. ClassDB::bind_method(D_METHOD("get_lock_rotation_plane"), &SkeletonModification3DLookAt::get_lock_rotation_plane);
  212. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
  213. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_index"), "set_bone_index", "get_bone_index");
  214. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node3D"), "set_target_node", "get_target_node");
  215. }
  216. SkeletonModification3DLookAt::SkeletonModification3DLookAt() {
  217. stack = nullptr;
  218. is_setup = false;
  219. bone_name = "";
  220. bone_idx = -2;
  221. additional_rotation = Vector3();
  222. lock_rotation_to_plane = false;
  223. enabled = true;
  224. }
  225. SkeletonModification3DLookAt::~SkeletonModification3DLookAt() {
  226. }