copy_transform_modifier_3d.cpp 22 KB

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