skeleton_3d.h 10 KB

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