Debian Codegraph @

scene/3d/look_at_modifier_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* look_at_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 "look_at_modifier_3d.h"
32
33 #include "scene/resources/animation.h"
34
35 void LookAtModifier3D::_validate_property(PropertyInfo &p_property) const {
36 if (Engine::get_singleton()->is_editor_hint() && (p_property.name == "bone_name" || p_property.name == "origin_bone_name")) {
37 Skeleton3D *skeleton = get_skeleton();
38 if (skeleton) {
39 p_property.hint = PROPERTY_HINT_ENUM_SUGGESTION;
40 p_property.hint_string = skeleton->get_concatenated_bone_names();
41 } else {
42 p_property.hint = PROPERTY_HINT_NONE;
43 p_property.hint_string = "";
44 }
45 }
46
47 if (origin_from == ORIGIN_FROM_SPECIFIC_BONE) {
48 if (p_property.name == "origin_external_node") {
49 p_property.usage = PROPERTY_USAGE_NONE;
50 }
51 } else if (origin_from == ORIGIN_FROM_EXTERNAL_NODE) {
52 if (p_property.name == "origin_bone" || p_property.name == "origin_bone_name") {
53 p_property.usage = PROPERTY_USAGE_NONE;
54 }
55 } else {
56 if (p_property.name == "origin_external_node" || p_property.name == "origin_bone" || p_property.name == "origin_bone_name") {
57 p_property.usage = PROPERTY_USAGE_NONE;
58 }
59 }
60
61 if ((!use_angle_limitation &&
62 (p_property.name == "symmetry_limitation" || p_property.name.ends_with("limit_angle") || p_property.name.ends_with("damp_threshold"))) ||
63 (!use_secondary_rotation && p_property.name.begins_with("secondary_")) ||
64 (!symmetry_limitation && (p_property.name == "primary_limit_angle" || p_property.name == "primary_damp_threshold" || p_property.name == "secondary_limit_angle" || p_property.name == "secondary_damp_threshold")) ||
65 (symmetry_limitation && (p_property.name.begins_with("primary_positive") || p_property.name.begins_with("primary_negative") || p_property.name.begins_with("secondary_positive") || (p_property.name.begins_with("secondary_negative"))))) {
66 p_property.usage = PROPERTY_USAGE_NONE;
67 }
68 }
69
70 PackedStringArray LookAtModifier3D::get_configuration_warnings() const {
71 PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings();
72 if (get_axis_from_bone_axis(forward_axis) == primary_rotation_axis) {
73 warnings.push_back(RTR("Forward axis and primary rotation axis must not be parallel."));
74 }
75 return warnings;
76 }
77
78 void LookAtModifier3D::_validate_bone_names() {
79 // Prior bone name.
80 if (!bone_name.is_empty()) {
81 set_bone_name(bone_name);
82 } else if (bone != -1) {
83 set_bone(bone);
84 }
85 if (!origin_bone_name.is_empty()) {
86 set_origin_bone_name(origin_bone_name);
87 } else if (origin_bone != -1) {
88 set_origin_bone(origin_bone);
89 }
90 }
91
92 void LookAtModifier3D::set_bone_name(const String &p_bone_name) {
93 bone_name = p_bone_name;
94 Skeleton3D *sk = get_skeleton();
95 if (sk) {
96 set_bone(sk->find_bone(bone_name));
97 }
98 }
99
100 String LookAtModifier3D::get_bone_name() const {
101 return bone_name;
102 }
103
104 void LookAtModifier3D::set_bone(int p_bone) {
105 bone = p_bone;
106 Skeleton3D *sk = get_skeleton();
107 if (sk) {
108 if (bone <= -1 || bone >= sk->get_bone_count()) {
109 WARN_PRINT("Bone index out of range!");
110 bone = -1;
111 } else {
112 bone_name = sk->get_bone_name(bone);
113 }
114 }
115 }
116
117 int LookAtModifier3D::get_bone() const {
118 return bone;
119 }
120
121 void LookAtModifier3D::set_forward_axis(BoneAxis p_axis) {
122 forward_axis = p_axis;
123 update_configuration_warnings();
124 }
125
126 SkeletonModifier3D::BoneAxis LookAtModifier3D::get_forward_axis() const {
127 return forward_axis;
128 }
129
130 void LookAtModifier3D::set_primary_rotation_axis(Vector3::Axis p_axis) {
131 primary_rotation_axis = p_axis;
132 update_configuration_warnings();
133 }
134
135 Vector3::Axis LookAtModifier3D::get_primary_rotation_axis() const {
136 return primary_rotation_axis;
137 }
138
139 void LookAtModifier3D::set_use_secondary_rotation(bool p_enabled) {
140 use_secondary_rotation = p_enabled;
141 notify_property_list_changed();
142 }
143
144 bool LookAtModifier3D::is_using_secondary_rotation() const {
145 return use_secondary_rotation;
146 }
147
148 void LookAtModifier3D::set_relative(bool p_enabled) {
149 relative = p_enabled;
150 }
151
152 bool LookAtModifier3D::is_relative() const {
153 return relative;
154 }
155
156 void LookAtModifier3D::set_target_node(const NodePath &p_target_node) {
157 if (target_node != p_target_node) {
158 init_transition();
159 }
160 target_node = p_target_node;
161 }
162
163 NodePath LookAtModifier3D::get_target_node() const {
164 return target_node;
165 }
166
167 // For origin settings.
168
169 void LookAtModifier3D::set_origin_from(OriginFrom p_origin_from) {
170 origin_from = p_origin_from;
171 notify_property_list_changed();
172 }
173
174 LookAtModifier3D::OriginFrom LookAtModifier3D::get_origin_from() const {
175 return origin_from;
176 }
177
178 void LookAtModifier3D::set_origin_bone_name(const String &p_bone_name) {
179 origin_bone_name = p_bone_name;
180 Skeleton3D *sk = get_skeleton();
181 if (sk) {
182 set_origin_bone(sk->find_bone(origin_bone_name));
183 }
184 }
185
186 String LookAtModifier3D::get_origin_bone_name() const {
187 return origin_bone_name;
188 }
189
190 void LookAtModifier3D::set_origin_bone(int p_bone) {
191 origin_bone = p_bone;
192 Skeleton3D *sk = get_skeleton();
193 if (sk) {
194 if (origin_bone <= -1 || origin_bone >= sk->get_bone_count()) {
195 WARN_PRINT("Bone index out of range!");
196 origin_bone = -1;
197 } else {
198 origin_bone_name = sk->get_bone_name(origin_bone);
199 }
200 }
201 }
202
203 int LookAtModifier3D::get_origin_bone() const {
204 return origin_bone;
205 }
206
207 void LookAtModifier3D::set_origin_external_node(const NodePath &p_external_node) {
208 origin_external_node = p_external_node;
209 }
210
211 NodePath LookAtModifier3D::get_origin_external_node() const {
212 return origin_external_node;
213 }
214
215 void LookAtModifier3D::set_origin_offset(const Vector3 &p_offset) {
216 origin_offset = p_offset;
217 }
218
219 Vector3 LookAtModifier3D::get_origin_offset() const {
220 return origin_offset;
221 }
222
223 void LookAtModifier3D::set_origin_safe_margin(float p_margin) {
224 origin_safe_margin = p_margin;
225 }
226
227 float LookAtModifier3D::get_origin_safe_margin() const {
228 return origin_safe_margin;
229 }
230
231 // For time-based interpolation.
232
233 void LookAtModifier3D::set_duration(float p_duration) {
234 duration = p_duration;
235 if (Math::is_zero_approx(p_duration)) {
236 time_step = 0;
237 remaining = 0;
238 } else {
239 time_step = 1.0 / p_duration; // Cache to avoid division.
240 }
241 }
242
243 float LookAtModifier3D::get_duration() const {
244 return duration;
245 }
246
247 void LookAtModifier3D::set_transition_type(Tween::TransitionType p_transition_type) {
248 transition_type = p_transition_type;
249 }
250
251 Tween::TransitionType LookAtModifier3D::get_transition_type() const {
252 return transition_type;
253 }
254
255 void LookAtModifier3D::set_ease_type(Tween::EaseType p_ease_type) {
256 ease_type = p_ease_type;
257 }
258
259 Tween::EaseType LookAtModifier3D::get_ease_type() const {
260 return ease_type;
261 }
262
263 // For angle limitation.
264
265 void LookAtModifier3D::set_use_angle_limitation(bool p_enabled) {
266 use_angle_limitation = p_enabled;
267 notify_property_list_changed();
268 }
269
270 bool LookAtModifier3D::is_using_angle_limitation() const {
271 return use_angle_limitation;
272 }
273
274 void LookAtModifier3D::set_symmetry_limitation(bool p_enabled) {
275 symmetry_limitation = p_enabled;
276 notify_property_list_changed();
277 }
278
279 bool LookAtModifier3D::is_limitation_symmetry() const {
280 return symmetry_limitation;
281 }
282
283 void LookAtModifier3D::set_primary_limit_angle(float p_angle) {
284 primary_limit_angle = p_angle;
285 }
286
287 float LookAtModifier3D::get_primary_limit_angle() const {
288 return primary_limit_angle;
289 }
290
291 void LookAtModifier3D::set_primary_damp_threshold(float p_power) {
292 primary_damp_threshold = p_power;
293 }
294
295 float LookAtModifier3D::get_primary_damp_threshold() const {
296 return primary_damp_threshold;
297 }
298
299 void LookAtModifier3D::set_primary_positive_limit_angle(float p_angle) {
300 primary_positive_limit_angle = p_angle;
301 }
302
303 float LookAtModifier3D::get_primary_positive_limit_angle() const {
304 return primary_positive_limit_angle;
305 }
306
307 void LookAtModifier3D::set_primary_positive_damp_threshold(float p_power) {
308 primary_positive_damp_threshold = p_power;
309 }
310
311 float LookAtModifier3D::get_primary_positive_damp_threshold() const {
312 return primary_positive_damp_threshold;
313 }
314
315 void LookAtModifier3D::set_primary_negative_limit_angle(float p_angle) {
316 primary_negative_limit_angle = p_angle;
317 }
318
319 float LookAtModifier3D::get_primary_negative_limit_angle() const {
320 return primary_negative_limit_angle;
321 }
322
323 void LookAtModifier3D::set_primary_negative_damp_threshold(float p_power) {
324 primary_negative_damp_threshold = p_power;
325 }
326
327 float LookAtModifier3D::get_primary_negative_damp_threshold() const {
328 return primary_negative_damp_threshold;
329 }
330
331 void LookAtModifier3D::set_secondary_limit_angle(float p_angle) {
332 secondary_limit_angle = p_angle;
333 }
334
335 float LookAtModifier3D::get_secondary_limit_angle() const {
336 return secondary_limit_angle;
337 }
338
339 void LookAtModifier3D::set_secondary_damp_threshold(float p_power) {
340 secondary_damp_threshold = p_power;
341 }
342
343 float LookAtModifier3D::get_secondary_damp_threshold() const {
344 return secondary_damp_threshold;
345 }
346
347 void LookAtModifier3D::set_secondary_positive_limit_angle(float p_angle) {
348 secondary_positive_limit_angle = p_angle;
349 }
350
351 float LookAtModifier3D::get_secondary_positive_limit_angle() const {
352 return secondary_positive_limit_angle;
353 }
354
355 void LookAtModifier3D::set_secondary_positive_damp_threshold(float p_power) {
356 secondary_positive_damp_threshold = p_power;
357 }
358
359 float LookAtModifier3D::get_secondary_positive_damp_threshold() const {
360 return secondary_positive_damp_threshold;
361 }
362
363 void LookAtModifier3D::set_secondary_negative_limit_angle(float p_angle) {
364 secondary_negative_limit_angle = p_angle;
365 }
366
367 float LookAtModifier3D::get_secondary_negative_limit_angle() const {
368 return secondary_negative_limit_angle;
369 }
370
371 void LookAtModifier3D::set_secondary_negative_damp_threshold(float p_power) {
372 secondary_negative_damp_threshold = p_power;
373 }
374
375 float LookAtModifier3D::get_secondary_negative_damp_threshold() const {
376 return secondary_negative_damp_threshold;
377 }
378
379 bool LookAtModifier3D::is_target_within_limitation() const {
380 return is_within_limitations;
381 }
382
383 float LookAtModifier3D::get_interpolation_remaining() const {
384 return remaining * duration;
385 }
386
387 bool LookAtModifier3D::is_interpolating() const {
388 return Math::is_zero_approx(remaining);
389 }
390
391 // General API.
392
393 void LookAtModifier3D::_bind_methods() {
394 ClassDB::bind_method(D_METHOD("set_target_node", "target_node"), &LookAtModifier3D::set_target_node);
395 ClassDB::bind_method(D_METHOD("get_target_node"), &LookAtModifier3D::get_target_node);
396
397 ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &LookAtModifier3D::set_bone_name);
398 ClassDB::bind_method(D_METHOD("get_bone_name"), &LookAtModifier3D::get_bone_name);
399 ClassDB::bind_method(D_METHOD("set_bone", "bone"), &LookAtModifier3D::set_bone);
400 ClassDB::bind_method(D_METHOD("get_bone"), &LookAtModifier3D::get_bone);
401 ClassDB::bind_method(D_METHOD("set_forward_axis", "forward_axis"), &LookAtModifier3D::set_forward_axis);
402 ClassDB::bind_method(D_METHOD("get_forward_axis"), &LookAtModifier3D::get_forward_axis);
403 ClassDB::bind_method(D_METHOD("set_primary_rotation_axis", "axis"), &LookAtModifier3D::set_primary_rotation_axis);
404 ClassDB::bind_method(D_METHOD("get_primary_rotation_axis"), &LookAtModifier3D::get_primary_rotation_axis);
405 ClassDB::bind_method(D_METHOD("set_use_secondary_rotation", "enabled"), &LookAtModifier3D::set_use_secondary_rotation);
406 ClassDB::bind_method(D_METHOD("is_using_secondary_rotation"), &LookAtModifier3D::is_using_secondary_rotation);
407 ClassDB::bind_method(D_METHOD("set_relative", "enabled"), &LookAtModifier3D::set_relative);
408 ClassDB::bind_method(D_METHOD("is_relative"), &LookAtModifier3D::is_relative);
409
410 ClassDB::bind_method(D_METHOD("set_origin_safe_margin", "margin"), &LookAtModifier3D::set_origin_safe_margin);
411 ClassDB::bind_method(D_METHOD("get_origin_safe_margin"), &LookAtModifier3D::get_origin_safe_margin);
412 ClassDB::bind_method(D_METHOD("set_origin_from", "origin_from"), &LookAtModifier3D::set_origin_from);
413 ClassDB::bind_method(D_METHOD("get_origin_from"), &LookAtModifier3D::get_origin_from);
414 ClassDB::bind_method(D_METHOD("set_origin_bone_name", "bone_name"), &LookAtModifier3D::set_origin_bone_name);
415 ClassDB::bind_method(D_METHOD("get_origin_bone_name"), &LookAtModifier3D::get_origin_bone_name);
416 ClassDB::bind_method(D_METHOD("set_origin_bone", "bone"), &LookAtModifier3D::set_origin_bone);
417 ClassDB::bind_method(D_METHOD("get_origin_bone"), &LookAtModifier3D::get_origin_bone);
418 ClassDB::bind_method(D_METHOD("set_origin_external_node", "external_node"), &LookAtModifier3D::set_origin_external_node);
419 ClassDB::bind_method(D_METHOD("get_origin_external_node"), &LookAtModifier3D::get_origin_external_node);
420
421 ClassDB::bind_method(D_METHOD("set_origin_offset", "offset"), &LookAtModifier3D::set_origin_offset);
422 ClassDB::bind_method(D_METHOD("get_origin_offset"), &LookAtModifier3D::get_origin_offset);
423
424 ClassDB::bind_method(D_METHOD("set_duration", "duration"), &LookAtModifier3D::set_duration);
425 ClassDB::bind_method(D_METHOD("get_duration"), &LookAtModifier3D::get_duration);
426 ClassDB::bind_method(D_METHOD("set_transition_type", "transition_type"), &LookAtModifier3D::set_transition_type);
427 ClassDB::bind_method(D_METHOD("get_transition_type"), &LookAtModifier3D::get_transition_type);
428 ClassDB::bind_method(D_METHOD("set_ease_type", "ease_type"), &LookAtModifier3D::set_ease_type);
429 ClassDB::bind_method(D_METHOD("get_ease_type"), &LookAtModifier3D::get_ease_type);
430
431 ClassDB::bind_method(D_METHOD("set_use_angle_limitation", "enabled"), &LookAtModifier3D::set_use_angle_limitation);
432 ClassDB::bind_method(D_METHOD("is_using_angle_limitation"), &LookAtModifier3D::is_using_angle_limitation);
433 ClassDB::bind_method(D_METHOD("set_symmetry_limitation", "enabled"), &LookAtModifier3D::set_symmetry_limitation);
434 ClassDB::bind_method(D_METHOD("is_limitation_symmetry"), &LookAtModifier3D::is_limitation_symmetry);
435
436 ClassDB::bind_method(D_METHOD("set_primary_limit_angle", "angle"), &LookAtModifier3D::set_primary_limit_angle);
437 ClassDB::bind_method(D_METHOD("get_primary_limit_angle"), &LookAtModifier3D::get_primary_limit_angle);
438 ClassDB::bind_method(D_METHOD("set_primary_damp_threshold", "power"), &LookAtModifier3D::set_primary_damp_threshold);
439 ClassDB::bind_method(D_METHOD("get_primary_damp_threshold"), &LookAtModifier3D::get_primary_damp_threshold);
440
441 ClassDB::bind_method(D_METHOD("set_primary_positive_limit_angle", "angle"), &LookAtModifier3D::set_primary_positive_limit_angle);
442 ClassDB::bind_method(D_METHOD("get_primary_positive_limit_angle"), &LookAtModifier3D::get_primary_positive_limit_angle);
443 ClassDB::bind_method(D_METHOD("set_primary_positive_damp_threshold", "power"), &LookAtModifier3D::set_primary_positive_damp_threshold);
444 ClassDB::bind_method(D_METHOD("get_primary_positive_damp_threshold"), &LookAtModifier3D::get_primary_positive_damp_threshold);
445 ClassDB::bind_method(D_METHOD("set_primary_negative_limit_angle", "angle"), &LookAtModifier3D::set_primary_negative_limit_angle);
446 ClassDB::bind_method(D_METHOD("get_primary_negative_limit_angle"), &LookAtModifier3D::get_primary_negative_limit_angle);
447 ClassDB::bind_method(D_METHOD("set_primary_negative_damp_threshold", "power"), &LookAtModifier3D::set_primary_negative_damp_threshold);
448 ClassDB::bind_method(D_METHOD("get_primary_negative_damp_threshold"), &LookAtModifier3D::get_primary_negative_damp_threshold);
449
450 ClassDB::bind_method(D_METHOD("set_secondary_limit_angle", "angle"), &LookAtModifier3D::set_secondary_limit_angle);
451 ClassDB::bind_method(D_METHOD("get_secondary_limit_angle"), &LookAtModifier3D::get_secondary_limit_angle);
452 ClassDB::bind_method(D_METHOD("set_secondary_damp_threshold", "power"), &LookAtModifier3D::set_secondary_damp_threshold);
453 ClassDB::bind_method(D_METHOD("get_secondary_damp_threshold"), &LookAtModifier3D::get_secondary_damp_threshold);
454
455 ClassDB::bind_method(D_METHOD("set_secondary_positive_limit_angle", "angle"), &LookAtModifier3D::set_secondary_positive_limit_angle);
456 ClassDB::bind_method(D_METHOD("get_secondary_positive_limit_angle"), &LookAtModifier3D::get_secondary_positive_limit_angle);
457 ClassDB::bind_method(D_METHOD("set_secondary_positive_damp_threshold", "power"), &LookAtModifier3D::set_secondary_positive_damp_threshold);
458 ClassDB::bind_method(D_METHOD("get_secondary_positive_damp_threshold"), &LookAtModifier3D::get_secondary_positive_damp_threshold);
459 ClassDB::bind_method(D_METHOD("set_secondary_negative_limit_angle", "angle"), &LookAtModifier3D::set_secondary_negative_limit_angle);
460 ClassDB::bind_method(D_METHOD("get_secondary_negative_limit_angle"), &LookAtModifier3D::get_secondary_negative_limit_angle);
461 ClassDB::bind_method(D_METHOD("set_secondary_negative_damp_threshold", "power"), &LookAtModifier3D::set_secondary_negative_damp_threshold);
462 ClassDB::bind_method(D_METHOD("get_secondary_negative_damp_threshold"), &LookAtModifier3D::get_secondary_negative_damp_threshold);
463
464 ClassDB::bind_method(D_METHOD("get_interpolation_remaining"), &LookAtModifier3D::get_interpolation_remaining);
465 ClassDB::bind_method(D_METHOD("is_interpolating"), &LookAtModifier3D::is_interpolating);
466 ClassDB::bind_method(D_METHOD("is_target_within_limitation"), &LookAtModifier3D::is_target_within_limitation);
467
468 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_node", PROPERTY_HINT_NODE_TYPE, "Node3D"), "set_target_node", "get_target_node");
469
470 ADD_PROPERTY(PropertyInfo(Variant::STRING, "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, ""), "set_bone_name", "get_bone_name");
471 ADD_PROPERTY(PropertyInfo(Variant::INT, "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_bone", "get_bone");
472 ADD_PROPERTY(PropertyInfo(Variant::INT, "forward_axis", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_bone_axis()), "set_forward_axis", "get_forward_axis");
473 ADD_PROPERTY(PropertyInfo(Variant::INT, "primary_rotation_axis", PROPERTY_HINT_ENUM, "X,Y,Z"), "set_primary_rotation_axis", "get_primary_rotation_axis");
474 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_secondary_rotation"), "set_use_secondary_rotation", "is_using_secondary_rotation");
475 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "relative"), "set_relative", "is_relative");
476
477 ADD_GROUP("Origin Settings", "origin_");
478 ADD_PROPERTY(PropertyInfo(Variant::INT, "origin_from", PROPERTY_HINT_ENUM, "Self,SpecificBone,ExternalNode"), "set_origin_from", "get_origin_from");
479 ADD_PROPERTY(PropertyInfo(Variant::STRING, "origin_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, ""), "set_origin_bone_name", "get_origin_bone_name");
480 ADD_PROPERTY(PropertyInfo(Variant::INT, "origin_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_origin_bone", "get_origin_bone");
481 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "origin_external_node", PROPERTY_HINT_NODE_TYPE, "Node3D"), "set_origin_external_node", "get_origin_external_node");
482 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "origin_offset"), "set_origin_offset", "get_origin_offset");
483 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "origin_safe_margin", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater,suffix:m"), "set_origin_safe_margin", "get_origin_safe_margin");
484
485 ADD_GROUP("Time Based Interpolation", "");
486 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "duration", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater,suffix:s"), "set_duration", "get_duration");
487 ADD_PROPERTY(PropertyInfo(Variant::INT, "transition_type", PROPERTY_HINT_ENUM, "Linear,Sine,Quint,Quart,Quad,Expo,Elastic,Cubic,Circ,Bounce,Back,Spring"), "set_transition_type", "get_transition_type");
488 ADD_PROPERTY(PropertyInfo(Variant::INT, "ease_type", PROPERTY_HINT_ENUM, "In,Out,InOut,OutIn"), "set_ease_type", "get_ease_type");
489
490 ADD_GROUP("Angle Limitation", "");
491 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_angle_limitation"), "set_use_angle_limitation", "is_using_angle_limitation");
492 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symmetry_limitation"), "set_symmetry_limitation", "is_limitation_symmetry");
493
494 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "primary_limit_angle", PROPERTY_HINT_RANGE, "0,360,0.01,radians_as_degrees"), "set_primary_limit_angle", "get_primary_limit_angle");
495 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "primary_damp_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_primary_damp_threshold", "get_primary_damp_threshold");
496
497 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "primary_positive_limit_angle", PROPERTY_HINT_RANGE, "0,180,0.01,radians_as_degrees"), "set_primary_positive_limit_angle", "get_primary_positive_limit_angle");
498 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "primary_positive_damp_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_primary_positive_damp_threshold", "get_primary_positive_damp_threshold");
499 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "primary_negative_limit_angle", PROPERTY_HINT_RANGE, "0,180,0.01,radians_as_degrees"), "set_primary_negative_limit_angle", "get_primary_negative_limit_angle");
500 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "primary_negative_damp_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_primary_negative_damp_threshold", "get_primary_negative_damp_threshold");
501
502 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "secondary_limit_angle", PROPERTY_HINT_RANGE, "0,360,0.01,radians_as_degrees"), "set_secondary_limit_angle", "get_secondary_limit_angle");
503 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "secondary_damp_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_secondary_damp_threshold", "get_secondary_damp_threshold");
504
505 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "secondary_positive_limit_angle", PROPERTY_HINT_RANGE, "0,180,0.01,radians_as_degrees"), "set_secondary_positive_limit_angle", "get_secondary_positive_limit_angle");
506 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "secondary_positive_damp_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_secondary_positive_damp_threshold", "get_secondary_positive_damp_threshold");
507 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "secondary_negative_limit_angle", PROPERTY_HINT_RANGE, "0,180,0.01,radians_as_degrees"), "set_secondary_negative_limit_angle", "get_secondary_negative_limit_angle");
508 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "secondary_negative_damp_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_secondary_negative_damp_threshold", "get_secondary_negative_damp_threshold");
509
510 BIND_ENUM_CONSTANT(ORIGIN_FROM_SELF);
511 BIND_ENUM_CONSTANT(ORIGIN_FROM_SPECIFIC_BONE);
512 BIND_ENUM_CONSTANT(ORIGIN_FROM_EXTERNAL_NODE);
513 }
514
515 void LookAtModifier3D::_process_modification(double p_delta) {
516 Skeleton3D *skeleton = get_skeleton();
517 if (!skeleton || bone < 0 || bone >= skeleton->get_bone_count()) {
518 return;
519 }
520
521 // Calculate bone rest space in the world.
522 Transform3D bone_rest = relative ? skeleton->get_bone_pose(bone) : skeleton->get_bone_rest(bone);
523 Transform3D bone_rest_space;
524 int parent_bone = skeleton->get_bone_parent(bone);
525 if (parent_bone < 0) {
526 bone_rest_space = skeleton->get_global_transform_interpolated();
527 bone_rest_space.translate_local(bone_rest.origin);
528 } else {
529 bone_rest_space = skeleton->get_global_transform_interpolated() * skeleton->get_bone_global_pose(parent_bone);
530 bone_rest_space.translate_local(bone_rest.origin);
531 }
532
533 // Calculate forward_vector and destination.
534 is_within_limitations = true;
535 Vector3 prev_forward_vector = forward_vector;
536 Quaternion destination;
537 Node3D *target = Object::cast_to<Node3D>(get_node_or_null(target_node));
538 if (!target) {
539 destination = skeleton->get_bone_pose_rotation(bone);
540 } else {
541 Transform3D origin_tr;
542 if (origin_from == ORIGIN_FROM_SPECIFIC_BONE && origin_bone >= 0 && origin_bone < skeleton->get_bone_count()) {
543 origin_tr = skeleton->get_global_transform_interpolated() * skeleton->get_bone_global_pose(origin_bone);
544 } else if (origin_from == ORIGIN_FROM_EXTERNAL_NODE) {
545 Node3D *origin_src = Object::cast_to<Node3D>(get_node_or_null(origin_external_node));
546 if (origin_src) {
547 origin_tr = origin_src->get_global_transform_interpolated();
548 } else {
549 origin_tr = bone_rest_space;
550 }
551 } else {
552 origin_tr = bone_rest_space;
553 }
554 forward_vector = bone_rest_space.orthonormalized().basis.xform_inv(target->get_global_transform_interpolated().origin - origin_tr.translated_local(origin_offset).origin);
555 forward_vector_nrm = forward_vector.normalized();
556 if (forward_vector_nrm.abs().is_equal_approx(get_vector_from_axis(primary_rotation_axis))) {
557 destination = skeleton->get_bone_pose_rotation(bone);
558 forward_vector = Vector3(0, 0, 0); // The zero-vector to be used for checking in the line immediately below to avoid animation glitch.
559 } else {
560 destination = look_at_with_axes(bone_rest).basis.get_rotation_quaternion();
561 forward_vector = bone_rest.basis.xform_inv(forward_vector_nrm); // Forward vector in the "current" bone rest.
562 }
563 }
564
565 // Detect flipping.
566 bool is_not_max_influence = influence < 1.0;
567 bool is_flippable = use_angle_limitation || is_not_max_influence;
568 Vector3::Axis current_forward_axis = get_axis_from_bone_axis(forward_axis);
569 if (is_intersecting_axis(prev_forward_vector, forward_vector, current_forward_axis, secondary_rotation_axis) ||
570 is_intersecting_axis(prev_forward_vector, forward_vector, primary_rotation_axis, primary_rotation_axis, true) ||
571 is_intersecting_axis(prev_forward_vector, forward_vector, secondary_rotation_axis, current_forward_axis) ||
572 (prev_forward_vector != Vector3(0, 0, 0) && forward_vector == Vector3(0, 0, 0)) ||
573 (prev_forward_vector == Vector3(0, 0, 0) && forward_vector != Vector3(0, 0, 0))) {
574 init_transition();
575 } else if (is_flippable && std::signbit(prev_forward_vector[secondary_rotation_axis]) != std::signbit(forward_vector[secondary_rotation_axis])) {
576 // Flipping by angle_limitation can be detected by sign of secondary rotation axes during forward_vector is rotated more than 90 degree from forward_axis (means dot production is negative).
577 Vector3 rest_forward_vector = get_vector_from_bone_axis(forward_axis);
578 if (symmetry_limitation) {
579 if ((is_not_max_influence || !Math::is_equal_approx(primary_limit_angle, (float)Math::TAU)) &&
580 prev_forward_vector.dot(rest_forward_vector) < 0 &&
581 forward_vector.dot(rest_forward_vector) < 0) {
582 init_transition();
583 }
584 } else {
585 if ((is_not_max_influence || !Math::is_equal_approx(primary_positive_limit_angle + primary_negative_limit_angle, (float)Math::TAU)) &&
586 prev_forward_vector.dot(rest_forward_vector) < 0 &&
587 forward_vector.dot(rest_forward_vector) < 0) {
588 init_transition();
589 }
590 }
591 }
592
593 // Do time-based interpolation.
594 if (remaining > 0) {
595 remaining = MAX(0, remaining - time_step * p_delta);
596 if (is_flippable) {
597 // Interpolate through the rest same as AnimationTree blending for preventing to penetrate the bone into the body.
598 Quaternion rest = bone_rest.basis.get_rotation_quaternion();
599 float weight = Tween::run_equation(transition_type, ease_type, 1 - remaining, 0.0, 1.0, 1.0);
600 destination = Animation::interpolate_via_rest(Animation::interpolate_via_rest(rest, from_q, 1 - weight, rest), destination, weight, rest);
601 } else {
602 destination = from_q.slerp(destination, Tween::run_equation(transition_type, ease_type, 1 - remaining, 0.0, 1.0, 1.0));
603 }
604 }
605
606 skeleton->set_bone_pose_rotation(bone, destination);
607 prev_q = destination;
608 }
609
610 bool LookAtModifier3D::is_intersecting_axis(const Vector3 &p_prev, const Vector3 &p_current, Vector3::Axis p_flipping_axis, Vector3::Axis p_check_axis, bool p_check_plane) const {
611 // Prevent that the angular velocity does not become too large.
612 // Check that is p_flipping_axis flipped nearby p_check_axis (close than origin_safe_margin) or not. If p_check_plane is true, check two axes of crossed plane.
613 if (p_check_plane) {
614 if (get_projection_vector(p_prev, p_check_axis).length() > origin_safe_margin && get_projection_vector(p_current, p_check_axis).length() > origin_safe_margin) {
615 return false;
616 }
617 } else if (Math::abs(p_prev[p_check_axis]) > origin_safe_margin && Math::abs(p_current[p_check_axis]) > origin_safe_margin) {
618 return false;
619 }
620
621 return std::signbit(p_prev[p_flipping_axis]) != std::signbit(p_current[p_flipping_axis]);
622 }
623
624 Vector3 LookAtModifier3D::get_basis_vector_from_bone_axis(const Basis &p_basis, BoneAxis p_axis) {
625 Vector3 ret;
626 switch (p_axis) {
627 case BONE_AXIS_PLUS_X: {
628 ret = p_basis.get_column(0);
629 } break;
630 case BONE_AXIS_MINUS_X: {
631 ret = -p_basis.get_column(0);
632 } break;
633 case BONE_AXIS_PLUS_Y: {
634 ret = p_basis.get_column(1);
635 } break;
636 case BONE_AXIS_MINUS_Y: {
637 ret = -p_basis.get_column(1);
638 } break;
639 case BONE_AXIS_PLUS_Z: {
640 ret = p_basis.get_column(2);
641 } break;
642 case BONE_AXIS_MINUS_Z: {
643 ret = -p_basis.get_column(2);
644 } break;
645 }
646 return ret;
647 }
648
649 Vector3::Axis LookAtModifier3D::get_secondary_rotation_axis(BoneAxis p_forward_axis, Vector3::Axis p_primary_rotation_axis) {
650 Vector3 secondary_plane = get_vector_from_bone_axis(p_forward_axis) + get_vector_from_axis(p_primary_rotation_axis);
651 return Math::is_zero_approx(secondary_plane.x) ? Vector3::AXIS_X : (Math::is_zero_approx(secondary_plane.y) ? Vector3::AXIS_Y : Vector3::AXIS_Z);
652 }
653
654 Vector2 LookAtModifier3D::get_projection_vector(const Vector3 &p_vector, Vector3::Axis p_axis) {
655 // NOTE: axis is swapped between 2D and 3D.
656 Vector2 ret;
657 switch (p_axis) {
658 case Vector3::AXIS_X: {
659 ret = Vector2(p_vector.z, p_vector.y);
660 } break;
661 case Vector3::AXIS_Y: {
662 ret = Vector2(p_vector.x, p_vector.z);
663 } break;
664 case Vector3::AXIS_Z: {
665 ret = Vector2(p_vector.y, p_vector.x);
666 } break;
667 }
668 return ret;
669 }
670
671 float LookAtModifier3D::remap_damped(float p_from, float p_to, float p_damp_threshold, float p_value) const {
672 float sign = std::signbit(p_value) ? -1.0f : 1.0f;
673 float abs_value = Math::abs(p_value);
674
675 if (Math::is_equal_approx(p_damp_threshold, 1.0f) || Math::is_zero_approx(p_to)) {
676 return sign * CLAMP(abs_value, p_from, p_to); // Avoid division by zero.
677 }
678
679 float value = Math::inverse_lerp(p_from, p_to, abs_value);
680
681 if (value <= p_damp_threshold) {
682 return sign * CLAMP(abs_value, p_from, p_to);
683 }
684
685 double limit = Math::PI;
686 double inv_to = 1.0 / p_to;
687 double end_x = limit * inv_to;
688 double position = abs_value * inv_to;
689 Vector2 start = Vector2(p_damp_threshold, p_damp_threshold);
690 Vector2 mid = Vector2(1.0, 1.0);
691 Vector2 end = Vector2(end_x, 1.0);
692 value = get_bspline_y(start, mid, end, position);
693
694 return sign * Math::lerp(p_from, p_to, value);
695 }
696
697 double LookAtModifier3D::get_bspline_y(const Vector2 &p_from, const Vector2 &p_control, const Vector2 &p_to, double p_x) const {
698 double a = p_from.x - 2.0 * p_control.x + p_to.x;
699 double b = -2.0 * p_from.x + 2.0 * p_control.x;
700 double c = p_from.x - p_x;
701 double t = 0.0;
702 if (Math::is_zero_approx(a)) {
703 t = -c / b; // Almost linear.
704 } else {
705 double discriminant = b * b - 4.0 * a * c;
706 double sqrt_discriminant = Math::sqrt(discriminant);
707 double e = 1.0 / (2.0 * a);
708 double t1 = (-b + sqrt_discriminant) * e;
709 t = (0.0 <= t1 && t1 <= 1.0) ? t1 : (-b - sqrt_discriminant) * e;
710 }
711 double u = 1.0 - t;
712 double y = u * u * p_from.y + 2.0 * u * t * p_control.y + t * t * p_to.y;
713 return y;
714 }
715
716 Transform3D LookAtModifier3D::look_at_with_axes(const Transform3D &p_rest) {
717 // Primary rotation by projection to 2D plane by xform_inv and picking elements.
718 Vector3 current_vector = get_basis_vector_from_bone_axis(p_rest.basis, forward_axis).normalized();
719 Vector2 src_vec2 = get_projection_vector(p_rest.basis.xform_inv(forward_vector_nrm), primary_rotation_axis).normalized();
720 Vector2 dst_vec2 = get_projection_vector(p_rest.basis.xform_inv(current_vector), primary_rotation_axis).normalized();
721 real_t calculated_angle = src_vec2.angle_to(dst_vec2);
722 Transform3D primary_result = p_rest.rotated_local(get_vector_from_axis(primary_rotation_axis), calculated_angle);
723 Transform3D current_result = primary_result; // primary_result will be used by calculation of secondary rotation, current_result is rotated by that.
724 float limit_angle = 0.0;
725 float damp_threshold = 0.0;
726
727 if (use_angle_limitation) {
728 if (symmetry_limitation) {
729 limit_angle = primary_limit_angle * 0.5f;
730 damp_threshold = primary_damp_threshold;
731 } else {
732 if (std::signbit(calculated_angle)) {
733 limit_angle = primary_negative_limit_angle;
734 damp_threshold = primary_negative_damp_threshold;
735 } else {
736 limit_angle = primary_positive_limit_angle;
737 damp_threshold = primary_positive_damp_threshold;
738 }
739 }
740 if (Math::abs(calculated_angle) > limit_angle) {
741 is_within_limitations = false;
742 }
743 calculated_angle = remap_damped(0, limit_angle, damp_threshold, calculated_angle);
744 current_result = p_rest.rotated_local(get_vector_from_axis(primary_rotation_axis), calculated_angle);
745 }
746
747 // Needs for detecting flipping even if use_secondary_rotation is false.
748 secondary_rotation_axis = get_secondary_rotation_axis(forward_axis, primary_rotation_axis);
749
750 if (!use_secondary_rotation) {
751 return current_result;
752 }
753
754 // Secondary rotation by projection to 2D plane by xform_inv and picking elements.
755 current_vector = get_basis_vector_from_bone_axis(primary_result.basis, forward_axis).normalized();
756 src_vec2 = get_projection_vector(primary_result.basis.xform_inv(forward_vector_nrm), secondary_rotation_axis).normalized();
757 dst_vec2 = get_projection_vector(primary_result.basis.xform_inv(current_vector), secondary_rotation_axis).normalized();
758 calculated_angle = src_vec2.angle_to(dst_vec2);
759
760 if (use_angle_limitation) {
761 if (symmetry_limitation) {
762 limit_angle = secondary_limit_angle * 0.5f;
763 damp_threshold = secondary_damp_threshold;
764 } else {
765 if (std::signbit(calculated_angle)) {
766 limit_angle = secondary_negative_limit_angle;
767 damp_threshold = secondary_negative_damp_threshold;
768 } else {
769 limit_angle = secondary_positive_limit_angle;
770 damp_threshold = secondary_positive_damp_threshold;
771 }
772 }
773 if (Math::abs(calculated_angle) > limit_angle) {
774 is_within_limitations = false;
775 }
776 calculated_angle = remap_damped(0, limit_angle, damp_threshold, calculated_angle);
777 }
778
779 current_result = current_result.rotated_local(get_vector_from_axis(secondary_rotation_axis), calculated_angle);
780
781 return current_result;
782 }
783
784 void LookAtModifier3D::init_transition() {
785 if (Math::is_zero_approx(duration)) {
786 return;
787 }
788 from_q = prev_q;
789 remaining = 1.0;
790 }