skeleton.h 6.7 KB

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