skeleton.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* skeleton.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef SKELETON_H
  31. #define SKELETON_H
  32. #include "core/rid.h"
  33. #include "scene/3d/spatial.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. #ifndef _3D_DISABLED
  38. typedef int BoneId;
  39. class PhysicalBone;
  40. #endif // _3D_DISABLED
  41. class Skeleton : public Spatial {
  42. GDCLASS(Skeleton, Spatial);
  43. struct Bone {
  44. String name;
  45. bool enabled;
  46. int parent;
  47. int sort_index; //used for re-sorting process order
  48. bool ignore_animation;
  49. bool disable_rest;
  50. Transform rest;
  51. Transform rest_global_inverse;
  52. Transform pose;
  53. Transform pose_global;
  54. bool custom_pose_enable;
  55. Transform custom_pose;
  56. Transform transform_final;
  57. #ifndef _3D_DISABLED
  58. PhysicalBone *physical_bone;
  59. PhysicalBone *cache_parent_physical_bone;
  60. #endif // _3D_DISABLED
  61. List<uint32_t> nodes_bound;
  62. Bone() {
  63. parent = -1;
  64. enabled = true;
  65. ignore_animation = false;
  66. custom_pose_enable = false;
  67. disable_rest = false;
  68. #ifndef _3D_DISABLED
  69. physical_bone = NULL;
  70. cache_parent_physical_bone = NULL;
  71. #endif // _3D_DISABLED
  72. }
  73. };
  74. bool rest_global_inverse_dirty;
  75. Vector<Bone> bones;
  76. Vector<int> process_order;
  77. bool process_order_dirty;
  78. RID skeleton;
  79. void _make_dirty();
  80. bool dirty;
  81. // bind helpers
  82. Array _get_bound_child_nodes_to_bone(int p_bone) const {
  83. Array bound;
  84. List<Node *> children;
  85. get_bound_child_nodes_to_bone(p_bone, &children);
  86. for (int i = 0; i < children.size(); i++) {
  87. bound.push_back(children[i]);
  88. }
  89. return bound;
  90. }
  91. void _update_process_order();
  92. protected:
  93. bool _get(const StringName &p_path, Variant &r_ret) const;
  94. bool _set(const StringName &p_path, const Variant &p_value);
  95. void _get_property_list(List<PropertyInfo> *p_list) const;
  96. void _notification(int p_what);
  97. static void _bind_methods();
  98. public:
  99. enum {
  100. NOTIFICATION_UPDATE_SKELETON = 50
  101. };
  102. RID get_skeleton() const;
  103. // skeleton creation api
  104. void add_bone(const String &p_name);
  105. int find_bone(const String &p_name) const;
  106. String get_bone_name(int p_bone) const;
  107. bool is_bone_parent_of(int p_bone_id, int p_parent_bone_id) const;
  108. void set_bone_parent(int p_bone, int p_parent);
  109. int get_bone_parent(int p_bone) const;
  110. void unparent_bone_and_rest(int p_bone);
  111. void set_bone_ignore_animation(int p_bone, bool p_ignore);
  112. bool is_bone_ignore_animation(int p_bone) const;
  113. void set_bone_disable_rest(int p_bone, bool p_disable);
  114. bool is_bone_rest_disabled(int p_bone) const;
  115. int get_bone_count() const;
  116. void set_bone_rest(int p_bone, const Transform &p_rest);
  117. Transform get_bone_rest(int p_bone) const;
  118. Transform get_bone_transform(int p_bone) const;
  119. Transform get_bone_global_pose(int p_bone) const;
  120. void set_bone_global_pose(int p_bone, const Transform &p_pose);
  121. void set_bone_enabled(int p_bone, bool p_enabled);
  122. bool is_bone_enabled(int p_bone) const;
  123. void bind_child_node_to_bone(int p_bone, Node *p_node);
  124. void unbind_child_node_from_bone(int p_bone, Node *p_node);
  125. void get_bound_child_nodes_to_bone(int p_bone, List<Node *> *p_bound) const;
  126. void clear_bones();
  127. // posing api
  128. void set_bone_pose(int p_bone, const Transform &p_pose);
  129. Transform get_bone_pose(int p_bone) const;
  130. void set_bone_custom_pose(int p_bone, const Transform &p_custom_pose);
  131. Transform get_bone_custom_pose(int p_bone) const;
  132. void localize_rests(); // used for loaders and tools
  133. int get_process_order(int p_idx);
  134. #ifndef _3D_DISABLED
  135. // Physical bone API
  136. void bind_physical_bone_to_bone(int p_bone, PhysicalBone *p_physical_bone);
  137. void unbind_physical_bone_from_bone(int p_bone);
  138. PhysicalBone *get_physical_bone(int p_bone);
  139. PhysicalBone *get_physical_bone_parent(int p_bone);
  140. private:
  141. /// This is a slow API os it's cached
  142. PhysicalBone *_get_physical_bone_parent(int p_bone);
  143. void _rebuild_physical_bones_cache();
  144. public:
  145. void physical_bones_stop_simulation();
  146. void physical_bones_start_simulation_on(const Array &p_bones);
  147. void physical_bones_add_collision_exception(RID p_exception);
  148. void physical_bones_remove_collision_exception(RID p_exception);
  149. #endif // _3D_DISABLED
  150. public:
  151. Skeleton();
  152. ~Skeleton();
  153. };
  154. #endif