Skip to content
0.2.0-dev Download ZIP
340 lines ·11.4 KB Raw
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
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
37typedef int BoneId;
38
39class Skeleton3D;
40
41class 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
53protected:
54 static void _bind_methods();
55
56public:
57 // Public for use as signal callback.
58 void _skin_changed();
59
60 RID get_skeleton() const;
61 Ref<Skin> get_skin() const;
62 ~SkinReference();
63};
64
65class Skeleton3D : public Node3D {
66 GDCLASS(Skeleton3D, Node3D);
67
68 const Vector3 DEFAULT_SKIN_SCALE = Vector3(1, 1, 1);
69
70#ifdef TOOLS_ENABLED
71 bool saving = false;
72#endif // TOOLS_ENABLED
73
74#if !defined(DISABLE_DEPRECATED) && !defined(PHYSICS_3D_DISABLED)
75 bool animate_physical_bones = true;
76 Node *simulator = nullptr;
77 void setup_simulator();
78#endif // _DISABLE_DEPRECATED && PHYSICS_3D_DISABLED
79
80public:
81 enum ModifierCallbackModeProcess {
82 MODIFIER_CALLBACK_MODE_PROCESS_PHYSICS,
83 MODIFIER_CALLBACK_MODE_PROCESS_IDLE,
84 MODIFIER_CALLBACK_MODE_PROCESS_MANUAL,
85 };
86
87private:
88 friend class SkinReference;
89
90 enum UpdateFlag {
91 UPDATE_FLAG_NONE = 1,
92 UPDATE_FLAG_MODIFIER = 2,
93 UPDATE_FLAG_POSE = 4,
94 };
95
96 void _update_deferred(UpdateFlag p_update_flag = UPDATE_FLAG_POSE);
97 uint8_t update_flags = UPDATE_FLAG_NONE;
98 bool updating = false; // Is updating now?
99 double update_delta = 0.0;
100
101 struct Bone {
102 String name;
103
104 int parent = -1;
105 Vector<int> child_bones;
106
107 Transform3D rest;
108 Transform3D global_rest;
109
110 bool enabled = true;
111 bool pose_cache_dirty = true;
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 Vector3 skin_scale = Vector3(1, 1, 1);
118 bool is_skin_scaled = false;
119 int nested_set_offset = 0; // Offset in nested set of bone hierarchy.
120 int nested_set_span = 0; // Subtree span in nested set of bone hierarchy.
121
122 bool modifier_applied = false;
123 Transform3D modifier_pose_cache;
124 void make_bone_modified() {
125 modifier_applied = true;
126 modifier_pose_cache = pose_cache;
127 }
128
129 void update_pose_cache() {
130 if (pose_cache_dirty) {
131 pose_cache.basis.set_quaternion_scale(pose_rotation, pose_scale);
132 pose_cache.origin = pose_position;
133 pose_cache_dirty = false;
134 }
135 }
136
137 HashMap<StringName, Variant> metadata;
138
139#ifndef DISABLE_DEPRECATED
140 Transform3D pose_global_no_override;
141 real_t global_pose_override_amount = 0.0;
142 bool global_pose_override_reset = false;
143 Transform3D global_pose_override;
144#endif // _DISABLE_DEPRECATED
145 };
146
147 struct BonePoseBackup {
148 Transform3D pose_cache;
149 Vector3 pose_position;
150 Quaternion pose_rotation;
151 Vector3 pose_scale = Vector3(1, 1, 1);
152 Transform3D global_pose;
153
154 void save(const Bone &p_bone) {
155 pose_cache = p_bone.pose_cache;
156 pose_position = p_bone.pose_position;
157 pose_rotation = p_bone.pose_rotation;
158 pose_scale = p_bone.pose_scale;
159 global_pose = p_bone.global_pose;
160 }
161
162 void restore(Bone &r_bone) {
163 r_bone.pose_cache = pose_cache;
164 r_bone.pose_position = pose_position;
165 r_bone.pose_rotation = pose_rotation;
166 r_bone.pose_scale = pose_scale;
167 r_bone.global_pose = global_pose;
168 }
169 };
170
171 HashSet<SkinReference *> skin_bindings;
172 void _skin_changed();
173
174 mutable LocalVector<Bone> bones;
175 mutable bool process_order_dirty = false;
176
177 mutable Vector<int> parentless_bones;
178 AHashMap<String, int> name_to_bone_index;
179
180 mutable StringName concatenated_bone_names;
181 void _update_bone_names() const;
182
183 void _make_dirty();
184 mutable bool dirty = false;
185 mutable bool rest_dirty = false;
186
187 bool show_rest_only = false;
188 float motion_scale = 1.0;
189
190 uint64_t version = 1;
191
192 void _update_process_order() const;
193
194 // To process modifiers.
195 ModifierCallbackModeProcess modifier_callback_mode_process = MODIFIER_CALLBACK_MODE_PROCESS_IDLE;
196 LocalVector<ObjectID> modifiers;
197 bool modifiers_dirty = false;
198 void _find_modifiers();
199 void _process_modifiers();
200 void _process_changed();
201 void _make_modifiers_dirty();
202 bool modifier_updating = false; // Is modifier updating now?
203
204 // Global bone pose calculation.
205 mutable LocalVector<int> nested_set_offset_to_bone_index; // Map from Bone::nested_set_offset to bone index.
206 mutable LocalVector<bool> bone_global_pose_dirty; // Indexable with Bone::nested_set_offset.
207 void _update_bones_nested_set() const;
208 int _update_bone_nested_set(int p_bone, int p_offset) const;
209 void _make_bone_global_poses_dirty() const;
210 void _make_bone_global_pose_subtree_dirty(int p_bone) const;
211 void _update_bone_global_pose(int p_bone) const;
212
213#ifndef DISABLE_DEPRECATED
214 void _add_bone_bind_compat_88791(const String &p_name);
215 void _reset_bone_pose_compat_120609(int p_bone);
216 void _reset_bone_poses_compat_120609();
217
218 static void _bind_compatibility_methods();
219#endif // DISABLE_DEPRECATED
220
221protected:
222 bool _get(const StringName &p_path, Variant &r_ret) const;
223 bool _set(const StringName &p_path, const Variant &p_value);
224 void _get_property_list(List<PropertyInfo> *p_list) const;
225 bool _property_can_revert(const StringName &p_name) const;
226 bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
227 void _notification(int p_what);
228 TypedArray<StringName> _get_bone_meta_list_bind(int p_bone) const;
229 static void _bind_methods();
230
231 virtual void add_child_notify(Node *p_child) override;
232 virtual void move_child_notify(Node *p_child) override;
233 virtual void remove_child_notify(Node *p_child) override;
234
235public:
236 enum {
237 NOTIFICATION_UPDATE_SKELETON = 50
238 };
239
240 // Skeleton creation API
241 uint64_t get_version() const;
242 int add_bone(const String &p_name);
243 int find_bone(const String &p_name) const;
244 String get_bone_name(int p_bone) const;
245 void set_bone_name(int p_bone, const String &p_name);
246 StringName get_concatenated_bone_names() const;
247
248 bool is_bone_parent_of(int p_bone_id, int p_parent_bone_id) const;
249
250 void set_bone_parent(int p_bone, int p_parent);
251 int get_bone_parent(int p_bone) const;
252
253 void unparent_bone_and_rest(int p_bone);
254
255 Vector<int> get_bone_children(int p_bone) const;
256 Vector<int> get_parentless_bones() const;
257
258 int get_bone_count() const;
259
260 void set_bone_rest(int p_bone, const Transform3D &p_rest);
261 Transform3D get_bone_rest(int p_bone) const;
262 Transform3D get_bone_global_rest(int p_bone) const;
263
264 void set_bone_enabled(int p_bone, bool p_enabled);
265 bool is_bone_enabled(int p_bone) const;
266
267 void set_show_rest_only(bool p_enabled);
268 bool is_show_rest_only() const;
269 void clear_bones();
270
271 void set_motion_scale(float p_motion_scale);
272 float get_motion_scale() const;
273
274 // bone metadata
275 Variant get_bone_meta(int p_bone, const StringName &p_key) const;
276 void get_bone_meta_list(int p_bone, List<StringName> *p_list) const;
277 bool has_bone_meta(int p_bone, const StringName &p_key) const;
278 void set_bone_meta(int p_bone, const StringName &p_key, const Variant &p_value);
279
280 // Posing API
281 Transform3D get_bone_pose(int p_bone) const;
282 Vector3 get_bone_pose_position(int p_bone) const;
283 Quaternion get_bone_pose_rotation(int p_bone) const;
284 Vector3 get_bone_pose_scale(int p_bone) const;
285 void set_bone_pose(int p_bone, const Transform3D &p_pose);
286 void set_bone_pose_position(int p_bone, const Vector3 &p_position);
287 void set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation);
288 void set_bone_pose_scale(int p_bone, const Vector3 &p_scale);
289
290 void set_bone_skin_scale(int p_bone, const Vector3 &p_skin_scale);
291 Vector3 get_bone_skin_scale(int p_bone) const;
292
293 Transform3D get_bone_global_pose(int p_bone) const;
294 void set_bone_global_pose(int p_bone, const Transform3D &p_pose);
295
296 void reset_bone_pose(int p_bone, bool p_reset_bone_skin_scale = false);
297 void reset_bone_poses(bool p_reset_bone_skin_scale = false);
298
299 void localize_rests(); // Used for loaders and tools.
300
301 Ref<Skin> create_skin_from_rest_transforms();
302
303 Ref<SkinReference> register_skin(const Ref<Skin> &p_skin);
304
305 void force_update_all_dirty_bones();
306 void _force_update_all_dirty_bones() const;
307 void force_update_all_bone_transforms();
308 void _force_update_all_bone_transforms() const;
309 void force_update_bone_children_transforms(int bone_idx);
310 void _force_update_bone_children_transforms(int bone_idx) const;
311 void force_update_deferred();
312
313 void set_modifier_callback_mode_process(ModifierCallbackModeProcess p_mode);
314 ModifierCallbackModeProcess get_modifier_callback_mode_process() const;
315
316 void advance(double p_delta);
317
318#ifndef DISABLE_DEPRECATED
319 Transform3D get_bone_global_pose_no_override(int p_bone) const;
320 void clear_bones_global_pose_override();
321 Transform3D get_bone_global_pose_override(int p_bone) const;
322 void set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent = false);
323
324 Node *get_simulator();
325#ifndef PHYSICS_3D_DISABLED
326 void set_animate_physical_bones(bool p_enabled);
327 bool get_animate_physical_bones() const;
328 void physical_bones_stop_simulation();
329 void physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones);
330 void physical_bones_add_collision_exception(RID p_exception);
331 void physical_bones_remove_collision_exception(RID p_exception);
332#endif // PHYSICS_3D_DISABLED
333#endif // _DISABLE_DEPRECATED
334
335public:
336 Skeleton3D();
337 ~Skeleton3D();
338};
339
340VARIANT_ENUM_CAST(Skeleton3D::ModifierCallbackModeProcess);