skeleton_3d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #pragma once
  31. #include "core/templates/a_hash_map.h"
  32. #include "scene/3d/node_3d.h"
  33. #include "scene/resources/3d/skin.h"
  34. typedef int BoneId;
  35. class Skeleton3D;
  36. class SkinReference : public RefCounted {
  37. GDCLASS(SkinReference, RefCounted)
  38. friend class Skeleton3D;
  39. Skeleton3D *skeleton_node = nullptr;
  40. RID skeleton;
  41. Ref<Skin> skin;
  42. uint32_t bind_count = 0;
  43. uint64_t skeleton_version = 0;
  44. Vector<uint32_t> skin_bone_indices;
  45. uint32_t *skin_bone_indices_ptrs = nullptr;
  46. protected:
  47. static void _bind_methods();
  48. public:
  49. // Public for use with callable_mp.
  50. void _skin_changed();
  51. RID get_skeleton() const;
  52. Ref<Skin> get_skin() const;
  53. ~SkinReference();
  54. };
  55. class Skeleton3D : public Node3D {
  56. GDCLASS(Skeleton3D, Node3D);
  57. #ifdef TOOLS_ENABLED
  58. bool saving = false;
  59. #endif //TOOLS_ENABLED
  60. #if !defined(DISABLE_DEPRECATED) && !defined(PHYSICS_3D_DISABLED)
  61. bool animate_physical_bones = true;
  62. Node *simulator = nullptr;
  63. void setup_simulator();
  64. #endif // _DISABLE_DEPRECATED && PHYSICS_3D_DISABLED
  65. public:
  66. enum ModifierCallbackModeProcess {
  67. MODIFIER_CALLBACK_MODE_PROCESS_PHYSICS,
  68. MODIFIER_CALLBACK_MODE_PROCESS_IDLE,
  69. MODIFIER_CALLBACK_MODE_PROCESS_MANUAL,
  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. double update_delta = 0.0;
  82. struct Bone {
  83. String name;
  84. int parent = -1;
  85. Vector<int> child_bones;
  86. Transform3D rest;
  87. Transform3D global_rest;
  88. bool enabled = true;
  89. bool pose_cache_dirty = true;
  90. Transform3D pose_cache;
  91. Vector3 pose_position;
  92. Quaternion pose_rotation;
  93. Vector3 pose_scale = Vector3(1, 1, 1);
  94. Transform3D global_pose;
  95. int nested_set_offset = 0; // Offset in nested set of bone hierarchy.
  96. int nested_set_span = 0; // Subtree span in nested set of bone hierarchy.
  97. void update_pose_cache() {
  98. if (pose_cache_dirty) {
  99. pose_cache.basis.set_quaternion_scale(pose_rotation, pose_scale);
  100. pose_cache.origin = pose_position;
  101. pose_cache_dirty = false;
  102. }
  103. }
  104. HashMap<StringName, Variant> metadata;
  105. #ifndef DISABLE_DEPRECATED
  106. Transform3D pose_global_no_override;
  107. real_t global_pose_override_amount = 0.0;
  108. bool global_pose_override_reset = false;
  109. Transform3D global_pose_override;
  110. #endif // _DISABLE_DEPRECATED
  111. };
  112. struct BonePoseBackup {
  113. Transform3D pose_cache;
  114. Vector3 pose_position;
  115. Quaternion pose_rotation;
  116. Vector3 pose_scale = Vector3(1, 1, 1);
  117. Transform3D global_pose;
  118. void save(const Bone &p_bone) {
  119. pose_cache = p_bone.pose_cache;
  120. pose_position = p_bone.pose_position;
  121. pose_rotation = p_bone.pose_rotation;
  122. pose_scale = p_bone.pose_scale;
  123. global_pose = p_bone.global_pose;
  124. }
  125. void restore(Bone &r_bone) {
  126. r_bone.pose_cache = pose_cache;
  127. r_bone.pose_position = pose_position;
  128. r_bone.pose_rotation = pose_rotation;
  129. r_bone.pose_scale = pose_scale;
  130. r_bone.global_pose = global_pose;
  131. }
  132. };
  133. HashSet<SkinReference *> skin_bindings;
  134. void _skin_changed();
  135. mutable LocalVector<Bone> bones;
  136. mutable bool process_order_dirty = false;
  137. mutable Vector<int> parentless_bones;
  138. AHashMap<String, int> name_to_bone_index;
  139. mutable StringName concatenated_bone_names;
  140. void _update_bone_names() const;
  141. void _make_dirty();
  142. mutable bool dirty = false;
  143. mutable bool rest_dirty = false;
  144. bool show_rest_only = false;
  145. float motion_scale = 1.0;
  146. uint64_t version = 1;
  147. void _update_process_order() const;
  148. // To process modifiers.
  149. ModifierCallbackModeProcess modifier_callback_mode_process = MODIFIER_CALLBACK_MODE_PROCESS_IDLE;
  150. LocalVector<ObjectID> modifiers;
  151. bool modifiers_dirty = false;
  152. void _find_modifiers();
  153. void _process_modifiers();
  154. void _process_changed();
  155. void _make_modifiers_dirty();
  156. // Global bone pose calculation.
  157. mutable LocalVector<int> nested_set_offset_to_bone_index; // Map from Bone::nested_set_offset to bone index.
  158. mutable LocalVector<bool> bone_global_pose_dirty; // Indexable with Bone::nested_set_offset.
  159. void _update_bones_nested_set() const;
  160. int _update_bone_nested_set(int p_bone, int p_offset) const;
  161. void _make_bone_global_poses_dirty() const;
  162. void _make_bone_global_pose_subtree_dirty(int p_bone) const;
  163. void _update_bone_global_pose(int p_bone) const;
  164. #ifndef DISABLE_DEPRECATED
  165. void _add_bone_bind_compat_88791(const String &p_name);
  166. static void _bind_compatibility_methods();
  167. #endif // DISABLE_DEPRECATED
  168. protected:
  169. bool _get(const StringName &p_path, Variant &r_ret) const;
  170. bool _set(const StringName &p_path, const Variant &p_value);
  171. void _get_property_list(List<PropertyInfo> *p_list) 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. void advance(double p_delta);
  238. #ifndef DISABLE_DEPRECATED
  239. Transform3D get_bone_global_pose_no_override(int p_bone) const;
  240. void clear_bones_global_pose_override();
  241. Transform3D get_bone_global_pose_override(int p_bone) const;
  242. void set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent = false);
  243. Node *get_simulator();
  244. #ifndef PHYSICS_3D_DISABLED
  245. void set_animate_physical_bones(bool p_enabled);
  246. bool get_animate_physical_bones() const;
  247. void physical_bones_stop_simulation();
  248. void physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones);
  249. void physical_bones_add_collision_exception(RID p_exception);
  250. void physical_bones_remove_collision_exception(RID p_exception);
  251. #endif // PHYSICS_3D_DISABLED
  252. #endif // _DISABLE_DEPRECATED
  253. public:
  254. Skeleton3D();
  255. ~Skeleton3D();
  256. };
  257. VARIANT_ENUM_CAST(Skeleton3D::ModifierCallbackModeProcess);