pivot_transform.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**************************************************************************/
  2. /* pivot_transform.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 "pivot_transform.h"
  31. #include "tools/import_utils.h"
  32. void PivotTransform::ReadTransformChain() {
  33. const FBXDocParser::PropertyTable *props = fbx_model->Props();
  34. const FBXDocParser::Model::RotOrder &rot = fbx_model->RotationOrder();
  35. const FBXDocParser::TransformInheritance &inheritType = fbx_model->InheritType();
  36. inherit_type = inheritType; // copy the inherit type we need it in the second step.
  37. print_verbose("Model: " + String(fbx_model->Name().c_str()) + " Has inherit type: " + itos(fbx_model->InheritType()));
  38. bool ok = false;
  39. raw_pre_rotation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "PreRotation", ok));
  40. if (ok) {
  41. pre_rotation = ImportUtils::EulerToQuaternion(rot, ImportUtils::deg2rad(raw_pre_rotation));
  42. print_verbose("valid pre_rotation: " + raw_pre_rotation + " euler conversion: " + (pre_rotation.get_euler() * (180 / Math_PI)));
  43. }
  44. raw_post_rotation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "PostRotation", ok));
  45. if (ok) {
  46. post_rotation = ImportUtils::EulerToQuaternion(FBXDocParser::Model::RotOrder_EulerXYZ, ImportUtils::deg2rad(raw_post_rotation));
  47. print_verbose("valid post_rotation: " + raw_post_rotation + " euler conversion: " + (pre_rotation.get_euler() * (180 / Math_PI)));
  48. }
  49. const Vector3 &RotationPivot = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "RotationPivot", ok));
  50. if (ok) {
  51. rotation_pivot = ImportUtils::FixAxisConversions(RotationPivot);
  52. }
  53. const Vector3 &RotationOffset = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "RotationOffset", ok));
  54. if (ok) {
  55. rotation_offset = ImportUtils::FixAxisConversions(RotationOffset);
  56. }
  57. const Vector3 &ScalingOffset = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "ScalingOffset", ok));
  58. if (ok) {
  59. scaling_offset = ImportUtils::FixAxisConversions(ScalingOffset);
  60. }
  61. const Vector3 &ScalingPivot = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "ScalingPivot", ok));
  62. if (ok) {
  63. scaling_pivot = ImportUtils::FixAxisConversions(ScalingPivot);
  64. }
  65. const Vector3 &Translation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "Lcl Translation", ok));
  66. if (ok) {
  67. translation = ImportUtils::FixAxisConversions(Translation);
  68. }
  69. raw_rotation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "Lcl Rotation", ok));
  70. if (ok) {
  71. rotation = ImportUtils::EulerToQuaternion(rot, ImportUtils::deg2rad(raw_rotation));
  72. }
  73. const Vector3 &Scaling = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "Lcl Scaling", ok));
  74. if (ok) {
  75. scaling = Scaling;
  76. }
  77. const Vector3 &GeometricScaling = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "GeometricScaling", ok));
  78. if (ok) {
  79. geometric_scaling = GeometricScaling;
  80. } else {
  81. geometric_scaling = Vector3(0, 0, 0);
  82. }
  83. const Vector3 &GeometricRotation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "GeometricRotation", ok));
  84. if (ok) {
  85. geometric_rotation = ImportUtils::EulerToQuaternion(rot, ImportUtils::deg2rad(GeometricRotation));
  86. } else {
  87. geometric_rotation = Quat();
  88. }
  89. const Vector3 &GeometricTranslation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "GeometricTranslation", ok));
  90. if (ok) {
  91. geometric_translation = ImportUtils::FixAxisConversions(GeometricTranslation);
  92. } else {
  93. geometric_translation = Vector3(0, 0, 0);
  94. }
  95. if (geometric_rotation != Quat()) {
  96. print_error("geometric rotation is unsupported!");
  97. //CRASH_COND(true);
  98. }
  99. if (!geometric_scaling.is_equal_approx(Vector3(1, 1, 1))) {
  100. print_error("geometric scaling is unsupported!");
  101. //CRASH_COND(true);
  102. }
  103. if (!geometric_translation.is_equal_approx(Vector3(0, 0, 0))) {
  104. print_error("geometric translation is unsupported.");
  105. //CRASH_COND(true);
  106. }
  107. }
  108. Transform PivotTransform::ComputeLocalTransform(Vector3 p_translation, Quat p_rotation, Vector3 p_scaling) const {
  109. Transform T, Roff, Rp, Soff, Sp, S;
  110. // Here I assume this is the operation which needs done.
  111. // Its WorldTransform * V
  112. // Origin pivots
  113. T.set_origin(p_translation);
  114. Roff.set_origin(rotation_offset);
  115. Rp.set_origin(rotation_pivot);
  116. Soff.set_origin(scaling_offset);
  117. Sp.set_origin(scaling_pivot);
  118. // Scaling node
  119. S.scale(p_scaling);
  120. // Rotation pivots
  121. Transform Rpre = Transform(pre_rotation);
  122. Transform R = Transform(p_rotation);
  123. Transform Rpost = Transform(post_rotation);
  124. return T * Roff * Rp * Rpre * R * Rpost.affine_inverse() * Rp.affine_inverse() * Soff * Sp * S * Sp.affine_inverse();
  125. }
  126. Transform PivotTransform::ComputeGlobalTransform(Transform t) const {
  127. Vector3 pos = t.origin;
  128. Vector3 scale = t.basis.get_scale();
  129. Quat rot = t.basis.get_rotation_quat();
  130. return ComputeGlobalTransform(pos, rot, scale);
  131. }
  132. Transform PivotTransform::ComputeLocalTransform(Transform t) const {
  133. Vector3 pos = t.origin;
  134. Vector3 scale = t.basis.get_scale();
  135. Quat rot = t.basis.get_rotation_quat();
  136. return ComputeLocalTransform(pos, rot, scale);
  137. }
  138. Transform PivotTransform::ComputeGlobalTransform(Vector3 p_translation, Quat p_rotation, Vector3 p_scaling) const {
  139. Transform T, Roff, Rp, Soff, Sp, S;
  140. // Here I assume this is the operation which needs done.
  141. // Its WorldTransform * V
  142. // Origin pivots
  143. T.set_origin(p_translation);
  144. Roff.set_origin(rotation_offset);
  145. Rp.set_origin(rotation_pivot);
  146. Soff.set_origin(scaling_offset);
  147. Sp.set_origin(scaling_pivot);
  148. // Scaling node
  149. S.scale(p_scaling);
  150. // Rotation pivots
  151. Transform Rpre = Transform(pre_rotation);
  152. Transform R = Transform(p_rotation);
  153. Transform Rpost = Transform(post_rotation);
  154. Transform parent_global_xform;
  155. Transform parent_local_scaling_m;
  156. if (parent_transform.is_valid()) {
  157. parent_global_xform = parent_transform->GlobalTransform;
  158. parent_local_scaling_m = parent_transform->Local_Scaling_Matrix;
  159. }
  160. Transform local_rotation_m, parent_global_rotation_m;
  161. Quat parent_global_rotation = parent_global_xform.basis.get_rotation_quat();
  162. parent_global_rotation_m.basis.set_quat(parent_global_rotation);
  163. local_rotation_m = Rpre * R * Rpost;
  164. //Basis parent_global_rotation = Basis(parent_global_xform.get_basis().get_rotation_quat().normalized());
  165. Transform local_shear_scaling, parent_shear_scaling, parent_shear_rotation, parent_shear_translation;
  166. Vector3 parent_translation = parent_global_xform.get_origin();
  167. parent_shear_translation.origin = parent_translation;
  168. parent_shear_rotation = parent_shear_translation.affine_inverse() * parent_global_xform;
  169. parent_shear_scaling = parent_global_rotation_m.affine_inverse() * parent_shear_rotation;
  170. local_shear_scaling = S;
  171. // Inherit type handler - we don't care about T here, just reordering RSrs etc.
  172. Transform global_rotation_scale;
  173. if (inherit_type == FBXDocParser::Transform_RrSs) {
  174. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_shear_scaling * local_shear_scaling;
  175. } else if (inherit_type == FBXDocParser::Transform_RSrs) {
  176. global_rotation_scale = parent_global_rotation_m * parent_shear_scaling * local_rotation_m * local_shear_scaling;
  177. } else if (inherit_type == FBXDocParser::Transform_Rrs) {
  178. Transform parent_global_shear_m_noLocal = parent_shear_scaling * parent_local_scaling_m.affine_inverse();
  179. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_global_shear_m_noLocal * local_shear_scaling;
  180. }
  181. Transform local_transform = T * Roff * Rp * Rpre * R * Rpost.affine_inverse() * Rp.affine_inverse() * Soff * Sp * S * Sp.affine_inverse();
  182. //Transform local_translation_pivoted = Transform(Basis(), LocalTransform.origin);
  183. // manual hack to force SSC not to be compensated for - until we can handle it properly with tests
  184. return parent_global_xform * local_transform;
  185. }
  186. void PivotTransform::ComputePivotTransform() {
  187. Transform T, Roff, Rp, Soff, Sp, S;
  188. // Here I assume this is the operation which needs done.
  189. // Its WorldTransform * V
  190. // Origin pivots
  191. T.set_origin(translation);
  192. Roff.set_origin(rotation_offset);
  193. Rp.set_origin(rotation_pivot);
  194. Soff.set_origin(scaling_offset);
  195. Sp.set_origin(scaling_pivot);
  196. // Scaling node
  197. if (!scaling.is_equal_approx(Vector3())) {
  198. S.scale(scaling);
  199. } else {
  200. S.scale(Vector3(1, 1, 1));
  201. }
  202. Local_Scaling_Matrix = S; // copy for when node / child is looking for the value of this.
  203. // Rotation pivots
  204. Transform Rpre = Transform(pre_rotation);
  205. Transform R = Transform(rotation);
  206. Transform Rpost = Transform(post_rotation);
  207. Transform parent_global_xform;
  208. Transform parent_local_scaling_m;
  209. if (parent_transform.is_valid()) {
  210. parent_global_xform = parent_transform->GlobalTransform;
  211. parent_local_scaling_m = parent_transform->Local_Scaling_Matrix;
  212. }
  213. Transform local_rotation_m, parent_global_rotation_m;
  214. Quat parent_global_rotation = parent_global_xform.basis.get_rotation_quat();
  215. parent_global_rotation_m.basis.set_quat(parent_global_rotation);
  216. local_rotation_m = Rpre * R * Rpost;
  217. //Basis parent_global_rotation = Basis(parent_global_xform.get_basis().get_rotation_quat().normalized());
  218. Transform local_shear_scaling, parent_shear_scaling, parent_shear_rotation, parent_shear_translation;
  219. Vector3 parent_translation = parent_global_xform.get_origin();
  220. parent_shear_translation.origin = parent_translation;
  221. parent_shear_rotation = parent_shear_translation.affine_inverse() * parent_global_xform;
  222. parent_shear_scaling = parent_global_rotation_m.affine_inverse() * parent_shear_rotation;
  223. local_shear_scaling = S;
  224. // Inherit type handler - we don't care about T here, just reordering RSrs etc.
  225. Transform global_rotation_scale;
  226. if (inherit_type == FBXDocParser::Transform_RrSs) {
  227. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_shear_scaling * local_shear_scaling;
  228. } else if (inherit_type == FBXDocParser::Transform_RSrs) {
  229. global_rotation_scale = parent_global_rotation_m * parent_shear_scaling * local_rotation_m * local_shear_scaling;
  230. } else if (inherit_type == FBXDocParser::Transform_Rrs) {
  231. Transform parent_global_shear_m_noLocal = parent_shear_scaling * parent_local_scaling_m.inverse();
  232. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_global_shear_m_noLocal * local_shear_scaling;
  233. }
  234. LocalTransform = Transform();
  235. LocalTransform = T * Roff * Rp * Rpre * R * Rpost.affine_inverse() * Rp.affine_inverse() * Soff * Sp * S * Sp.affine_inverse();
  236. ERR_FAIL_COND_MSG(LocalTransform.basis.determinant() == 0, "invalid scale reset");
  237. Transform local_translation_pivoted = Transform(Basis(), LocalTransform.origin);
  238. GlobalTransform = Transform();
  239. //GlobalTransform = parent_global_xform * LocalTransform;
  240. Transform global_origin = Transform(Basis(), parent_translation);
  241. GlobalTransform = (global_origin * local_translation_pivoted) * global_rotation_scale;
  242. ImportUtils::debug_xform("local xform calculation", LocalTransform);
  243. print_verbose("scale of node: " + S.basis.get_scale_local());
  244. print_verbose("---------------------------------------------------------------");
  245. }
  246. void PivotTransform::Execute() {
  247. ReadTransformChain();
  248. ComputePivotTransform();
  249. ImportUtils::debug_xform("global xform: ", GlobalTransform);
  250. computed_global_xform = true;
  251. }