Debian Codegraph @

scene/3d/retarget_modifier_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* retarget_modifier_3d.cpp */
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 #include "retarget_modifier_3d.h"
32
33 PackedStringArray RetargetModifier3D::get_configuration_warnings() const {
34 PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings();
35 if (child_skeletons.is_empty()) {
36 warnings.push_back(RTR("There is no child Skeleton3D!"));
37 }
38 return warnings;
39 }
40
41 /// Caching
42
43 void RetargetModifier3D::_profile_changed(Ref<SkeletonProfile> p_old, Ref<SkeletonProfile> p_new) {
44 if (p_old.is_valid() && p_old->is_connected(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::cache_rests_with_reset))) {
45 p_old->disconnect(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::cache_rests_with_reset));
46 }
47 profile = p_new;
48 if (p_new.is_valid() && !p_new->is_connected(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::cache_rests_with_reset))) {
49 p_new->connect(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::cache_rests_with_reset));
50 }
51 cache_rests_with_reset();
52 }
53
54 void RetargetModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
55 if (p_old && p_old->is_connected(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_rests))) {
56 p_old->disconnect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_rests));
57 }
58 if (p_new && !p_new->is_connected(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_rests))) {
59 p_new->connect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_rests));
60 }
61 cache_rests();
62 }
63
64 void RetargetModifier3D::cache_rests_with_reset() {
65 _reset_child_skeleton_poses();
66 cache_rests();
67 }
68
69 void RetargetModifier3D::cache_rests() {
70 source_bone_ids.clear();
71
72 Skeleton3D *source_skeleton = get_skeleton();
73 if (profile.is_null() || !source_skeleton) {
74 return;
75 }
76
77 PackedStringArray bone_names = profile->get_bone_names();
78 for (const String &E : bone_names) {
79 source_bone_ids.push_back(source_skeleton->find_bone(E));
80 }
81
82 for (int i = 0; i < child_skeletons.size(); i++) {
83 _update_child_skeleton_rests(i);
84 }
85 }
86
87 Vector<RetargetModifier3D::RetargetBoneInfo> RetargetModifier3D::cache_bone_global_rests(Skeleton3D *p_skeleton) {
88 // Retarget global pose in model space:
89 // tgt_global_pose.basis = src_global_pose.basis * src_rest.basis.inv * src_parent_global_rest.basis.inv * tgt_parent_global_rest.basis * tgt_rest.basis
90 // tgt_global_pose.origin = src_global_pose.origin
91 Skeleton3D *source_skeleton = get_skeleton();
92 Vector<RetargetBoneInfo> bone_rests;
93 if (profile.is_null() || !source_skeleton) {
94 return bone_rests;
95 }
96 PackedStringArray bone_names = profile->get_bone_names();
97 for (const String &E : bone_names) {
98 RetargetBoneInfo rbi;
99 int source_bone_id = source_skeleton->find_bone(E);
100 if (source_bone_id >= 0) {
101 Transform3D parent_global_rest;
102 int bone_parent = source_skeleton->get_bone_parent(source_bone_id);
103 if (bone_parent >= 0) {
104 parent_global_rest = source_skeleton->get_bone_global_rest(bone_parent);
105 }
106 rbi.post_basis = source_skeleton->get_bone_rest(source_bone_id).basis.inverse() * parent_global_rest.basis.inverse();
107 }
108 int target_bone_id = p_skeleton->find_bone(E);
109 rbi.bone_id = target_bone_id;
110 if (target_bone_id >= 0) {
111 Transform3D parent_global_rest;
112 int bone_parent = p_skeleton->get_bone_parent(target_bone_id);
113 if (bone_parent >= 0) {
114 parent_global_rest = p_skeleton->get_bone_global_rest(bone_parent);
115 }
116 rbi.post_basis = rbi.post_basis * parent_global_rest.basis * p_skeleton->get_bone_rest(target_bone_id).basis;
117 }
118 bone_rests.push_back(rbi);
119 }
120 return bone_rests;
121 }
122
123 Vector<RetargetModifier3D::RetargetBoneInfo> RetargetModifier3D::cache_bone_rests(Skeleton3D *p_skeleton) {
124 // Retarget pose in model space:
125 // tgt_pose.basis = tgt_parent_global_rest.basis.inv * src_parent_global_rest.basis * src_pose.basis * src_rest.basis.inv * src_parent_global_rest.basis.inv * tgt_parent_global_rest.basis * tgt_rest.basis
126 // tgt_pose.origin = tgt_parent_global_rest.basis.inv.xform(src_parent_global_rest.basis.xform(src_pose.origin - src_rest.origin)) + tgt_rest.origin
127 Skeleton3D *source_skeleton = get_skeleton();
128 Vector<RetargetBoneInfo> bone_rests;
129 if (profile.is_null() || !source_skeleton) {
130 return bone_rests;
131 }
132 PackedStringArray bone_names = profile->get_bone_names();
133 for (const String &E : bone_names) {
134 RetargetBoneInfo rbi;
135 int source_bone_id = source_skeleton->find_bone(E);
136 if (source_bone_id >= 0) {
137 Transform3D parent_global_rest;
138 int bone_parent = source_skeleton->get_bone_parent(source_bone_id);
139 if (bone_parent >= 0) {
140 parent_global_rest = source_skeleton->get_bone_global_rest(bone_parent);
141 }
142 rbi.pre_basis = parent_global_rest.basis;
143 rbi.post_basis = source_skeleton->get_bone_rest(source_bone_id).basis.inverse() * parent_global_rest.basis.inverse();
144 }
145
146 int target_bone_id = p_skeleton->find_bone(E);
147 rbi.bone_id = target_bone_id;
148 if (target_bone_id >= 0) {
149 Transform3D parent_global_rest;
150 int bone_parent = p_skeleton->get_bone_parent(target_bone_id);
151 if (bone_parent >= 0) {
152 parent_global_rest = p_skeleton->get_bone_global_rest(bone_parent);
153 }
154 rbi.pre_basis = parent_global_rest.basis.inverse() * rbi.pre_basis;
155 rbi.post_basis = rbi.post_basis * parent_global_rest.basis * p_skeleton->get_bone_rest(target_bone_id).basis;
156 }
157 bone_rests.push_back(rbi);
158 }
159 return bone_rests;
160 }
161
162 void RetargetModifier3D::_update_child_skeleton_rests(int p_child_skeleton_idx) {
163 ERR_FAIL_INDEX(p_child_skeleton_idx, child_skeletons.size());
164 Skeleton3D *c = ObjectDB::get_instance<Skeleton3D>(child_skeletons[p_child_skeleton_idx].skeleton_id);
165 if (!c) {
166 return;
167 }
168 if (use_global_pose) {
169 child_skeletons.write[p_child_skeleton_idx].humanoid_bone_rests = cache_bone_global_rests(c);
170 } else {
171 child_skeletons.write[p_child_skeleton_idx].humanoid_bone_rests = cache_bone_rests(c);
172 }
173 }
174
175 void RetargetModifier3D::_update_child_skeletons() {
176 _reset_child_skeletons();
177
178 for (int i = 0; i < get_child_count(); i++) {
179 RetargetInfo ri;
180 Skeleton3D *c = Object::cast_to<Skeleton3D>(get_child(i));
181 if (c) {
182 int id = child_skeletons.size();
183 ri.skeleton_id = c->get_instance_id();
184 child_skeletons.push_back(ri);
185 c->connect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::_update_child_skeleton_rests).bind(id));
186 }
187 }
188
189 cache_rests();
190 update_configuration_warnings();
191 }
192
193 void RetargetModifier3D::_reset_child_skeleton_poses() {
194 for (const RetargetInfo &E : child_skeletons) {
195 Skeleton3D *c = ObjectDB::get_instance<Skeleton3D>(E.skeleton_id);
196 if (!c) {
197 continue;
198 }
199 if (c->is_connected(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::_update_child_skeleton_rests))) {
200 c->disconnect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::_update_child_skeleton_rests));
201 }
202 for (const RetargetBoneInfo &F : E.humanoid_bone_rests) {
203 if (F.bone_id < 0) {
204 continue;
205 }
206 c->reset_bone_pose(F.bone_id);
207 }
208 }
209 }
210
211 void RetargetModifier3D::_reset_child_skeletons() {
212 _reset_child_skeleton_poses();
213 child_skeletons.clear();
214 }
215
216 #ifdef TOOLS_ENABLED
217 void RetargetModifier3D::_force_update_child_skeletons() {
218 for (const RetargetInfo &E : child_skeletons) {
219 Skeleton3D *c = ObjectDB::get_instance<Skeleton3D>(E.skeleton_id);
220 if (!c) {
221 continue;
222 }
223 c->force_update_all_dirty_bones();
224 c->emit_signal(SceneStringName(skeleton_updated));
225 }
226 }
227 #endif // TOOLS_ENABLED
228
229 /// General functions
230
231 void RetargetModifier3D::add_child_notify(Node *p_child) {
232 if (Object::cast_to<Skeleton3D>(p_child)) {
233 _update_child_skeletons();
234 }
235 }
236
237 void RetargetModifier3D::move_child_notify(Node *p_child) {
238 if (Object::cast_to<Skeleton3D>(p_child)) {
239 _update_child_skeletons();
240 }
241 }
242
243 void RetargetModifier3D::remove_child_notify(Node *p_child) {
244 if (Object::cast_to<Skeleton3D>(p_child)) {
245 // Reset after process.
246 callable_mp(this, &RetargetModifier3D::_update_child_skeletons).call_deferred();
247 }
248 }
249
250 void RetargetModifier3D::_validate_property(PropertyInfo &p_property) const {
251 if (use_global_pose) {
252 if (p_property.name == "enable") {
253 p_property.usage = PROPERTY_USAGE_NONE;
254 }
255 }
256 }
257
258 void RetargetModifier3D::_bind_methods() {
259 ClassDB::bind_method(D_METHOD("set_profile", "profile"), &RetargetModifier3D::set_profile);
260 ClassDB::bind_method(D_METHOD("get_profile"), &RetargetModifier3D::get_profile);
261 ClassDB::bind_method(D_METHOD("set_use_global_pose", "use_global_pose"), &RetargetModifier3D::set_use_global_pose);
262 ClassDB::bind_method(D_METHOD("is_using_global_pose"), &RetargetModifier3D::is_using_global_pose);
263 ClassDB::bind_method(D_METHOD("set_enable_flags", "enable_flags"), &RetargetModifier3D::set_enable_flags);
264 ClassDB::bind_method(D_METHOD("get_enable_flags"), &RetargetModifier3D::get_enable_flags);
265
266 ClassDB::bind_method(D_METHOD("set_position_enabled", "enabled"), &RetargetModifier3D::set_position_enabled);
267 ClassDB::bind_method(D_METHOD("is_position_enabled"), &RetargetModifier3D::is_position_enabled);
268 ClassDB::bind_method(D_METHOD("set_rotation_enabled", "enabled"), &RetargetModifier3D::set_rotation_enabled);
269 ClassDB::bind_method(D_METHOD("is_rotation_enabled"), &RetargetModifier3D::is_rotation_enabled);
270 ClassDB::bind_method(D_METHOD("set_scale_enabled", "enabled"), &RetargetModifier3D::set_scale_enabled);
271 ClassDB::bind_method(D_METHOD("is_scale_enabled"), &RetargetModifier3D::is_scale_enabled);
272
273 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "profile", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonProfile"), "set_profile", "get_profile");
274 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_global_pose"), "set_use_global_pose", "is_using_global_pose");
275 ADD_PROPERTY(PropertyInfo(Variant::INT, "enable", PROPERTY_HINT_FLAGS, "Position,Rotation,Scale"), "set_enable_flags", "get_enable_flags");
276
277 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_POSITION);
278 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_ROTATION);
279 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_SCALE);
280 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_ALL);
281 }
282
283 void RetargetModifier3D::_set_active(bool p_active) {
284 if (!p_active) {
285 _reset_child_skeleton_poses();
286 }
287 }
288
289 void RetargetModifier3D::_retarget_global_pose() {
290 Skeleton3D *source_skeleton = get_skeleton();
291 if (profile.is_null() || !source_skeleton) {
292 return;
293 }
294
295 LocalVector<Transform3D> source_poses;
296 if (influence < 1.0) {
297 for (int source_bone_id : source_bone_ids) {
298 source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_global_rest(source_bone_id).interpolate_with(source_skeleton->get_bone_global_pose(source_bone_id), influence));
299 }
300 } else {
301 for (int source_bone_id : source_bone_ids) {
302 source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_global_pose(source_bone_id));
303 }
304 }
305
306 for (const RetargetInfo &E : child_skeletons) {
307 Skeleton3D *target_skeleton = ObjectDB::get_instance<Skeleton3D>(E.skeleton_id);
308 if (!target_skeleton) {
309 continue;
310 }
311 for (int i = 0; i < source_bone_ids.size(); i++) {
312 int target_bone_id = E.humanoid_bone_rests[i].bone_id;
313 if (target_bone_id < 0) {
314 continue;
315 }
316 Transform3D retarget_pose = source_poses[i];
317 retarget_pose.basis = retarget_pose.basis * E.humanoid_bone_rests[i].post_basis;
318 target_skeleton->set_bone_global_pose(target_bone_id, retarget_pose);
319 }
320 }
321 }
322
323 void RetargetModifier3D::_retarget_pose() {
324 Skeleton3D *source_skeleton = get_skeleton();
325 if (profile.is_null() || !source_skeleton) {
326 return;
327 }
328
329 LocalVector<Transform3D> source_poses;
330 if (influence < 1.0) {
331 for (int source_bone_id : source_bone_ids) {
332 source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_rest(source_bone_id).interpolate_with(source_skeleton->get_bone_pose(source_bone_id), influence));
333 }
334 } else {
335 for (int source_bone_id : source_bone_ids) {
336 source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_pose(source_bone_id));
337 }
338 }
339
340 for (const RetargetInfo &E : child_skeletons) {
341 Skeleton3D *target_skeleton = ObjectDB::get_instance<Skeleton3D>(E.skeleton_id);
342 if (!target_skeleton) {
343 continue;
344 }
345 float motion_scale_ratio = target_skeleton->get_motion_scale() / source_skeleton->get_motion_scale();
346 for (int i = 0; i < source_bone_ids.size(); i++) {
347 int target_bone_id = E.humanoid_bone_rests[i].bone_id;
348 if (target_bone_id < 0) {
349 continue;
350 }
351 int source_bone_id = source_bone_ids[i];
352 if (source_bone_id < 0) {
353 continue;
354 }
355
356 Transform3D extracted_transform = source_poses[i];
357 extracted_transform.basis = E.humanoid_bone_rests[i].pre_basis * extracted_transform.basis * E.humanoid_bone_rests[i].post_basis;
358 extracted_transform.origin = E.humanoid_bone_rests[i].pre_basis.xform((extracted_transform.origin - source_skeleton->get_bone_rest(source_bone_id).origin) * motion_scale_ratio) + target_skeleton->get_bone_rest(target_bone_id).origin;
359
360 if (enable_flags.has_flag(TRANSFORM_FLAG_POSITION)) {
361 target_skeleton->set_bone_pose_position(target_bone_id, extracted_transform.origin);
362 }
363 if (enable_flags.has_flag(TRANSFORM_FLAG_ROTATION)) {
364 target_skeleton->set_bone_pose_rotation(target_bone_id, extracted_transform.basis.get_rotation_quaternion());
365 }
366 if (enable_flags.has_flag(TRANSFORM_FLAG_SCALE)) {
367 target_skeleton->set_bone_pose_scale(target_bone_id, extracted_transform.basis.get_scale());
368 }
369 }
370 }
371 }
372
373 void RetargetModifier3D::_process_modification(double p_delta) {
374 if (use_global_pose) {
375 _retarget_global_pose();
376 } else {
377 _retarget_pose();
378 }
379 }
380
381 void RetargetModifier3D::set_profile(Ref<SkeletonProfile> p_profile) {
382 if (profile == p_profile) {
383 return;
384 }
385 _profile_changed(profile, p_profile);
386 }
387
388 Ref<SkeletonProfile> RetargetModifier3D::get_profile() const {
389 return profile;
390 }
391
392 void RetargetModifier3D::set_use_global_pose(bool p_use_global_pose) {
393 if (use_global_pose == p_use_global_pose) {
394 return;
395 }
396
397 use_global_pose = p_use_global_pose;
398 cache_rests_with_reset();
399
400 notify_property_list_changed();
401 }
402
403 bool RetargetModifier3D::is_using_global_pose() const {
404 return use_global_pose;
405 }
406
407 void RetargetModifier3D::set_enable_flags(BitField<TransformFlag> p_enable_flag) {
408 if (enable_flags != p_enable_flag) {
409 _reset_child_skeleton_poses();
410 }
411 enable_flags = p_enable_flag;
412 }
413
414 BitField<RetargetModifier3D::TransformFlag> RetargetModifier3D::get_enable_flags() const {
415 return enable_flags;
416 }
417
418 void RetargetModifier3D::set_position_enabled(bool p_enabled) {
419 if (enable_flags.has_flag(TRANSFORM_FLAG_POSITION) != p_enabled) {
420 _reset_child_skeleton_poses();
421 }
422 if (p_enabled) {
423 enable_flags.set_flag(TRANSFORM_FLAG_POSITION);
424 } else {
425 enable_flags.clear_flag(TRANSFORM_FLAG_POSITION);
426 }
427 }
428
429 bool RetargetModifier3D::is_position_enabled() const {
430 return enable_flags.has_flag(TRANSFORM_FLAG_POSITION);
431 }
432
433 void RetargetModifier3D::set_rotation_enabled(bool p_enabled) {
434 if (enable_flags.has_flag(TRANSFORM_FLAG_ROTATION) != p_enabled) {
435 _reset_child_skeleton_poses();
436 }
437 if (p_enabled) {
438 enable_flags.set_flag(TRANSFORM_FLAG_ROTATION);
439 } else {
440 enable_flags.clear_flag(TRANSFORM_FLAG_ROTATION);
441 }
442 }
443
444 bool RetargetModifier3D::is_rotation_enabled() const {
445 return enable_flags.has_flag(TRANSFORM_FLAG_ROTATION);
446 }
447
448 void RetargetModifier3D::set_scale_enabled(bool p_enabled) {
449 if (enable_flags.has_flag(TRANSFORM_FLAG_SCALE) != p_enabled) {
450 _reset_child_skeleton_poses();
451 }
452 if (p_enabled) {
453 enable_flags.set_flag(TRANSFORM_FLAG_SCALE);
454 } else {
455 enable_flags.clear_flag(TRANSFORM_FLAG_SCALE);
456 }
457 }
458
459 bool RetargetModifier3D::is_scale_enabled() const {
460 return enable_flags.has_flag(TRANSFORM_FLAG_SCALE);
461 }
462
463 void RetargetModifier3D::_notification(int p_what) {
464 switch (p_what) {
465 case NOTIFICATION_ENTER_TREE: {
466 _update_child_skeletons();
467 } break;
468 case NOTIFICATION_EXIT_TREE: {
469 _reset_child_skeletons();
470 } break;
471 }
472 }
473
474 RetargetModifier3D::RetargetModifier3D() {
475 }
476
477 RetargetModifier3D::~RetargetModifier3D() {
478 }