Debian Codegraph @

scene/3d/copy_transform_modifier_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* copy_transform_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 "copy_transform_modifier_3d.h"
32
33 bool CopyTransformModifier3D::_set(const StringName &p_path, const Variant &p_value) {
34 String path = p_path;
35
36 if (path.begins_with("settings/")) {
37 int which = path.get_slicec('/', 1).to_int();
38 String what = path.get_slicec('/', 2);
39 ERR_FAIL_INDEX_V(which, (int)settings.size(), false);
40
41 if (what == "copy") {
42 set_copy_flags(which, static_cast<BitField<TransformFlag>>((int)p_value));
43 } else if (what == "axes") {
44 set_axis_flags(which, static_cast<BitField<AxisFlag>>((int)p_value));
45 } else if (what == "invert") {
46 set_invert_flags(which, static_cast<BitField<AxisFlag>>((int)p_value));
47 } else if (what == "relative") {
48 set_relative(which, p_value);
49 } else if (what == "additive") {
50 set_additive(which, p_value);
51 } else {
52 return false;
53 }
54 }
55 return true;
56 }
57
58 bool CopyTransformModifier3D::_get(const StringName &p_path, Variant &r_ret) const {
59 String path = p_path;
60
61 if (path.begins_with("settings/")) {
62 int which = path.get_slicec('/', 1).to_int();
63 String what = path.get_slicec('/', 2);
64 ERR_FAIL_INDEX_V(which, (int)settings.size(), false);
65
66 if (what == "copy") {
67 r_ret = (int)get_copy_flags(which);
68 } else if (what == "axes") {
69 r_ret = (int)get_axis_flags(which);
70 } else if (what == "invert") {
71 r_ret = (int)get_invert_flags(which);
72 } else if (what == "relative") {
73 r_ret = is_relative(which);
74 } else if (what == "additive") {
75 r_ret = is_additive(which);
76 } else {
77 return false;
78 }
79 }
80 return true;
81 }
82
83 void CopyTransformModifier3D::_get_property_list(List<PropertyInfo> *p_list) const {
84 BoneConstraint3D::get_property_list(p_list);
85
86 LocalVector<PropertyInfo> props;
87
88 for (uint32_t i = 0; i < settings.size(); i++) {
89 String path = "settings/" + itos(i) + "/";
90 props.push_back(PropertyInfo(Variant::INT, path + "copy", PROPERTY_HINT_FLAGS, "Position,Rotation,Scale"));
91 props.push_back(PropertyInfo(Variant::INT, path + "axes", PROPERTY_HINT_FLAGS, "X,Y,Z"));
92 props.push_back(PropertyInfo(Variant::INT, path + "invert", PROPERTY_HINT_FLAGS, "X,Y,Z"));
93 props.push_back(PropertyInfo(Variant::BOOL, path + "relative"));
94 props.push_back(PropertyInfo(Variant::BOOL, path + "additive"));
95 }
96
97 for (PropertyInfo &p : props) {
98 _validate_dynamic_prop(p);
99 p_list->push_back(p);
100 }
101 }
102
103 void CopyTransformModifier3D::_validate_dynamic_prop(PropertyInfo &p_property) const {
104 PackedStringArray split = p_property.name.split("/");
105 if (split.size() > 2 && split[0] == "settings") {
106 int which = split[1].to_int();
107 if (split[2].begins_with("relative") && get_reference_type(which) != REFERENCE_TYPE_BONE) {
108 p_property.usage = PROPERTY_USAGE_NONE;
109 }
110 }
111 }
112
113 void CopyTransformModifier3D::_validate_setting(int p_index) {
114 settings[p_index] = memnew(CopyTransform3DSetting);
115 }
116
117 void CopyTransformModifier3D::set_copy_flags(int p_index, BitField<TransformFlag> p_copy_flags) {
118 ERR_FAIL_INDEX(p_index, (int)settings.size());
119 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
120 setting->copy_flags = p_copy_flags;
121 notify_property_list_changed();
122 }
123
124 BitField<CopyTransformModifier3D::TransformFlag> CopyTransformModifier3D::get_copy_flags(int p_index) const {
125 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
126 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
127 return setting->copy_flags;
128 }
129
130 void CopyTransformModifier3D::set_axis_flags(int p_index, BitField<AxisFlag> p_axis_flags) {
131 ERR_FAIL_INDEX(p_index, (int)settings.size());
132 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
133 setting->axis_flags = p_axis_flags;
134 notify_property_list_changed();
135 }
136
137 BitField<CopyTransformModifier3D::AxisFlag> CopyTransformModifier3D::get_axis_flags(int p_index) const {
138 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
139 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
140 return setting->axis_flags;
141 }
142
143 void CopyTransformModifier3D::set_invert_flags(int p_index, BitField<AxisFlag> p_axis_flags) {
144 ERR_FAIL_INDEX(p_index, (int)settings.size());
145 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
146 setting->invert_flags = p_axis_flags;
147 notify_property_list_changed();
148 }
149
150 BitField<CopyTransformModifier3D::AxisFlag> CopyTransformModifier3D::get_invert_flags(int p_index) const {
151 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
152 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
153 return setting->invert_flags;
154 }
155
156 void CopyTransformModifier3D::set_copy_position(int p_index, bool p_enabled) {
157 ERR_FAIL_INDEX(p_index, (int)settings.size());
158 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
159 if (p_enabled) {
160 setting->copy_flags.set_flag(TRANSFORM_FLAG_POSITION);
161 } else {
162 setting->copy_flags.clear_flag(TRANSFORM_FLAG_POSITION);
163 }
164 }
165
166 bool CopyTransformModifier3D::is_position_copying(int p_index) const {
167 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
168 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
169 return setting->copy_flags.has_flag(TRANSFORM_FLAG_POSITION);
170 }
171
172 void CopyTransformModifier3D::set_copy_rotation(int p_index, bool p_enabled) {
173 ERR_FAIL_INDEX(p_index, (int)settings.size());
174 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
175 if (p_enabled) {
176 setting->copy_flags.set_flag(TRANSFORM_FLAG_ROTATION);
177 } else {
178 setting->copy_flags.clear_flag(TRANSFORM_FLAG_ROTATION);
179 }
180 }
181
182 bool CopyTransformModifier3D::is_rotation_copying(int p_index) const {
183 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
184 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
185 return setting->copy_flags.has_flag(TRANSFORM_FLAG_ROTATION);
186 }
187
188 void CopyTransformModifier3D::set_copy_scale(int p_index, bool p_enabled) {
189 ERR_FAIL_INDEX(p_index, (int)settings.size());
190 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
191 if (p_enabled) {
192 setting->copy_flags.set_flag(TRANSFORM_FLAG_SCALE);
193 } else {
194 setting->copy_flags.clear_flag(TRANSFORM_FLAG_SCALE);
195 }
196 }
197
198 bool CopyTransformModifier3D::is_scale_copying(int p_index) const {
199 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
200 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
201 return setting->copy_flags.has_flag(TRANSFORM_FLAG_SCALE);
202 }
203
204 void CopyTransformModifier3D::set_axis_x_enabled(int p_index, bool p_enabled) {
205 ERR_FAIL_INDEX(p_index, (int)settings.size());
206 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
207 if (p_enabled) {
208 setting->axis_flags.set_flag(AXIS_FLAG_X);
209 } else {
210 setting->axis_flags.clear_flag(AXIS_FLAG_X);
211 }
212 }
213
214 bool CopyTransformModifier3D::is_axis_x_enabled(int p_index) const {
215 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
216 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
217 return setting->axis_flags.has_flag(AXIS_FLAG_X);
218 }
219
220 void CopyTransformModifier3D::set_axis_y_enabled(int p_index, bool p_enabled) {
221 ERR_FAIL_INDEX(p_index, (int)settings.size());
222 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
223 if (p_enabled) {
224 setting->axis_flags.set_flag(AXIS_FLAG_Y);
225 } else {
226 setting->axis_flags.clear_flag(AXIS_FLAG_Y);
227 }
228 }
229
230 bool CopyTransformModifier3D::is_axis_y_enabled(int p_index) const {
231 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
232 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
233 return setting->axis_flags.has_flag(AXIS_FLAG_Y);
234 }
235
236 void CopyTransformModifier3D::set_axis_z_enabled(int p_index, bool p_enabled) {
237 ERR_FAIL_INDEX(p_index, (int)settings.size());
238 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
239 if (p_enabled) {
240 setting->axis_flags.set_flag(AXIS_FLAG_Z);
241 } else {
242 setting->axis_flags.clear_flag(AXIS_FLAG_Z);
243 }
244 }
245
246 bool CopyTransformModifier3D::is_axis_z_enabled(int p_index) const {
247 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
248 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
249 return setting->axis_flags.has_flag(AXIS_FLAG_Z);
250 }
251
252 void CopyTransformModifier3D::set_axis_x_inverted(int p_index, bool p_enabled) {
253 ERR_FAIL_INDEX(p_index, (int)settings.size());
254 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
255 if (p_enabled) {
256 setting->invert_flags.set_flag(AXIS_FLAG_X);
257 } else {
258 setting->invert_flags.clear_flag(AXIS_FLAG_X);
259 }
260 }
261
262 bool CopyTransformModifier3D::is_axis_x_inverted(int p_index) const {
263 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
264 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
265 return setting->invert_flags.has_flag(AXIS_FLAG_X);
266 }
267
268 void CopyTransformModifier3D::set_axis_y_inverted(int p_index, bool p_enabled) {
269 ERR_FAIL_INDEX(p_index, (int)settings.size());
270 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
271 if (p_enabled) {
272 setting->invert_flags.set_flag(AXIS_FLAG_Y);
273 } else {
274 setting->invert_flags.clear_flag(AXIS_FLAG_Y);
275 }
276 }
277
278 bool CopyTransformModifier3D::is_axis_y_inverted(int p_index) const {
279 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
280 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
281 return setting->invert_flags.has_flag(AXIS_FLAG_Y);
282 }
283
284 void CopyTransformModifier3D::set_axis_z_inverted(int p_index, bool p_enabled) {
285 ERR_FAIL_INDEX(p_index, (int)settings.size());
286 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
287 if (p_enabled) {
288 setting->invert_flags.set_flag(AXIS_FLAG_Z);
289 } else {
290 setting->invert_flags.clear_flag(AXIS_FLAG_Z);
291 }
292 }
293
294 bool CopyTransformModifier3D::is_axis_z_inverted(int p_index) const {
295 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
296 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
297 return setting->invert_flags.has_flag(AXIS_FLAG_Z);
298 }
299
300 void CopyTransformModifier3D::set_relative(int p_index, bool p_enabled) {
301 ERR_FAIL_INDEX(p_index, (int)settings.size());
302 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
303 setting->relative = p_enabled;
304 }
305
306 bool CopyTransformModifier3D::is_relative(int p_index) const {
307 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
308 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
309 return setting->is_relative();
310 }
311
312 void CopyTransformModifier3D::set_additive(int p_index, bool p_enabled) {
313 ERR_FAIL_INDEX(p_index, (int)settings.size());
314 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
315 setting->additive = p_enabled;
316 }
317
318 bool CopyTransformModifier3D::is_additive(int p_index) const {
319 ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
320 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
321 return setting->additive;
322 }
323
324 void CopyTransformModifier3D::_bind_methods() {
325 ClassDB::bind_method(D_METHOD("set_copy_flags", "index", "copy_flags"), &CopyTransformModifier3D::set_copy_flags);
326 ClassDB::bind_method(D_METHOD("get_copy_flags", "index"), &CopyTransformModifier3D::get_copy_flags);
327 ClassDB::bind_method(D_METHOD("set_axis_flags", "index", "axis_flags"), &CopyTransformModifier3D::set_axis_flags);
328 ClassDB::bind_method(D_METHOD("get_axis_flags", "index"), &CopyTransformModifier3D::get_axis_flags);
329 ClassDB::bind_method(D_METHOD("set_invert_flags", "index", "axis_flags"), &CopyTransformModifier3D::set_invert_flags);
330 ClassDB::bind_method(D_METHOD("get_invert_flags", "index"), &CopyTransformModifier3D::get_invert_flags);
331
332 ClassDB::bind_method(D_METHOD("set_copy_position", "index", "enabled"), &CopyTransformModifier3D::set_copy_position);
333 ClassDB::bind_method(D_METHOD("is_position_copying", "index"), &CopyTransformModifier3D::is_position_copying);
334 ClassDB::bind_method(D_METHOD("set_copy_rotation", "index", "enabled"), &CopyTransformModifier3D::set_copy_rotation);
335 ClassDB::bind_method(D_METHOD("is_rotation_copying", "index"), &CopyTransformModifier3D::is_rotation_copying);
336 ClassDB::bind_method(D_METHOD("set_copy_scale", "index", "enabled"), &CopyTransformModifier3D::set_copy_scale);
337 ClassDB::bind_method(D_METHOD("is_scale_copying", "index"), &CopyTransformModifier3D::is_scale_copying);
338
339 ClassDB::bind_method(D_METHOD("set_axis_x_enabled", "index", "enabled"), &CopyTransformModifier3D::set_axis_x_enabled);
340 ClassDB::bind_method(D_METHOD("is_axis_x_enabled", "index"), &CopyTransformModifier3D::is_axis_x_enabled);
341 ClassDB::bind_method(D_METHOD("set_axis_y_enabled", "index", "enabled"), &CopyTransformModifier3D::set_axis_y_enabled);
342 ClassDB::bind_method(D_METHOD("is_axis_y_enabled", "index"), &CopyTransformModifier3D::is_axis_y_enabled);
343 ClassDB::bind_method(D_METHOD("set_axis_z_enabled", "index", "enabled"), &CopyTransformModifier3D::set_axis_z_enabled);
344 ClassDB::bind_method(D_METHOD("is_axis_z_enabled", "index"), &CopyTransformModifier3D::is_axis_z_enabled);
345
346 ClassDB::bind_method(D_METHOD("set_axis_x_inverted", "index", "enabled"), &CopyTransformModifier3D::set_axis_x_inverted);
347 ClassDB::bind_method(D_METHOD("is_axis_x_inverted", "index"), &CopyTransformModifier3D::is_axis_x_inverted);
348 ClassDB::bind_method(D_METHOD("set_axis_y_inverted", "index", "enabled"), &CopyTransformModifier3D::set_axis_y_inverted);
349 ClassDB::bind_method(D_METHOD("is_axis_y_inverted", "index"), &CopyTransformModifier3D::is_axis_y_inverted);
350 ClassDB::bind_method(D_METHOD("set_axis_z_inverted", "index", "enabled"), &CopyTransformModifier3D::set_axis_z_inverted);
351 ClassDB::bind_method(D_METHOD("is_axis_z_inverted", "index"), &CopyTransformModifier3D::is_axis_z_inverted);
352
353 ClassDB::bind_method(D_METHOD("set_relative", "index", "enabled"), &CopyTransformModifier3D::set_relative);
354 ClassDB::bind_method(D_METHOD("is_relative", "index"), &CopyTransformModifier3D::is_relative);
355 ClassDB::bind_method(D_METHOD("set_additive", "index", "enabled"), &CopyTransformModifier3D::set_additive);
356 ClassDB::bind_method(D_METHOD("is_additive", "index"), &CopyTransformModifier3D::is_additive);
357
358 ADD_ARRAY_COUNT("Settings", "setting_count", "set_setting_count", "get_setting_count", "settings/");
359
360 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_POSITION);
361 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_ROTATION);
362 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_SCALE);
363 BIND_BITFIELD_FLAG(TRANSFORM_FLAG_ALL);
364
365 BIND_BITFIELD_FLAG(AXIS_FLAG_X);
366 BIND_BITFIELD_FLAG(AXIS_FLAG_Y);
367 BIND_BITFIELD_FLAG(AXIS_FLAG_Z);
368 BIND_BITFIELD_FLAG(AXIS_FLAG_ALL);
369 }
370
371 void CopyTransformModifier3D::_process_constraint_by_bone(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, int p_reference_bone, float p_amount) {
372 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
373 Transform3D destination = p_skeleton->get_bone_pose(p_reference_bone);
374 if (setting->is_relative()) {
375 Vector3 scl_relative = destination.basis.get_scale() / p_skeleton->get_bone_rest(p_reference_bone).basis.get_scale();
376 destination.basis = p_skeleton->get_bone_rest(p_reference_bone).basis.get_rotation_quaternion().inverse() * destination.basis.get_rotation_quaternion();
377 destination.basis.scale_local(scl_relative);
378 destination.origin = destination.origin - p_skeleton->get_bone_rest(p_reference_bone).origin;
379 }
380 _process_copy(p_index, p_skeleton, p_apply_bone, destination, p_amount);
381 }
382
383 void CopyTransformModifier3D::_process_constraint_by_node(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, const NodePath &p_reference_node, float p_amount) {
384 Node3D *nd = Object::cast_to<Node3D>(get_node_or_null(p_reference_node));
385 if (!nd) {
386 return;
387 }
388 Transform3D skel_tr = p_skeleton->get_global_transform_interpolated();
389 int parent = p_skeleton->get_bone_parent(p_apply_bone);
390 if (parent >= 0) {
391 skel_tr = skel_tr * p_skeleton->get_bone_global_pose(parent);
392 }
393 Transform3D dest_tr = nd->get_global_transform_interpolated();
394 Transform3D reference_dest = skel_tr.affine_inverse() * dest_tr;
395 _process_copy(p_index, p_skeleton, p_apply_bone, reference_dest, p_amount);
396 }
397
398 void CopyTransformModifier3D::_process_copy(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, const Transform3D &p_destination, float p_amount) {
399 CopyTransform3DSetting *setting = static_cast<CopyTransform3DSetting *>(settings[p_index]);
400
401 Transform3D destination = p_destination;
402 Vector3 dest_pos = destination.origin;
403 Quaternion dest_rot = destination.basis.get_rotation_quaternion();
404 Vector3 dest_scl = destination.basis.get_scale();
405
406 // Mask pos and scale.
407 for (int i = 0; i < 3; i++) {
408 if (!setting->axis_flags.has_flag(static_cast<AxisFlag>(1 << i))) {
409 dest_pos[i] = 0.0;
410 dest_scl[i] = 1.0;
411 }
412 }
413
414 // Mask rot.
415 switch (static_cast<int>(setting->axis_flags)) {
416 case 0: {
417 dest_rot = Quaternion();
418 } break;
419 case AXIS_FLAG_X: {
420 Vector3 axis = get_vector_from_axis(Vector3::AXIS_X);
421 dest_rot = Quaternion(axis, get_roll_angle(dest_rot, axis));
422 } break;
423 case AXIS_FLAG_Y: {
424 Vector3 axis = get_vector_from_axis(Vector3::AXIS_Y);
425 dest_rot = Quaternion(axis, get_roll_angle(dest_rot, axis));
426 } break;
427 case AXIS_FLAG_Z: {
428 Vector3 axis = get_vector_from_axis(Vector3::AXIS_Z);
429 dest_rot = Quaternion(axis, get_roll_angle(dest_rot, axis));
430 } break;
431 case AXIS_FLAG_X | AXIS_FLAG_Y: {
432 Vector3 axis = get_vector_from_axis(Vector3::AXIS_Z);
433 dest_rot = dest_rot * Quaternion(axis, get_roll_angle(dest_rot, axis)).inverse();
434 } break;
435 case AXIS_FLAG_Y | AXIS_FLAG_Z: {
436 Vector3 axis = get_vector_from_axis(Vector3::AXIS_X);
437 dest_rot = dest_rot * Quaternion(axis, get_roll_angle(dest_rot, axis)).inverse();
438 } break;
439 case AXIS_FLAG_Z | AXIS_FLAG_X: {
440 Vector3 axis = get_vector_from_axis(Vector3::AXIS_Y);
441 dest_rot = dest_rot * Quaternion(axis, get_roll_angle(dest_rot, axis)).inverse();
442 } break;
443 case AXIS_FLAG_ALL: {
444 } break;
445 }
446
447 // Process inversion.
448 for (int i = 0; i < 3; i++) {
449 AxisFlag axis = static_cast<AxisFlag>(1 << i);
450 if (setting->axis_flags.has_flag(axis) && setting->invert_flags.has_flag(axis)) {
451 dest_pos[i] *= -1;
452 dest_rot[i] *= -1;
453 dest_scl[i] = 1.0 / dest_scl[i];
454 }
455 }
456 dest_rot.normalize();
457
458 if (setting->additive) {
459 destination.origin = p_skeleton->get_bone_pose_position(p_apply_bone) + dest_pos;
460 destination.basis = p_skeleton->get_bone_pose_rotation(p_apply_bone) * Basis(dest_rot);
461 destination.basis.scale_local(p_skeleton->get_bone_pose_scale(p_apply_bone) * dest_scl);
462 } else if (setting->is_relative()) {
463 Transform3D rest = p_skeleton->get_bone_rest(p_apply_bone);
464 destination.origin = rest.origin + dest_pos;
465 destination.basis = rest.basis.get_rotation_quaternion() * Basis(dest_rot);
466 destination.basis.scale_local(rest.basis.get_scale() * dest_scl);
467 } else {
468 destination.origin = dest_pos;
469 destination.basis = Basis(dest_rot);
470 destination.basis.scale_local(dest_scl);
471 }
472
473 // Process interpolation depends on the amount.
474 destination = p_skeleton->get_bone_pose(p_apply_bone).interpolate_with(destination, p_amount);
475
476 // Apply transform depends on the element mask.
477 if (setting->copy_flags.has_flag(TRANSFORM_FLAG_POSITION)) {
478 p_skeleton->set_bone_pose_position(p_apply_bone, destination.origin);
479 }
480 if (setting->copy_flags.has_flag(TRANSFORM_FLAG_ROTATION)) {
481 p_skeleton->set_bone_pose_rotation(p_apply_bone, destination.basis.get_rotation_quaternion());
482 }
483 if (setting->copy_flags.has_flag(TRANSFORM_FLAG_SCALE)) {
484 p_skeleton->set_bone_pose_scale(p_apply_bone, destination.basis.get_scale());
485 }
486 }
487
488 CopyTransformModifier3D::~CopyTransformModifier3D() {
489 clear_settings();
490 }