copy_transform_modifier_3d.cpp 20 KB

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