skeleton_3d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "core/templates/a_hash_map.h"
  33. #include "scene/3d/node_3d.h"
  34. #include "scene/resources/3d/skin.h"
  35. typedef int BoneId;
  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. #ifdef TOOLS_ENABLED
  59. bool saving = false;
  60. #endif //TOOLS_ENABLED
  61. #ifndef DISABLE_DEPRECATED
  62. bool animate_physical_bones = true;
  63. Node *simulator = nullptr;
  64. void setup_simulator();
  65. #endif // _DISABLE_DEPRECATED
  66. public:
  67. enum ModifierCallbackModeProcess {
  68. MODIFIER_CALLBACK_MODE_PROCESS_PHYSICS,
  69. MODIFIER_CALLBACK_MODE_PROCESS_IDLE,
  70. };
  71. private:
  72. friend class SkinReference;
  73. enum UpdateFlag {
  74. UPDATE_FLAG_NONE = 1,
  75. UPDATE_FLAG_MODIFIER = 2,
  76. UPDATE_FLAG_POSE = 4,
  77. };
  78. void _update_deferred(UpdateFlag p_update_flag = UPDATE_FLAG_POSE);
  79. uint8_t update_flags = UPDATE_FLAG_NONE;
  80. bool updating = false; // Is updating now?
  81. struct Bone {
  82. String name;
  83. int parent = -1;
  84. Vector<int> child_bones;
  85. Transform3D rest;
  86. Transform3D global_rest;
  87. bool enabled = true;
  88. bool pose_cache_dirty = true;
  89. Transform3D pose_cache;
  90. Vector3 pose_position;
  91. Quaternion pose_rotation;
  92. Vector3 pose_scale = Vector3(1, 1, 1);
  93. Transform3D global_pose;
  94. int nested_set_offset = 0; // Offset in nested set of bone hierarchy.
  95. int nested_set_span = 0; // Subtree span in nested set of bone hierarchy.
  96. void update_pose_cache() {
  97. if (pose_cache_dirty) {
  98. pose_cache.basis.set_quaternion_scale(pose_rotation, pose_scale);
  99. pose_cache.origin = pose_position;
  100. pose_cache_dirty = false;
  101. }
  102. }
  103. HashMap<StringName, Variant> metadata;
  104. #ifndef DISABLE_DEPRECATED
  105. Transform3D pose_global_no_override;
  106. real_t global_pose_override_amount = 0.0;
  107. bool global_pose_override_reset = false;
  108. Transform3D global_pose_override;
  109. #endif // _DISABLE_DEPRECATED
  110. };
  111. struct BonePoseBackup {
  112. Transform3D pose_cache;
  113. Vector3 pose_position;
  114. Quaternion pose_rotation;
  115. Vector3 pose_scale = Vector3(1, 1, 1);
  116. Transform3D global_pose;
  117. void save(const Bone &p_bone) {
  118. pose_cache = p_bone.pose_cache;
  119. pose_position = p_bone.pose_position;
  120. pose_rotation = p_bone.pose_rotation;
  121. pose_scale = p_bone.pose_scale;
  122. global_pose = p_bone.global_pose;
  123. }
  124. void restore(Bone &r_bone) {
  125. r_bone.pose_cache = pose_cache;
  126. r_bone.pose_position = pose_position;
  127. r_bone.pose_rotation = pose_rotation;
  128. r_bone.pose_scale = pose_scale;
  129. r_bone.global_pose = global_pose;
  130. }
  131. };
  132. HashSet<SkinReference *> skin_bindings;
  133. void _skin_changed();
  134. mutable LocalVector<Bone> bones;
  135. mutable bool process_order_dirty = false;
  136. mutable Vector<int> parentless_bones;
  137. AHashMap<String, int> name_to_bone_index;
  138. mutable StringName concatenated_bone_names;
  139. void _update_bone_names() const;
  140. void _make_dirty();
  141. mutable bool dirty = false;
  142. mutable bool rest_dirty = false;
  143. bool show_rest_only = false;
  144. float motion_scale = 1.0;
  145. uint64_t version = 1;
  146. void _update_process_order() const;
  147. // To process modifiers.
  148. ModifierCallbackModeProcess modifier_callback_mode_process = MODIFIER_CALLBACK_MODE_PROCESS_IDLE;
  149. LocalVector<ObjectID> modifiers;
  150. bool modifiers_dirty = false;
  151. void _find_modifiers();
  152. void _process_modifiers();
  153. void _process_changed();
  154. void _make_modifiers_dirty();
  155. // Global bone pose calculation.
  156. mutable LocalVector<int> nested_set_offset_to_bone_index; // Map from Bone::nested_set_offset to bone index.
  157. mutable LocalVector<bool> bone_global_pose_dirty; // Indexable with Bone::nested_set_offset.
  158. void _update_bones_nested_set() const;
  159. int _update_bone_nested_set(int p_bone, int p_offset) const;
  160. void _make_bone_global_poses_dirty() const;
  161. void _make_bone_global_pose_subtree_dirty(int p_bone) const;
  162. void _update_bone_global_pose(int p_bone) const;
  163. #ifndef DISABLE_DEPRECATED
  164. void _add_bone_bind_compat_88791(const String &p_name);
  165. static void _bind_compatibility_methods();
  166. #endif // DISABLE_DEPRECATED
  167. protected:
  168. bool _get(const StringName &p_path, Variant &r_ret) const;
  169. bool _set(const StringName &p_path, const Variant &p_value);
  170. void _get_property_list(List<PropertyInfo> *p_list) const;
  171. void _validate_property(PropertyInfo &p_property) const;
  172. void _notification(int p_what);
  173. TypedArray<StringName> _get_bone_meta_list_bind(int p_bone) const;
  174. static void _bind_methods();
  175. virtual void add_child_notify(Node *p_child) override;
  176. virtual void move_child_notify(Node *p_child) override;
  177. virtual void remove_child_notify(Node *p_child) override;
  178. public:
  179. enum {
  180. NOTIFICATION_UPDATE_SKELETON = 50
  181. };
  182. // Skeleton creation API
  183. uint64_t get_version() const;
  184. int add_bone(const String &p_name);
  185. void remove_bone(int p_bone);
  186. int find_bone(const String &p_name) const;
  187. String get_bone_name(int p_bone) const;
  188. void set_bone_name(int p_bone, const String &p_name);
  189. StringName get_concatenated_bone_names() const;
  190. bool is_bone_parent_of(int p_bone_id, int p_parent_bone_id) const;
  191. void set_bone_parent(int p_bone, int p_parent);
  192. int get_bone_parent(int p_bone) const;
  193. void unparent_bone_and_rest(int p_bone);
  194. Vector<int> get_bone_children(int p_bone) const;
  195. Vector<int> get_parentless_bones() const;
  196. int get_bone_count() const;
  197. void set_bone_rest(int p_bone, const Transform3D &p_rest);
  198. Transform3D get_bone_rest(int p_bone) const;
  199. Transform3D get_bone_global_rest(int p_bone) const;
  200. void set_bone_enabled(int p_bone, bool p_enabled);
  201. bool is_bone_enabled(int p_bone) const;
  202. void set_show_rest_only(bool p_enabled);
  203. bool is_show_rest_only() const;
  204. void clear_bones();
  205. void set_motion_scale(float p_motion_scale);
  206. float get_motion_scale() const;
  207. // bone metadata
  208. Variant get_bone_meta(int p_bone, const StringName &p_key) const;
  209. void get_bone_meta_list(int p_bone, List<StringName> *p_list) const;
  210. bool has_bone_meta(int p_bone, const StringName &p_key) const;
  211. void set_bone_meta(int p_bone, const StringName &p_key, const Variant &p_value);
  212. // Posing API
  213. Transform3D get_bone_pose(int p_bone) const;
  214. Vector3 get_bone_pose_position(int p_bone) const;
  215. Quaternion get_bone_pose_rotation(int p_bone) const;
  216. Vector3 get_bone_pose_scale(int p_bone) const;
  217. void set_bone_pose(int p_bone, const Transform3D &p_pose);
  218. void set_bone_pose_position(int p_bone, const Vector3 &p_position);
  219. void set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation);
  220. void set_bone_pose_scale(int p_bone, const Vector3 &p_scale);
  221. Transform3D get_bone_global_pose(int p_bone) const;
  222. void set_bone_global_pose(int p_bone, const Transform3D &p_pose);
  223. void reset_bone_pose(int p_bone);
  224. void reset_bone_poses();
  225. void localize_rests(); // Used for loaders and tools.
  226. Ref<Skin> create_skin_from_rest_transforms();
  227. Ref<SkinReference> register_skin(const Ref<Skin> &p_skin);
  228. void force_update_all_dirty_bones();
  229. void _force_update_all_dirty_bones() const;
  230. void force_update_all_bone_transforms();
  231. void _force_update_all_bone_transforms() const;
  232. void force_update_bone_children_transforms(int bone_idx);
  233. void _force_update_bone_children_transforms(int bone_idx) const;
  234. void force_update_deferred();
  235. void set_modifier_callback_mode_process(ModifierCallbackModeProcess p_mode);
  236. ModifierCallbackModeProcess get_modifier_callback_mode_process() const;
  237. #ifndef DISABLE_DEPRECATED
  238. Transform3D get_bone_global_pose_no_override(int p_bone) const;
  239. void clear_bones_global_pose_override();
  240. Transform3D get_bone_global_pose_override(int p_bone) const;
  241. void set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent = false);
  242. Node *get_simulator();
  243. void set_animate_physical_bones(bool p_enabled);
  244. bool get_animate_physical_bones() const;
  245. void physical_bones_stop_simulation();
  246. void physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones);
  247. void physical_bones_add_collision_exception(RID p_exception);
  248. void physical_bones_remove_collision_exception(RID p_exception);
  249. #endif // _DISABLE_DEPRECATED
  250. public:
  251. Skeleton3D();
  252. ~Skeleton3D();
  253. };
  254. VARIANT_ENUM_CAST(Skeleton3D::ModifierCallbackModeProcess);
  255. #endif // SKELETON_3D_H