| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/a_hash_map.h" |
| 34 |
#include "scene/3d/node_3d.h" |
| 35 |
#include "scene/resources/3d/skin.h" |
| 36 |
|
| 37 |
typedef int BoneId; |
| 38 |
|
| 39 |
class Skeleton3D; |
| 40 |
|
| 41 |
class SkinReference : public RefCounted { |
| 42 |
GDCLASS(SkinReference, RefCounted) |
| 43 |
friend class Skeleton3D; |
| 44 |
|
| 45 |
Skeleton3D *skeleton_node = nullptr; |
| 46 |
RID skeleton; |
| 47 |
Ref<Skin> skin; |
| 48 |
uint32_t bind_count = 0; |
| 49 |
uint64_t skeleton_version = 0; |
| 50 |
Vector<uint32_t> skin_bone_indices; |
| 51 |
uint32_t *skin_bone_indices_ptrs = nullptr; |
| 52 |
|
| 53 |
protected: |
| 54 |
static void _bind_methods(); |
| 55 |
|
| 56 |
public: |
| 57 |
|
| 58 |
void _skin_changed(); |
| 59 |
|
| 60 |
RID get_skeleton() const; |
| 61 |
Ref<Skin> get_skin() const; |
| 62 |
~SkinReference(); |
| 63 |
}; |
| 64 |
|
| 65 |
class Skeleton3D : public Node3D { |
| 66 |
GDCLASS(Skeleton3D, Node3D); |
| 67 |
|
| 68 |
#ifdef TOOLS_ENABLED |
| 69 |
bool saving = false; |
| 70 |
#endif |
| 71 |
|
| 72 |
#if !defined(DISABLE_DEPRECATED) && !defined(PHYSICS_3D_DISABLED) |
| 73 |
bool animate_physical_bones = true; |
| 74 |
Node *simulator = nullptr; |
| 75 |
void setup_simulator(); |
| 76 |
#endif |
| 77 |
|
| 78 |
public: |
| 79 |
enum ModifierCallbackModeProcess { |
| 80 |
MODIFIER_CALLBACK_MODE_PROCESS_PHYSICS, |
| 81 |
MODIFIER_CALLBACK_MODE_PROCESS_IDLE, |
| 82 |
MODIFIER_CALLBACK_MODE_PROCESS_MANUAL, |
| 83 |
}; |
| 84 |
|
| 85 |
private: |
| 86 |
friend class SkinReference; |
| 87 |
|
| 88 |
enum UpdateFlag { |
| 89 |
UPDATE_FLAG_NONE = 1, |
| 90 |
UPDATE_FLAG_MODIFIER = 2, |
| 91 |
UPDATE_FLAG_POSE = 4, |
| 92 |
}; |
| 93 |
|
| 94 |
void _update_deferred(UpdateFlag p_update_flag = UPDATE_FLAG_POSE); |
| 95 |
uint8_t update_flags = UPDATE_FLAG_NONE; |
| 96 |
bool updating = false; |
| 97 |
double update_delta = 0.0; |
| 98 |
|
| 99 |
struct Bone { |
| 100 |
String name; |
| 101 |
|
| 102 |
int parent = -1; |
| 103 |
Vector<int> child_bones; |
| 104 |
|
| 105 |
Transform3D rest; |
| 106 |
Transform3D global_rest; |
| 107 |
|
| 108 |
bool enabled = true; |
| 109 |
bool pose_cache_dirty = true; |
| 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 |
int nested_set_offset = 0; |
| 116 |
int nested_set_span = 0; |
| 117 |
|
| 118 |
void update_pose_cache() { |
| 119 |
if (pose_cache_dirty) { |
| 120 |
pose_cache.basis.set_quaternion_scale(pose_rotation, pose_scale); |
| 121 |
pose_cache.origin = pose_position; |
| 122 |
pose_cache_dirty = false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
HashMap<StringName, Variant> metadata; |
| 127 |
|
| 128 |
#ifndef DISABLE_DEPRECATED |
| 129 |
Transform3D pose_global_no_override; |
| 130 |
real_t global_pose_override_amount = 0.0; |
| 131 |
bool global_pose_override_reset = false; |
| 132 |
Transform3D global_pose_override; |
| 133 |
#endif |
| 134 |
}; |
| 135 |
|
| 136 |
struct BonePoseBackup { |
| 137 |
Transform3D pose_cache; |
| 138 |
Vector3 pose_position; |
| 139 |
Quaternion pose_rotation; |
| 140 |
Vector3 pose_scale = Vector3(1, 1, 1); |
| 141 |
Transform3D global_pose; |
| 142 |
|
| 143 |
void save(const Bone &p_bone) { |
| 144 |
pose_cache = p_bone.pose_cache; |
| 145 |
pose_position = p_bone.pose_position; |
| 146 |
pose_rotation = p_bone.pose_rotation; |
| 147 |
pose_scale = p_bone.pose_scale; |
| 148 |
global_pose = p_bone.global_pose; |
| 149 |
} |
| 150 |
|
| 151 |
void restore(Bone &r_bone) { |
| 152 |
r_bone.pose_cache = pose_cache; |
| 153 |
r_bone.pose_position = pose_position; |
| 154 |
r_bone.pose_rotation = pose_rotation; |
| 155 |
r_bone.pose_scale = pose_scale; |
| 156 |
r_bone.global_pose = global_pose; |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
HashSet<SkinReference *> skin_bindings; |
| 161 |
void _skin_changed(); |
| 162 |
|
| 163 |
mutable LocalVector<Bone> bones; |
| 164 |
mutable bool process_order_dirty = false; |
| 165 |
|
| 166 |
mutable Vector<int> parentless_bones; |
| 167 |
AHashMap<String, int> name_to_bone_index; |
| 168 |
|
| 169 |
mutable StringName concatenated_bone_names; |
| 170 |
void _update_bone_names() const; |
| 171 |
|
| 172 |
void _make_dirty(); |
| 173 |
mutable bool dirty = false; |
| 174 |
mutable bool rest_dirty = false; |
| 175 |
|
| 176 |
bool show_rest_only = false; |
| 177 |
float motion_scale = 1.0; |
| 178 |
|
| 179 |
uint64_t version = 1; |
| 180 |
|
| 181 |
void _update_process_order() const; |
| 182 |
|
| 183 |
|
| 184 |
ModifierCallbackModeProcess modifier_callback_mode_process = MODIFIER_CALLBACK_MODE_PROCESS_IDLE; |
| 185 |
LocalVector<ObjectID> modifiers; |
| 186 |
bool modifiers_dirty = false; |
| 187 |
void _find_modifiers(); |
| 188 |
void _process_modifiers(); |
| 189 |
void _process_changed(); |
| 190 |
void _make_modifiers_dirty(); |
| 191 |
|
| 192 |
|
| 193 |
mutable LocalVector<int> nested_set_offset_to_bone_index; |
| 194 |
mutable LocalVector<bool> bone_global_pose_dirty; |
| 195 |
void _update_bones_nested_set() const; |
| 196 |
int _update_bone_nested_set(int p_bone, int p_offset) const; |
| 197 |
void _make_bone_global_poses_dirty() const; |
| 198 |
void _make_bone_global_pose_subtree_dirty(int p_bone) const; |
| 199 |
void _update_bone_global_pose(int p_bone) const; |
| 200 |
|
| 201 |
#ifndef DISABLE_DEPRECATED |
| 202 |
void _add_bone_bind_compat_88791(const String &p_name); |
| 203 |
|
| 204 |
static void _bind_compatibility_methods(); |
| 205 |
#endif |
| 206 |
|
| 207 |
protected: |
| 208 |
bool _get(const StringName &p_path, Variant &r_ret) const; |
| 209 |
bool _set(const StringName &p_path, const Variant &p_value); |
| 210 |
void _get_property_list(List<PropertyInfo> *p_list) const; |
| 211 |
void _notification(int p_what); |
| 212 |
TypedArray<StringName> _get_bone_meta_list_bind(int p_bone) const; |
| 213 |
static void _bind_methods(); |
| 214 |
|
| 215 |
virtual void add_child_notify(Node *p_child) override; |
| 216 |
virtual void move_child_notify(Node *p_child) override; |
| 217 |
virtual void remove_child_notify(Node *p_child) override; |
| 218 |
|
| 219 |
public: |
| 220 |
enum { |
| 221 |
NOTIFICATION_UPDATE_SKELETON = 50 |
| 222 |
}; |
| 223 |
|
| 224 |
|
| 225 |
uint64_t get_version() const; |
| 226 |
int add_bone(const String &p_name); |
| 227 |
int find_bone(const String &p_name) const; |
| 228 |
String get_bone_name(int p_bone) const; |
| 229 |
void set_bone_name(int p_bone, const String &p_name); |
| 230 |
StringName get_concatenated_bone_names() const; |
| 231 |
|
| 232 |
bool is_bone_parent_of(int p_bone_id, int p_parent_bone_id) const; |
| 233 |
|
| 234 |
void set_bone_parent(int p_bone, int p_parent); |
| 235 |
int get_bone_parent(int p_bone) const; |
| 236 |
|
| 237 |
void unparent_bone_and_rest(int p_bone); |
| 238 |
|
| 239 |
Vector<int> get_bone_children(int p_bone) const; |
| 240 |
Vector<int> get_parentless_bones() const; |
| 241 |
|
| 242 |
int get_bone_count() const; |
| 243 |
|
| 244 |
void set_bone_rest(int p_bone, const Transform3D &p_rest); |
| 245 |
Transform3D get_bone_rest(int p_bone) const; |
| 246 |
Transform3D get_bone_global_rest(int p_bone) const; |
| 247 |
|
| 248 |
void set_bone_enabled(int p_bone, bool p_enabled); |
| 249 |
bool is_bone_enabled(int p_bone) const; |
| 250 |
|
| 251 |
void set_show_rest_only(bool p_enabled); |
| 252 |
bool is_show_rest_only() const; |
| 253 |
void clear_bones(); |
| 254 |
|
| 255 |
void set_motion_scale(float p_motion_scale); |
| 256 |
float get_motion_scale() const; |
| 257 |
|
| 258 |
|
| 259 |
Variant get_bone_meta(int p_bone, const StringName &p_key) const; |
| 260 |
void get_bone_meta_list(int p_bone, List<StringName> *p_list) const; |
| 261 |
bool has_bone_meta(int p_bone, const StringName &p_key) const; |
| 262 |
void set_bone_meta(int p_bone, const StringName &p_key, const Variant &p_value); |
| 263 |
|
| 264 |
|
| 265 |
Transform3D get_bone_pose(int p_bone) const; |
| 266 |
Vector3 get_bone_pose_position(int p_bone) const; |
| 267 |
Quaternion get_bone_pose_rotation(int p_bone) const; |
| 268 |
Vector3 get_bone_pose_scale(int p_bone) const; |
| 269 |
void set_bone_pose(int p_bone, const Transform3D &p_pose); |
| 270 |
void set_bone_pose_position(int p_bone, const Vector3 &p_position); |
| 271 |
void set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation); |
| 272 |
void set_bone_pose_scale(int p_bone, const Vector3 &p_scale); |
| 273 |
|
| 274 |
Transform3D get_bone_global_pose(int p_bone) const; |
| 275 |
void set_bone_global_pose(int p_bone, const Transform3D &p_pose); |
| 276 |
|
| 277 |
void reset_bone_pose(int p_bone); |
| 278 |
void reset_bone_poses(); |
| 279 |
|
| 280 |
void localize_rests(); |
| 281 |
|
| 282 |
Ref<Skin> create_skin_from_rest_transforms(); |
| 283 |
|
| 284 |
Ref<SkinReference> register_skin(const Ref<Skin> &p_skin); |
| 285 |
|
| 286 |
void force_update_all_dirty_bones(); |
| 287 |
void _force_update_all_dirty_bones() const; |
| 288 |
void force_update_all_bone_transforms(); |
| 289 |
void _force_update_all_bone_transforms() const; |
| 290 |
void force_update_bone_children_transforms(int bone_idx); |
| 291 |
void _force_update_bone_children_transforms(int bone_idx) const; |
| 292 |
void force_update_deferred(); |
| 293 |
|
| 294 |
void set_modifier_callback_mode_process(ModifierCallbackModeProcess p_mode); |
| 295 |
ModifierCallbackModeProcess get_modifier_callback_mode_process() const; |
| 296 |
|
| 297 |
void advance(double p_delta); |
| 298 |
|
| 299 |
#ifndef DISABLE_DEPRECATED |
| 300 |
Transform3D get_bone_global_pose_no_override(int p_bone) const; |
| 301 |
void clear_bones_global_pose_override(); |
| 302 |
Transform3D get_bone_global_pose_override(int p_bone) const; |
| 303 |
void set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent = false); |
| 304 |
|
| 305 |
Node *get_simulator(); |
| 306 |
#ifndef PHYSICS_3D_DISABLED |
| 307 |
void set_animate_physical_bones(bool p_enabled); |
| 308 |
bool get_animate_physical_bones() const; |
| 309 |
void physical_bones_stop_simulation(); |
| 310 |
void physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones); |
| 311 |
void physical_bones_add_collision_exception(RID p_exception); |
| 312 |
void physical_bones_remove_collision_exception(RID p_exception); |
| 313 |
#endif |
| 314 |
#endif |
| 315 |
|
| 316 |
public: |
| 317 |
Skeleton3D(); |
| 318 |
~Skeleton3D(); |
| 319 |
}; |
| 320 |
|
| 321 |
VARIANT_ENUM_CAST(Skeleton3D::ModifierCallbackModeProcess); |