skeleton_3d.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**************************************************************************/
  2. /* skeleton_3d.h */
  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. #ifndef SKELETON_3D_H
  31. #define SKELETON_3D_H
  32. #include "scene/3d/node_3d.h"
  33. #include "scene/resources/skin.h"
  34. typedef int BoneId;
  35. class PhysicalBone3D;
  36. class Skeleton3D;
  37. class SkinReference : public RefCounted {
  38. GDCLASS(SkinReference, RefCounted)
  39. friend class Skeleton3D;
  40. Skeleton3D *skeleton_node = nullptr;
  41. RID skeleton;
  42. Ref<Skin> skin;
  43. uint32_t bind_count = 0;
  44. uint64_t skeleton_version = 0;
  45. Vector<uint32_t> skin_bone_indices;
  46. uint32_t *skin_bone_indices_ptrs = nullptr;
  47. protected:
  48. static void _bind_methods();
  49. public:
  50. // Public for use with callable_mp.
  51. void _skin_changed();
  52. RID get_skeleton() const;
  53. Ref<Skin> get_skin() const;
  54. ~SkinReference();
  55. };
  56. class Skeleton3D : public Node3D {
  57. GDCLASS(Skeleton3D, Node3D);
  58. private:
  59. friend class SkinReference;
  60. struct Bone {
  61. String name;
  62. bool enabled;
  63. int parent;
  64. Transform3D rest;
  65. Transform3D global_rest;
  66. _FORCE_INLINE_ void update_pose_cache() {
  67. if (pose_cache_dirty) {
  68. pose_cache.basis.set_quaternion_scale(pose_rotation, pose_scale);
  69. pose_cache.origin = pose_position;
  70. pose_cache_dirty = false;
  71. }
  72. }
  73. bool pose_cache_dirty = true;
  74. Transform3D pose_cache;
  75. Vector3 pose_position;
  76. Quaternion pose_rotation;
  77. Vector3 pose_scale = Vector3(1, 1, 1);
  78. Transform3D pose_global;
  79. Transform3D pose_global_no_override;
  80. real_t global_pose_override_amount = 0.0;
  81. bool global_pose_override_reset = false;
  82. Transform3D global_pose_override;
  83. PhysicalBone3D *physical_bone = nullptr;
  84. PhysicalBone3D *cache_parent_physical_bone = nullptr;
  85. Vector<int> child_bones;
  86. Bone() {
  87. parent = -1;
  88. enabled = true;
  89. global_pose_override_amount = 0;
  90. global_pose_override_reset = false;
  91. #ifndef _3D_DISABLED
  92. physical_bone = nullptr;
  93. cache_parent_physical_bone = nullptr;
  94. #endif // _3D_DISABLED
  95. child_bones = Vector<int>();
  96. }
  97. };
  98. HashSet<SkinReference *> skin_bindings;
  99. void _skin_changed();
  100. bool animate_physical_bones = true;
  101. Vector<Bone> bones;
  102. bool process_order_dirty = false;
  103. Vector<int> parentless_bones;
  104. HashMap<String, int> name_to_bone_index;
  105. void _make_dirty();
  106. bool dirty = false;
  107. bool rest_dirty = false;
  108. bool show_rest_only = false;
  109. float motion_scale = 1.0;
  110. uint64_t version = 1;
  111. void _update_process_order();
  112. protected:
  113. bool _get(const StringName &p_path, Variant &r_ret) const;
  114. bool _set(const StringName &p_path, const Variant &p_value);
  115. void _get_property_list(List<PropertyInfo> *p_list) const;
  116. void _validate_property(PropertyInfo &p_property) 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. uint64_t get_version() const;
  125. void add_bone(const String &p_name);
  126. int find_bone(const String &p_name) const;
  127. String get_bone_name(int p_bone) const;
  128. void set_bone_name(int p_bone, const String &p_name);
  129. bool is_bone_parent_of(int p_bone_id, int p_parent_bone_id) const;
  130. void set_bone_parent(int p_bone, int p_parent);
  131. int get_bone_parent(int p_bone) const;
  132. void unparent_bone_and_rest(int p_bone);
  133. Vector<int> get_bone_children(int p_bone) const;
  134. Vector<int> get_parentless_bones() const;
  135. int get_bone_count() const;
  136. void set_bone_rest(int p_bone, const Transform3D &p_rest);
  137. Transform3D get_bone_rest(int p_bone) const;
  138. Transform3D get_bone_global_rest(int p_bone) const;
  139. Transform3D get_bone_global_pose(int p_bone) const;
  140. Transform3D get_bone_global_pose_no_override(int p_bone) const;
  141. void set_bone_enabled(int p_bone, bool p_enabled);
  142. bool is_bone_enabled(int p_bone) const;
  143. void set_show_rest_only(bool p_enabled);
  144. bool is_show_rest_only() const;
  145. void clear_bones();
  146. void set_motion_scale(float p_motion_scale);
  147. float get_motion_scale() const;
  148. // posing api
  149. void set_bone_pose_position(int p_bone, const Vector3 &p_position);
  150. void set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation);
  151. void set_bone_pose_scale(int p_bone, const Vector3 &p_scale);
  152. Transform3D get_bone_pose(int p_bone) const;
  153. Vector3 get_bone_pose_position(int p_bone) const;
  154. Quaternion get_bone_pose_rotation(int p_bone) const;
  155. Vector3 get_bone_pose_scale(int p_bone) const;
  156. void reset_bone_pose(int p_bone);
  157. void reset_bone_poses();
  158. void clear_bones_global_pose_override();
  159. Transform3D get_bone_global_pose_override(int p_bone) const;
  160. void set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent = false);
  161. void localize_rests(); // used for loaders and tools
  162. Ref<Skin> create_skin_from_rest_transforms();
  163. Ref<SkinReference> register_skin(const Ref<Skin> &p_skin);
  164. void force_update_all_dirty_bones();
  165. void force_update_all_bone_transforms();
  166. void force_update_bone_children_transforms(int bone_idx);
  167. // Physical bone API
  168. void set_animate_physical_bones(bool p_enabled);
  169. bool get_animate_physical_bones() const;
  170. void bind_physical_bone_to_bone(int p_bone, PhysicalBone3D *p_physical_bone);
  171. void unbind_physical_bone_from_bone(int p_bone);
  172. PhysicalBone3D *get_physical_bone(int p_bone);
  173. PhysicalBone3D *get_physical_bone_parent(int p_bone);
  174. private:
  175. /// This is a slow API, so it's cached
  176. PhysicalBone3D *_get_physical_bone_parent(int p_bone);
  177. void _rebuild_physical_bones_cache();
  178. public:
  179. void physical_bones_stop_simulation();
  180. void physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones);
  181. void physical_bones_add_collision_exception(RID p_exception);
  182. void physical_bones_remove_collision_exception(RID p_exception);
  183. public:
  184. Skeleton3D();
  185. ~Skeleton3D();
  186. };
  187. #endif // SKELETON_3D_H