Debian Codegraph @

scene/3d/skeleton_modifier_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* skeleton_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 "skeleton_modifier_3d.h"
32
33 PackedStringArray SkeletonModifier3D::get_configuration_warnings() const {
34 PackedStringArray warnings = Node3D::get_configuration_warnings();
35 if (skeleton_id.is_null()) {
36 warnings.push_back(RTR("Skeleton3D node not set! SkeletonModifier3D must be child of Skeleton3D."));
37 }
38 return warnings;
39 }
40
41 /* Skeleton3D */
42
43 Skeleton3D *SkeletonModifier3D::get_skeleton() const {
44 return ObjectDB::get_instance<Skeleton3D>(skeleton_id);
45 }
46
47 void SkeletonModifier3D::_update_skeleton_path() {
48 skeleton_id = ObjectID();
49
50 // Make sure parent is a Skeleton3D.
51 Skeleton3D *sk = Object::cast_to<Skeleton3D>(get_parent());
52 if (sk) {
53 skeleton_id = sk->get_instance_id();
54 }
55 }
56
57 void SkeletonModifier3D::_update_skeleton() {
58 if (!is_inside_tree()) {
59 return;
60 }
61 Skeleton3D *old_sk = get_skeleton();
62 _update_skeleton_path();
63 Skeleton3D *new_sk = get_skeleton();
64 if (old_sk != new_sk) {
65 _skeleton_changed(old_sk, new_sk);
66 }
67 if (new_sk) {
68 _validate_bone_names();
69 }
70 update_configuration_warnings();
71 }
72
73 void SkeletonModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
74 GDVIRTUAL_CALL(_skeleton_changed, p_old, p_new);
75 }
76
77 void SkeletonModifier3D::_validate_bone_names() {
78 GDVIRTUAL_CALL(_validate_bone_names);
79 }
80
81 void SkeletonModifier3D::_force_update_skeleton_skin() {
82 if (!is_inside_tree()) {
83 return;
84 }
85 Skeleton3D *skeleton = get_skeleton();
86 if (!skeleton) {
87 return;
88 }
89 skeleton->force_update_deferred();
90 }
91
92 /* Process */
93
94 void SkeletonModifier3D::set_active(bool p_active) {
95 if (active == p_active) {
96 return;
97 }
98 active = p_active;
99 _set_active(active);
100 _force_update_skeleton_skin();
101 }
102
103 bool SkeletonModifier3D::is_active() const {
104 return active;
105 }
106
107 void SkeletonModifier3D::_set_active(bool p_active) {
108 //
109 }
110
111 void SkeletonModifier3D::set_influence(real_t p_influence) {
112 influence = p_influence;
113 }
114
115 real_t SkeletonModifier3D::get_influence() const {
116 return influence;
117 }
118
119 void SkeletonModifier3D::process_modification(double p_delta) {
120 if (!is_inside_tree() || !active) {
121 return;
122 }
123 _process_modification(p_delta);
124 emit_signal(SNAME("modification_processed"));
125 }
126
127 void SkeletonModifier3D::_process_modification(double p_delta) {
128 if (GDVIRTUAL_CALL(_process_modification_with_delta, p_delta)) {
129 return;
130 }
131 #ifndef DISABLE_DEPRECATED
132 if (GDVIRTUAL_CALL(_process_modification)) {
133 return;
134 }
135 #endif // DISABLE_DEPRECATED
136 }
137
138 void SkeletonModifier3D::_notification(int p_what) {
139 switch (p_what) {
140 case NOTIFICATION_ENTER_TREE:
141 case NOTIFICATION_PARENTED: {
142 _update_skeleton();
143 } break;
144 case NOTIFICATION_EXIT_TREE:
145 case NOTIFICATION_UNPARENTED: {
146 _force_update_skeleton_skin();
147 } break;
148 }
149 }
150
151 void SkeletonModifier3D::_bind_methods() {
152 ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModifier3D::get_skeleton);
153
154 ClassDB::bind_method(D_METHOD("set_active", "active"), &SkeletonModifier3D::set_active);
155 ClassDB::bind_method(D_METHOD("is_active"), &SkeletonModifier3D::is_active);
156
157 ClassDB::bind_method(D_METHOD("set_influence", "influence"), &SkeletonModifier3D::set_influence);
158 ClassDB::bind_method(D_METHOD("get_influence"), &SkeletonModifier3D::get_influence);
159
160 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
161 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "influence", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_influence", "get_influence");
162
163 ADD_SIGNAL(MethodInfo("modification_processed"));
164 GDVIRTUAL_BIND(_process_modification_with_delta, "delta");
165 #ifndef DISABLE_DEPRECATED
166 GDVIRTUAL_BIND(_process_modification);
167 #endif
168
169 GDVIRTUAL_BIND(_skeleton_changed, "old_skeleton", "new_skeleton");
170 GDVIRTUAL_BIND(_validate_bone_names);
171
172 BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_X);
173 BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_X);
174 BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_Y);
175 BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_Y);
176 BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_Z);
177 BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_Z);
178
179 BIND_ENUM_CONSTANT(BONE_DIRECTION_PLUS_X);
180 BIND_ENUM_CONSTANT(BONE_DIRECTION_MINUS_X);
181 BIND_ENUM_CONSTANT(BONE_DIRECTION_PLUS_Y);
182 BIND_ENUM_CONSTANT(BONE_DIRECTION_MINUS_Y);
183 BIND_ENUM_CONSTANT(BONE_DIRECTION_PLUS_Z);
184 BIND_ENUM_CONSTANT(BONE_DIRECTION_MINUS_Z);
185 BIND_ENUM_CONSTANT(BONE_DIRECTION_FROM_PARENT);
186
187 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_NONE);
188 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_PLUS_X);
189 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_MINUS_X);
190 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_PLUS_Y);
191 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_MINUS_Y);
192 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_PLUS_Z);
193 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_MINUS_Z);
194 BIND_ENUM_CONSTANT(SECONDARY_DIRECTION_CUSTOM);
195
196 BIND_ENUM_CONSTANT(ROTATION_AXIS_X);
197 BIND_ENUM_CONSTANT(ROTATION_AXIS_Y);
198 BIND_ENUM_CONSTANT(ROTATION_AXIS_Z);
199 BIND_ENUM_CONSTANT(ROTATION_AXIS_ALL);
200 BIND_ENUM_CONSTANT(ROTATION_AXIS_CUSTOM);
201 }
202
203 Vector3 SkeletonModifier3D::get_vector_from_bone_axis(BoneAxis p_axis) {
204 Vector3 ret;
205 switch (p_axis) {
206 case BONE_AXIS_PLUS_X: {
207 ret = Vector3(1, 0, 0);
208 } break;
209 case BONE_AXIS_MINUS_X: {
210 ret = Vector3(-1, 0, 0);
211 } break;
212 case BONE_AXIS_PLUS_Y: {
213 ret = Vector3(0, 1, 0);
214 } break;
215 case BONE_AXIS_MINUS_Y: {
216 ret = Vector3(0, -1, 0);
217 } break;
218 case BONE_AXIS_PLUS_Z: {
219 ret = Vector3(0, 0, 1);
220 } break;
221 case BONE_AXIS_MINUS_Z: {
222 ret = Vector3(0, 0, -1);
223 } break;
224 }
225 return ret;
226 }
227
228 Vector3 SkeletonModifier3D::get_vector_from_axis(Vector3::Axis p_axis) {
229 Vector3 ret;
230 switch (p_axis) {
231 case Vector3::AXIS_X: {
232 ret = Vector3(1, 0, 0);
233 } break;
234 case Vector3::AXIS_Y: {
235 ret = Vector3(0, 1, 0);
236 } break;
237 case Vector3::AXIS_Z: {
238 ret = Vector3(0, 0, 1);
239 } break;
240 }
241 return ret;
242 }
243
244 Vector3::Axis SkeletonModifier3D::get_axis_from_bone_axis(BoneAxis p_axis) {
245 Vector3::Axis ret = Vector3::AXIS_X;
246 switch (p_axis) {
247 case BONE_AXIS_PLUS_X:
248 case BONE_AXIS_MINUS_X: {
249 ret = Vector3::AXIS_X;
250 } break;
251 case BONE_AXIS_PLUS_Y:
252 case BONE_AXIS_MINUS_Y: {
253 ret = Vector3::AXIS_Y;
254 } break;
255 case BONE_AXIS_PLUS_Z:
256 case BONE_AXIS_MINUS_Z: {
257 ret = Vector3::AXIS_Z;
258 } break;
259 }
260 return ret;
261 }
262
263 Vector3 SkeletonModifier3D::limit_length(const Vector3 &p_origin, const Vector3 &p_destination, float p_length) {
264 return p_origin + (p_destination - p_origin).normalized() * p_length;
265 }
266
267 Quaternion SkeletonModifier3D::get_local_pose_rotation(Skeleton3D *p_skeleton, int p_bone, const Quaternion &p_global_pose_rotation) {
268 int parent = p_skeleton->get_bone_parent(p_bone);
269 if (parent < 0) {
270 return p_global_pose_rotation;
271 }
272 return (p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion().inverse() * p_global_pose_rotation).normalized();
273 }
274
275 Quaternion SkeletonModifier3D::get_from_to_rotation(const Vector3 &p_from, const Vector3 &p_to, const Quaternion &p_prev_rot) {
276 if (Math::is_equal_approx((float)p_from.dot(p_to), -1.0f)) {
277 return p_prev_rot; // For preventing to glitch, checking dot for detecting flip is more accurate than checking cross.
278 }
279 Vector3 axis = p_from.cross(p_to);
280 if (axis.is_zero_approx()) {
281 return p_prev_rot;
282 }
283 float angle = p_from.angle_to(p_to);
284 if (Math::is_zero_approx(angle)) {
285 angle = 0.0;
286 }
287 return Quaternion(axis.normalized(), angle);
288 }
289
290 Quaternion SkeletonModifier3D::get_from_to_rotation_by_axis(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_axis) {
291 const double ALMOST_ONE = 1.0 - CMP_EPSILON;
292 double dot = p_from.dot(p_to);
293 if (dot > ALMOST_ONE) {
294 return Quaternion();
295 }
296 if (dot < -ALMOST_ONE) {
297 return Quaternion(p_axis, Math::PI);
298 }
299 double angle = p_from.angle_to(p_to);
300 Vector3 cross = p_from.cross(p_to);
301 if (std::signbit(cross.dot(p_axis))) {
302 angle = -angle;
303 }
304 return Quaternion(p_axis, angle);
305 }
306
307 Quaternion SkeletonModifier3D::get_swing(const Quaternion &p_rotation, const Vector3 &p_axis) {
308 if (p_axis.is_zero_approx()) {
309 return p_rotation;
310 }
311 Quaternion rot = p_rotation;
312 if (!rot.is_normalized()) {
313 rot.normalize();
314 }
315 Vector3 axis = p_axis.normalized();
316 const Vector3 v(rot.x, rot.y, rot.z);
317 const real_t proj_len = v.dot(axis);
318 const Vector3 twist_vec = axis * proj_len;
319 Quaternion twist(twist_vec.x, twist_vec.y, twist_vec.z, rot.w);
320 if (!twist.is_normalized()) {
321 if (Math::is_zero_approx(twist.length_squared())) {
322 return rot;
323 }
324 twist.normalize();
325 }
326 Quaternion swing = rot * twist.inverse();
327 swing.normalize();
328 return swing;
329 }
330
331 Vector3 SkeletonModifier3D::snap_vector_to_plane(const Vector3 &p_plane_normal, const Vector3 &p_vector) {
332 if (Math::is_zero_approx(p_plane_normal.length_squared())) {
333 return p_vector;
334 }
335 double length = p_vector.length();
336 Vector3 normalized_vec = p_vector.normalized();
337 Vector3 normal = p_plane_normal.normalized();
338 return normalized_vec.slide(normal) * length;
339 }
340
341 double SkeletonModifier3D::symmetrize_angle(double p_angle) {
342 double angle = Math::fposmod(p_angle, Math::TAU);
343 return angle > Math::PI ? angle - Math::TAU : angle;
344 }
345
346 double SkeletonModifier3D::get_roll_angle(const Quaternion &p_rotation, const Vector3 &p_roll_axis) {
347 // Ensure roll axis is normalized.
348 Vector3 roll_axis = p_roll_axis.normalized();
349
350 // Project the quaternion rotation onto the roll axis.
351 // This gives us the component of rotation around that axis.
352 double dot = p_rotation.x * roll_axis.x +
353 p_rotation.y * roll_axis.y +
354 p_rotation.z * roll_axis.z;
355
356 // Create a quaternion representing just the roll component.
357 Quaternion roll_component;
358 roll_component.x = roll_axis.x * dot;
359 roll_component.y = roll_axis.y * dot;
360 roll_component.z = roll_axis.z * dot;
361 roll_component.w = p_rotation.w;
362
363 // Normalize this component.
364 double length = roll_component.length();
365 if (length > CMP_EPSILON) {
366 roll_component = roll_component / length;
367 } else {
368 return 0.0;
369 }
370
371 // Extract the angle.
372 double angle = 2.0 * Math::acos(CLAMP(roll_component.w, -1.0, 1.0));
373
374 // Determine the sign.
375 double direction = (roll_component.x * roll_axis.x + roll_component.y * roll_axis.y + roll_component.z * roll_axis.z > 0) ? 1.0 : -1.0;
376
377 return symmetrize_angle(angle * direction);
378 }
379
380 Vector3 SkeletonModifier3D::get_projected_normal(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_point) {
381 // Get nearest normal vector to p_point from the infinite line p_a to p_b.
382 const Vector3 dir = p_b - p_a;
383 const real_t denom = dir.length_squared();
384 if (Math::is_zero_approx(denom)) {
385 return Vector3();
386 }
387 const Vector3 w = p_point - p_a;
388 const real_t t = w.dot(dir) / denom;
389 const Vector3 h = p_a + dir * t;
390 return (p_point - h).normalized();
391 }
392
393 SkeletonModifier3D::SkeletonModifier3D() {
394 }