skeleton_modification_3d_jiggle.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*************************************************************************/
  2. /* skeleton_modification_3d_jiggle.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_jiggle.h"
  31. #include "scene/3d/skeleton_3d.h"
  32. #include "scene/resources/skeleton_modification_3d.h"
  33. bool SkeletonModification3DJiggle::_set(const StringName &p_path, const Variant &p_value) {
  34. String path = p_path;
  35. if (path.begins_with("joint_data/")) {
  36. const int jiggle_size = jiggle_data_chain.size();
  37. int which = path.get_slicec('/', 1).to_int();
  38. String what = path.get_slicec('/', 2);
  39. ERR_FAIL_INDEX_V(which, jiggle_size, false);
  40. if (what == "bone_name") {
  41. set_jiggle_joint_bone_name(which, p_value);
  42. } else if (what == "bone_index") {
  43. set_jiggle_joint_bone_index(which, p_value);
  44. } else if (what == "override_defaults") {
  45. set_jiggle_joint_override(which, p_value);
  46. } else if (what == "stiffness") {
  47. set_jiggle_joint_stiffness(which, p_value);
  48. } else if (what == "mass") {
  49. set_jiggle_joint_mass(which, p_value);
  50. } else if (what == "damping") {
  51. set_jiggle_joint_damping(which, p_value);
  52. } else if (what == "use_gravity") {
  53. set_jiggle_joint_use_gravity(which, p_value);
  54. } else if (what == "gravity") {
  55. set_jiggle_joint_gravity(which, p_value);
  56. } else if (what == "roll") {
  57. set_jiggle_joint_roll(which, Math::deg_to_rad(real_t(p_value)));
  58. }
  59. return true;
  60. } else {
  61. if (path == "use_colliders") {
  62. set_use_colliders(p_value);
  63. } else if (path == "collision_mask") {
  64. set_collision_mask(p_value);
  65. }
  66. return true;
  67. }
  68. return true;
  69. }
  70. bool SkeletonModification3DJiggle::_get(const StringName &p_path, Variant &r_ret) const {
  71. String path = p_path;
  72. if (path.begins_with("joint_data/")) {
  73. const int jiggle_size = jiggle_data_chain.size();
  74. int which = path.get_slicec('/', 1).to_int();
  75. String what = path.get_slicec('/', 2);
  76. ERR_FAIL_INDEX_V(which, jiggle_size, false);
  77. if (what == "bone_name") {
  78. r_ret = get_jiggle_joint_bone_name(which);
  79. } else if (what == "bone_index") {
  80. r_ret = get_jiggle_joint_bone_index(which);
  81. } else if (what == "override_defaults") {
  82. r_ret = get_jiggle_joint_override(which);
  83. } else if (what == "stiffness") {
  84. r_ret = get_jiggle_joint_stiffness(which);
  85. } else if (what == "mass") {
  86. r_ret = get_jiggle_joint_mass(which);
  87. } else if (what == "damping") {
  88. r_ret = get_jiggle_joint_damping(which);
  89. } else if (what == "use_gravity") {
  90. r_ret = get_jiggle_joint_use_gravity(which);
  91. } else if (what == "gravity") {
  92. r_ret = get_jiggle_joint_gravity(which);
  93. } else if (what == "roll") {
  94. r_ret = Math::rad_to_deg(get_jiggle_joint_roll(which));
  95. }
  96. return true;
  97. } else {
  98. if (path == "use_colliders") {
  99. r_ret = get_use_colliders();
  100. } else if (path == "collision_mask") {
  101. r_ret = get_collision_mask();
  102. }
  103. return true;
  104. }
  105. return true;
  106. }
  107. void SkeletonModification3DJiggle::_get_property_list(List<PropertyInfo> *p_list) const {
  108. p_list->push_back(PropertyInfo(Variant::BOOL, "use_colliders", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  109. if (use_colliders) {
  110. p_list->push_back(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS, "", PROPERTY_USAGE_DEFAULT));
  111. }
  112. for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) {
  113. String base_string = "joint_data/" + itos(i) + "/";
  114. p_list->push_back(PropertyInfo(Variant::STRING_NAME, base_string + "bone_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  115. p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  116. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "roll", PROPERTY_HINT_RANGE, "-360,360,0.01", PROPERTY_USAGE_DEFAULT));
  117. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "override_defaults", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  118. if (jiggle_data_chain[i].override_defaults) {
  119. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "stiffness", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
  120. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "mass", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
  121. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01", PROPERTY_USAGE_DEFAULT));
  122. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  123. if (jiggle_data_chain[i].use_gravity) {
  124. p_list->push_back(PropertyInfo(Variant::VECTOR3, base_string + "gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  125. }
  126. }
  127. }
  128. }
  129. void SkeletonModification3DJiggle::_execute(real_t p_delta) {
  130. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  131. "Modification is not setup and therefore cannot execute!");
  132. if (!enabled) {
  133. return;
  134. }
  135. if (target_node_cache.is_null()) {
  136. _print_execution_error(true, "Target cache is out of date. Attempting to update...");
  137. update_cache();
  138. return;
  139. }
  140. Node3D *target = Object::cast_to<Node3D>(ObjectDB::get_instance(target_node_cache));
  141. _print_execution_error(!target || !target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!");
  142. for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) {
  143. _execute_jiggle_joint(i, target, p_delta);
  144. }
  145. execution_error_found = false;
  146. }
  147. void SkeletonModification3DJiggle::_execute_jiggle_joint(int p_joint_idx, Node3D *p_target, real_t p_delta) {
  148. // Adopted from: https://wiki.unity3d.com/index.php/JiggleBone
  149. // With modifications by TwistedTwigleg.
  150. if (jiggle_data_chain[p_joint_idx].bone_idx <= -2) {
  151. jiggle_data_chain[p_joint_idx].bone_idx = stack->skeleton->find_bone(jiggle_data_chain[p_joint_idx].bone_name);
  152. }
  153. if (_print_execution_error(
  154. jiggle_data_chain[p_joint_idx].bone_idx < 0 || jiggle_data_chain[p_joint_idx].bone_idx > stack->skeleton->get_bone_count(),
  155. "Jiggle joint " + itos(p_joint_idx) + " bone index is invalid. Cannot execute modification!")) {
  156. return;
  157. }
  158. Transform3D bone_local_pos = stack->skeleton->get_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx);
  159. if (bone_local_pos == Transform3D()) {
  160. bone_local_pos = stack->skeleton->get_bone_pose(jiggle_data_chain[p_joint_idx].bone_idx);
  161. }
  162. Transform3D new_bone_trans = stack->skeleton->local_pose_to_global_pose(jiggle_data_chain[p_joint_idx].bone_idx, bone_local_pos);
  163. Vector3 target_position = stack->skeleton->world_transform_to_global_pose(p_target->get_global_transform()).origin;
  164. jiggle_data_chain[p_joint_idx].force = (target_position - jiggle_data_chain[p_joint_idx].dynamic_position) * jiggle_data_chain[p_joint_idx].stiffness * p_delta;
  165. if (jiggle_data_chain[p_joint_idx].use_gravity) {
  166. Vector3 gravity_to_apply = new_bone_trans.basis.inverse().xform(jiggle_data_chain[p_joint_idx].gravity);
  167. jiggle_data_chain[p_joint_idx].force += gravity_to_apply * p_delta;
  168. }
  169. jiggle_data_chain[p_joint_idx].acceleration = jiggle_data_chain[p_joint_idx].force / jiggle_data_chain[p_joint_idx].mass;
  170. jiggle_data_chain[p_joint_idx].velocity += jiggle_data_chain[p_joint_idx].acceleration * (1 - jiggle_data_chain[p_joint_idx].damping);
  171. jiggle_data_chain[p_joint_idx].dynamic_position += jiggle_data_chain[p_joint_idx].velocity + jiggle_data_chain[p_joint_idx].force;
  172. jiggle_data_chain[p_joint_idx].dynamic_position += new_bone_trans.origin - jiggle_data_chain[p_joint_idx].last_position;
  173. jiggle_data_chain[p_joint_idx].last_position = new_bone_trans.origin;
  174. // Collision detection/response
  175. if (use_colliders) {
  176. if (execution_mode == SkeletonModificationStack3D::EXECUTION_MODE::execution_mode_physics_process) {
  177. Ref<World3D> world_3d = stack->skeleton->get_world_3d();
  178. ERR_FAIL_COND(world_3d.is_null());
  179. PhysicsDirectSpaceState3D *space_state = PhysicsServer3D::get_singleton()->space_get_direct_state(world_3d->get_space());
  180. PhysicsDirectSpaceState3D::RayResult ray_result;
  181. // Convert to world transforms, which is what the physics server needs
  182. Transform3D new_bone_trans_world = stack->skeleton->global_pose_to_world_transform(new_bone_trans);
  183. Transform3D dynamic_position_world = stack->skeleton->global_pose_to_world_transform(Transform3D(Basis(), jiggle_data_chain[p_joint_idx].dynamic_position));
  184. PhysicsDirectSpaceState3D::RayParameters ray_params;
  185. ray_params.from = new_bone_trans_world.origin;
  186. ray_params.to = dynamic_position_world.get_origin();
  187. ray_params.collision_mask = collision_mask;
  188. bool ray_hit = space_state->intersect_ray(ray_params, ray_result);
  189. if (ray_hit) {
  190. jiggle_data_chain[p_joint_idx].dynamic_position = jiggle_data_chain[p_joint_idx].last_noncollision_position;
  191. jiggle_data_chain[p_joint_idx].acceleration = Vector3(0, 0, 0);
  192. jiggle_data_chain[p_joint_idx].velocity = Vector3(0, 0, 0);
  193. } else {
  194. jiggle_data_chain[p_joint_idx].last_noncollision_position = jiggle_data_chain[p_joint_idx].dynamic_position;
  195. }
  196. } else {
  197. WARN_PRINT_ONCE("Jiggle modifier: You cannot detect colliders without the stack mode being set to _physics_process!");
  198. }
  199. }
  200. // Get the forward direction that the basis is facing in right now.
  201. stack->skeleton->update_bone_rest_forward_vector(jiggle_data_chain[p_joint_idx].bone_idx);
  202. Vector3 forward_vector = stack->skeleton->get_bone_axis_forward_vector(jiggle_data_chain[p_joint_idx].bone_idx);
  203. // Rotate the bone using the dynamic position!
  204. new_bone_trans.basis.rotate_to_align(forward_vector, new_bone_trans.origin.direction_to(jiggle_data_chain[p_joint_idx].dynamic_position));
  205. // Roll
  206. new_bone_trans.basis.rotate_local(forward_vector, jiggle_data_chain[p_joint_idx].roll);
  207. new_bone_trans = stack->skeleton->global_pose_to_local_pose(jiggle_data_chain[p_joint_idx].bone_idx, new_bone_trans);
  208. stack->skeleton->set_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx, new_bone_trans, stack->strength, true);
  209. stack->skeleton->force_update_bone_children_transforms(jiggle_data_chain[p_joint_idx].bone_idx);
  210. }
  211. void SkeletonModification3DJiggle::_update_jiggle_joint_data() {
  212. for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) {
  213. if (!jiggle_data_chain[i].override_defaults) {
  214. set_jiggle_joint_stiffness(i, stiffness);
  215. set_jiggle_joint_mass(i, mass);
  216. set_jiggle_joint_damping(i, damping);
  217. set_jiggle_joint_use_gravity(i, use_gravity);
  218. set_jiggle_joint_gravity(i, gravity);
  219. }
  220. }
  221. }
  222. void SkeletonModification3DJiggle::_setup_modification(SkeletonModificationStack3D *p_stack) {
  223. stack = p_stack;
  224. if (stack) {
  225. is_setup = true;
  226. execution_error_found = false;
  227. if (stack->skeleton) {
  228. for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) {
  229. int bone_idx = jiggle_data_chain[i].bone_idx;
  230. if (bone_idx > 0 && bone_idx < stack->skeleton->get_bone_count()) {
  231. jiggle_data_chain[i].dynamic_position = stack->skeleton->local_pose_to_global_pose(bone_idx, stack->skeleton->get_bone_local_pose_override(bone_idx)).origin;
  232. }
  233. }
  234. }
  235. update_cache();
  236. }
  237. }
  238. void SkeletonModification3DJiggle::update_cache() {
  239. if (!is_setup || !stack) {
  240. _print_execution_error(true, "Cannot update target cache: modification is not properly setup!");
  241. return;
  242. }
  243. target_node_cache = ObjectID();
  244. if (stack->skeleton) {
  245. if (stack->skeleton->is_inside_tree()) {
  246. if (stack->skeleton->has_node(target_node)) {
  247. Node *node = stack->skeleton->get_node(target_node);
  248. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  249. "Cannot update target cache: node is this modification's skeleton or cannot be found!");
  250. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  251. "Cannot update target cache: node is not in the scene tree!");
  252. target_node_cache = node->get_instance_id();
  253. execution_error_found = false;
  254. }
  255. }
  256. }
  257. }
  258. void SkeletonModification3DJiggle::set_target_node(const NodePath &p_target_node) {
  259. target_node = p_target_node;
  260. update_cache();
  261. }
  262. NodePath SkeletonModification3DJiggle::get_target_node() const {
  263. return target_node;
  264. }
  265. void SkeletonModification3DJiggle::set_stiffness(real_t p_stiffness) {
  266. ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
  267. stiffness = p_stiffness;
  268. _update_jiggle_joint_data();
  269. }
  270. real_t SkeletonModification3DJiggle::get_stiffness() const {
  271. return stiffness;
  272. }
  273. void SkeletonModification3DJiggle::set_mass(real_t p_mass) {
  274. ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
  275. mass = p_mass;
  276. _update_jiggle_joint_data();
  277. }
  278. real_t SkeletonModification3DJiggle::get_mass() const {
  279. return mass;
  280. }
  281. void SkeletonModification3DJiggle::set_damping(real_t p_damping) {
  282. ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
  283. ERR_FAIL_COND_MSG(p_damping > 1, "Damping cannot be more than one!");
  284. damping = p_damping;
  285. _update_jiggle_joint_data();
  286. }
  287. real_t SkeletonModification3DJiggle::get_damping() const {
  288. return damping;
  289. }
  290. void SkeletonModification3DJiggle::set_use_gravity(bool p_use_gravity) {
  291. use_gravity = p_use_gravity;
  292. _update_jiggle_joint_data();
  293. }
  294. bool SkeletonModification3DJiggle::get_use_gravity() const {
  295. return use_gravity;
  296. }
  297. void SkeletonModification3DJiggle::set_gravity(Vector3 p_gravity) {
  298. gravity = p_gravity;
  299. _update_jiggle_joint_data();
  300. }
  301. Vector3 SkeletonModification3DJiggle::get_gravity() const {
  302. return gravity;
  303. }
  304. void SkeletonModification3DJiggle::set_use_colliders(bool p_use_collider) {
  305. use_colliders = p_use_collider;
  306. notify_property_list_changed();
  307. }
  308. bool SkeletonModification3DJiggle::get_use_colliders() const {
  309. return use_colliders;
  310. }
  311. void SkeletonModification3DJiggle::set_collision_mask(int p_mask) {
  312. collision_mask = p_mask;
  313. }
  314. int SkeletonModification3DJiggle::get_collision_mask() const {
  315. return collision_mask;
  316. }
  317. // Jiggle joint data functions
  318. int SkeletonModification3DJiggle::get_jiggle_data_chain_length() {
  319. return jiggle_data_chain.size();
  320. }
  321. void SkeletonModification3DJiggle::set_jiggle_data_chain_length(int p_length) {
  322. ERR_FAIL_COND(p_length < 0);
  323. jiggle_data_chain.resize(p_length);
  324. execution_error_found = false;
  325. notify_property_list_changed();
  326. }
  327. void SkeletonModification3DJiggle::set_jiggle_joint_bone_name(int p_joint_idx, String p_name) {
  328. const int bone_chain_size = jiggle_data_chain.size();
  329. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  330. jiggle_data_chain[p_joint_idx].bone_name = p_name;
  331. if (stack && stack->skeleton) {
  332. jiggle_data_chain[p_joint_idx].bone_idx = stack->skeleton->find_bone(p_name);
  333. }
  334. execution_error_found = false;
  335. notify_property_list_changed();
  336. }
  337. String SkeletonModification3DJiggle::get_jiggle_joint_bone_name(int p_joint_idx) const {
  338. const int bone_chain_size = jiggle_data_chain.size();
  339. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, "");
  340. return jiggle_data_chain[p_joint_idx].bone_name;
  341. }
  342. int SkeletonModification3DJiggle::get_jiggle_joint_bone_index(int p_joint_idx) const {
  343. const int bone_chain_size = jiggle_data_chain.size();
  344. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1);
  345. return jiggle_data_chain[p_joint_idx].bone_idx;
  346. }
  347. void SkeletonModification3DJiggle::set_jiggle_joint_bone_index(int p_joint_idx, int p_bone_idx) {
  348. const int bone_chain_size = jiggle_data_chain.size();
  349. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  350. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  351. jiggle_data_chain[p_joint_idx].bone_idx = p_bone_idx;
  352. if (stack) {
  353. if (stack->skeleton) {
  354. jiggle_data_chain[p_joint_idx].bone_name = stack->skeleton->get_bone_name(p_bone_idx);
  355. }
  356. }
  357. execution_error_found = false;
  358. notify_property_list_changed();
  359. }
  360. void SkeletonModification3DJiggle::set_jiggle_joint_override(int p_joint_idx, bool p_override) {
  361. const int bone_chain_size = jiggle_data_chain.size();
  362. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  363. jiggle_data_chain[p_joint_idx].override_defaults = p_override;
  364. _update_jiggle_joint_data();
  365. notify_property_list_changed();
  366. }
  367. bool SkeletonModification3DJiggle::get_jiggle_joint_override(int p_joint_idx) const {
  368. const int bone_chain_size = jiggle_data_chain.size();
  369. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false);
  370. return jiggle_data_chain[p_joint_idx].override_defaults;
  371. }
  372. void SkeletonModification3DJiggle::set_jiggle_joint_stiffness(int p_joint_idx, real_t p_stiffness) {
  373. const int bone_chain_size = jiggle_data_chain.size();
  374. ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
  375. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  376. jiggle_data_chain[p_joint_idx].stiffness = p_stiffness;
  377. }
  378. real_t SkeletonModification3DJiggle::get_jiggle_joint_stiffness(int p_joint_idx) const {
  379. const int bone_chain_size = jiggle_data_chain.size();
  380. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1);
  381. return jiggle_data_chain[p_joint_idx].stiffness;
  382. }
  383. void SkeletonModification3DJiggle::set_jiggle_joint_mass(int p_joint_idx, real_t p_mass) {
  384. const int bone_chain_size = jiggle_data_chain.size();
  385. ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
  386. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  387. jiggle_data_chain[p_joint_idx].mass = p_mass;
  388. }
  389. real_t SkeletonModification3DJiggle::get_jiggle_joint_mass(int p_joint_idx) const {
  390. const int bone_chain_size = jiggle_data_chain.size();
  391. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1);
  392. return jiggle_data_chain[p_joint_idx].mass;
  393. }
  394. void SkeletonModification3DJiggle::set_jiggle_joint_damping(int p_joint_idx, real_t p_damping) {
  395. const int bone_chain_size = jiggle_data_chain.size();
  396. ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
  397. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  398. jiggle_data_chain[p_joint_idx].damping = p_damping;
  399. }
  400. real_t SkeletonModification3DJiggle::get_jiggle_joint_damping(int p_joint_idx) const {
  401. const int bone_chain_size = jiggle_data_chain.size();
  402. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1);
  403. return jiggle_data_chain[p_joint_idx].damping;
  404. }
  405. void SkeletonModification3DJiggle::set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity) {
  406. const int bone_chain_size = jiggle_data_chain.size();
  407. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  408. jiggle_data_chain[p_joint_idx].use_gravity = p_use_gravity;
  409. notify_property_list_changed();
  410. }
  411. bool SkeletonModification3DJiggle::get_jiggle_joint_use_gravity(int p_joint_idx) const {
  412. const int bone_chain_size = jiggle_data_chain.size();
  413. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false);
  414. return jiggle_data_chain[p_joint_idx].use_gravity;
  415. }
  416. void SkeletonModification3DJiggle::set_jiggle_joint_gravity(int p_joint_idx, Vector3 p_gravity) {
  417. const int bone_chain_size = jiggle_data_chain.size();
  418. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  419. jiggle_data_chain[p_joint_idx].gravity = p_gravity;
  420. }
  421. Vector3 SkeletonModification3DJiggle::get_jiggle_joint_gravity(int p_joint_idx) const {
  422. const int bone_chain_size = jiggle_data_chain.size();
  423. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, Vector3(0, 0, 0));
  424. return jiggle_data_chain[p_joint_idx].gravity;
  425. }
  426. void SkeletonModification3DJiggle::set_jiggle_joint_roll(int p_joint_idx, real_t p_roll) {
  427. const int bone_chain_size = jiggle_data_chain.size();
  428. ERR_FAIL_INDEX(p_joint_idx, bone_chain_size);
  429. jiggle_data_chain[p_joint_idx].roll = p_roll;
  430. }
  431. real_t SkeletonModification3DJiggle::get_jiggle_joint_roll(int p_joint_idx) const {
  432. const int bone_chain_size = jiggle_data_chain.size();
  433. ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, 0.0);
  434. return jiggle_data_chain[p_joint_idx].roll;
  435. }
  436. void SkeletonModification3DJiggle::_bind_methods() {
  437. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DJiggle::set_target_node);
  438. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DJiggle::get_target_node);
  439. ClassDB::bind_method(D_METHOD("set_jiggle_data_chain_length", "length"), &SkeletonModification3DJiggle::set_jiggle_data_chain_length);
  440. ClassDB::bind_method(D_METHOD("get_jiggle_data_chain_length"), &SkeletonModification3DJiggle::get_jiggle_data_chain_length);
  441. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &SkeletonModification3DJiggle::set_stiffness);
  442. ClassDB::bind_method(D_METHOD("get_stiffness"), &SkeletonModification3DJiggle::get_stiffness);
  443. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &SkeletonModification3DJiggle::set_mass);
  444. ClassDB::bind_method(D_METHOD("get_mass"), &SkeletonModification3DJiggle::get_mass);
  445. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &SkeletonModification3DJiggle::set_damping);
  446. ClassDB::bind_method(D_METHOD("get_damping"), &SkeletonModification3DJiggle::get_damping);
  447. ClassDB::bind_method(D_METHOD("set_use_gravity", "use_gravity"), &SkeletonModification3DJiggle::set_use_gravity);
  448. ClassDB::bind_method(D_METHOD("get_use_gravity"), &SkeletonModification3DJiggle::get_use_gravity);
  449. ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &SkeletonModification3DJiggle::set_gravity);
  450. ClassDB::bind_method(D_METHOD("get_gravity"), &SkeletonModification3DJiggle::get_gravity);
  451. ClassDB::bind_method(D_METHOD("set_use_colliders", "use_colliders"), &SkeletonModification3DJiggle::set_use_colliders);
  452. ClassDB::bind_method(D_METHOD("get_use_colliders"), &SkeletonModification3DJiggle::get_use_colliders);
  453. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &SkeletonModification3DJiggle::set_collision_mask);
  454. ClassDB::bind_method(D_METHOD("get_collision_mask"), &SkeletonModification3DJiggle::get_collision_mask);
  455. // Jiggle joint data functions
  456. ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_name", "joint_idx", "name"), &SkeletonModification3DJiggle::set_jiggle_joint_bone_name);
  457. ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_name", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_bone_name);
  458. ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification3DJiggle::set_jiggle_joint_bone_index);
  459. ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_index", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_bone_index);
  460. ClassDB::bind_method(D_METHOD("set_jiggle_joint_override", "joint_idx", "override"), &SkeletonModification3DJiggle::set_jiggle_joint_override);
  461. ClassDB::bind_method(D_METHOD("get_jiggle_joint_override", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_override);
  462. ClassDB::bind_method(D_METHOD("set_jiggle_joint_stiffness", "joint_idx", "stiffness"), &SkeletonModification3DJiggle::set_jiggle_joint_stiffness);
  463. ClassDB::bind_method(D_METHOD("get_jiggle_joint_stiffness", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_stiffness);
  464. ClassDB::bind_method(D_METHOD("set_jiggle_joint_mass", "joint_idx", "mass"), &SkeletonModification3DJiggle::set_jiggle_joint_mass);
  465. ClassDB::bind_method(D_METHOD("get_jiggle_joint_mass", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_mass);
  466. ClassDB::bind_method(D_METHOD("set_jiggle_joint_damping", "joint_idx", "damping"), &SkeletonModification3DJiggle::set_jiggle_joint_damping);
  467. ClassDB::bind_method(D_METHOD("get_jiggle_joint_damping", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_damping);
  468. ClassDB::bind_method(D_METHOD("set_jiggle_joint_use_gravity", "joint_idx", "use_gravity"), &SkeletonModification3DJiggle::set_jiggle_joint_use_gravity);
  469. ClassDB::bind_method(D_METHOD("get_jiggle_joint_use_gravity", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_use_gravity);
  470. ClassDB::bind_method(D_METHOD("set_jiggle_joint_gravity", "joint_idx", "gravity"), &SkeletonModification3DJiggle::set_jiggle_joint_gravity);
  471. ClassDB::bind_method(D_METHOD("get_jiggle_joint_gravity", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_gravity);
  472. ClassDB::bind_method(D_METHOD("set_jiggle_joint_roll", "joint_idx", "roll"), &SkeletonModification3DJiggle::set_jiggle_joint_roll);
  473. ClassDB::bind_method(D_METHOD("get_jiggle_joint_roll", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_roll);
  474. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node3D"), "set_target_node", "get_target_node");
  475. ADD_PROPERTY(PropertyInfo(Variant::INT, "jiggle_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_jiggle_data_chain_length", "get_jiggle_data_chain_length");
  476. ADD_GROUP("Default Joint Settings", "");
  477. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness"), "set_stiffness", "get_stiffness");
  478. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
  479. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01"), "set_damping", "get_damping");
  480. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_gravity"), "set_use_gravity", "get_use_gravity");
  481. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  482. ADD_GROUP("", "");
  483. }
  484. SkeletonModification3DJiggle::SkeletonModification3DJiggle() {
  485. stack = nullptr;
  486. is_setup = false;
  487. jiggle_data_chain = Vector<Jiggle_Joint_Data>();
  488. stiffness = 3;
  489. mass = 0.75;
  490. damping = 0.75;
  491. use_gravity = false;
  492. gravity = Vector3(0, -6.0, 0);
  493. enabled = true;
  494. }
  495. SkeletonModification3DJiggle::~SkeletonModification3DJiggle() {
  496. }