pivot_transform.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*************************************************************************/
  2. /* pivot_transform.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. } else {
  77. scaling = Vector3(1, 1, 1);
  78. }
  79. const Vector3 &GeometricScaling = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "GeometricScaling", ok));
  80. if (ok) {
  81. geometric_scaling = GeometricScaling;
  82. } else {
  83. geometric_scaling = Vector3(1, 1, 1);
  84. }
  85. const Vector3 &GeometricRotation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "GeometricRotation", ok));
  86. if (ok) {
  87. geometric_rotation = ImportUtils::EulerToQuaternion(rot, ImportUtils::deg2rad(GeometricRotation));
  88. } else {
  89. geometric_rotation = Quat();
  90. }
  91. const Vector3 &GeometricTranslation = ImportUtils::safe_import_vector3(FBXDocParser::PropertyGet<Vector3>(props, "GeometricTranslation", ok));
  92. if (ok) {
  93. geometric_translation = ImportUtils::FixAxisConversions(GeometricTranslation);
  94. } else {
  95. geometric_translation = Vector3(0, 0, 0);
  96. }
  97. if (geometric_rotation != Quat()) {
  98. print_error("geometric rotation is unsupported!");
  99. //CRASH_COND(true);
  100. }
  101. if (!geometric_scaling.is_equal_approx(Vector3(1, 1, 1))) {
  102. print_error("geometric scaling is unsupported!");
  103. //CRASH_COND(true);
  104. }
  105. if (!geometric_translation.is_equal_approx(Vector3(0, 0, 0))) {
  106. print_error("geometric translation is unsupported.");
  107. //CRASH_COND(true);
  108. }
  109. }
  110. Transform PivotTransform::ComputeLocalTransform(Vector3 p_translation, Quat p_rotation, Vector3 p_scaling) const {
  111. Transform T, Roff, Rp, Soff, Sp, S;
  112. // Here I assume this is the operation which needs done.
  113. // Its WorldTransform * V
  114. // Origin pivots
  115. T.set_origin(p_translation);
  116. Roff.set_origin(rotation_offset);
  117. Rp.set_origin(rotation_pivot);
  118. Soff.set_origin(scaling_offset);
  119. Sp.set_origin(scaling_pivot);
  120. // Scaling node
  121. S.scale(p_scaling);
  122. // Rotation pivots
  123. Transform Rpre = Transform(pre_rotation);
  124. Transform R = Transform(p_rotation);
  125. Transform Rpost = Transform(post_rotation);
  126. return T * Roff * Rp * Rpre * R * Rpost.affine_inverse() * Rp.affine_inverse() * Soff * Sp * S * Sp.affine_inverse();
  127. }
  128. Transform PivotTransform::ComputeGlobalTransform(Transform t) const {
  129. Vector3 pos = t.origin;
  130. Vector3 scale = t.basis.get_scale();
  131. Quat rot = t.basis.get_rotation_quat();
  132. return ComputeGlobalTransform(pos, rot, scale);
  133. }
  134. Transform PivotTransform::ComputeLocalTransform(Transform t) const {
  135. Vector3 pos = t.origin;
  136. Vector3 scale = t.basis.get_scale();
  137. Quat rot = t.basis.get_rotation_quat();
  138. return ComputeLocalTransform(pos, rot, scale);
  139. }
  140. Transform PivotTransform::ComputeGlobalTransform(Vector3 p_translation, Quat p_rotation, Vector3 p_scaling) const {
  141. Transform T, Roff, Rp, Soff, Sp, S;
  142. // Here I assume this is the operation which needs done.
  143. // Its WorldTransform * V
  144. // Origin pivots
  145. T.set_origin(p_translation);
  146. Roff.set_origin(rotation_offset);
  147. Rp.set_origin(rotation_pivot);
  148. Soff.set_origin(scaling_offset);
  149. Sp.set_origin(scaling_pivot);
  150. // Scaling node
  151. S.scale(p_scaling);
  152. // Rotation pivots
  153. Transform Rpre = Transform(pre_rotation);
  154. Transform R = Transform(p_rotation);
  155. Transform Rpost = Transform(post_rotation);
  156. Transform parent_global_xform;
  157. Transform parent_local_scaling_m;
  158. if (parent_transform.is_valid()) {
  159. parent_global_xform = parent_transform->GlobalTransform;
  160. parent_local_scaling_m = parent_transform->Local_Scaling_Matrix;
  161. }
  162. Transform local_rotation_m, parent_global_rotation_m;
  163. Quat parent_global_rotation = parent_global_xform.basis.get_rotation_quat();
  164. parent_global_rotation_m.basis.set_quat(parent_global_rotation);
  165. local_rotation_m = Rpre * R * Rpost;
  166. //Basis parent_global_rotation = Basis(parent_global_xform.get_basis().get_rotation_quat().normalized());
  167. Transform local_shear_scaling, parent_shear_scaling, parent_shear_rotation, parent_shear_translation;
  168. Vector3 parent_translation = parent_global_xform.get_origin();
  169. parent_shear_translation.origin = parent_translation;
  170. parent_shear_rotation = parent_shear_translation.affine_inverse() * parent_global_xform;
  171. parent_shear_scaling = parent_global_rotation_m.affine_inverse() * parent_shear_rotation;
  172. local_shear_scaling = S;
  173. // Inherit type handler - we don't care about T here, just reordering RSrs etc.
  174. Transform global_rotation_scale;
  175. if (inherit_type == FBXDocParser::Transform_RrSs) {
  176. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_shear_scaling * local_shear_scaling;
  177. } else if (inherit_type == FBXDocParser::Transform_RSrs) {
  178. global_rotation_scale = parent_global_rotation_m * parent_shear_scaling * local_rotation_m * local_shear_scaling;
  179. } else if (inherit_type == FBXDocParser::Transform_Rrs) {
  180. Transform parent_global_shear_m_noLocal = parent_shear_scaling * parent_local_scaling_m.affine_inverse();
  181. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_global_shear_m_noLocal * local_shear_scaling;
  182. }
  183. Transform local_transform = T * Roff * Rp * Rpre * R * Rpost.affine_inverse() * Rp.affine_inverse() * Soff * Sp * S * Sp.affine_inverse();
  184. //Transform local_translation_pivoted = Transform(Basis(), LocalTransform.origin);
  185. ERR_FAIL_COND_V_MSG(local_transform.basis.determinant() == 0, Transform(), "Det == 0 prevented in scene file");
  186. // manual hack to force SSC not to be compensated for - until we can handle it properly with tests
  187. return parent_global_xform * local_transform;
  188. }
  189. void PivotTransform::ComputePivotTransform() {
  190. Transform T, Roff, Rp, Soff, Sp, S;
  191. // Here I assume this is the operation which needs done.
  192. // Its WorldTransform * V
  193. // Origin pivots
  194. T.set_origin(translation);
  195. Roff.set_origin(rotation_offset);
  196. Rp.set_origin(rotation_pivot);
  197. Soff.set_origin(scaling_offset);
  198. Sp.set_origin(scaling_pivot);
  199. // Scaling node
  200. if (!scaling.is_equal_approx(Vector3())) {
  201. S.scale(scaling);
  202. } else {
  203. S.scale(Vector3(1, 1, 1));
  204. }
  205. Local_Scaling_Matrix = S; // copy for when node / child is looking for the value of this.
  206. // Rotation pivots
  207. Transform Rpre = Transform(pre_rotation);
  208. Transform R = Transform(rotation);
  209. Transform Rpost = Transform(post_rotation);
  210. Transform parent_global_xform;
  211. Transform parent_local_scaling_m;
  212. if (parent_transform.is_valid()) {
  213. parent_global_xform = parent_transform->GlobalTransform;
  214. parent_local_scaling_m = parent_transform->Local_Scaling_Matrix;
  215. }
  216. Transform local_rotation_m, parent_global_rotation_m;
  217. Quat parent_global_rotation = parent_global_xform.basis.get_rotation_quat();
  218. parent_global_rotation_m.basis.set_quat(parent_global_rotation);
  219. local_rotation_m = Rpre * R * Rpost;
  220. //Basis parent_global_rotation = Basis(parent_global_xform.get_basis().get_rotation_quat().normalized());
  221. Transform local_shear_scaling, parent_shear_scaling, parent_shear_rotation, parent_shear_translation;
  222. Vector3 parent_translation = parent_global_xform.get_origin();
  223. parent_shear_translation.origin = parent_translation;
  224. parent_shear_rotation = parent_shear_translation.affine_inverse() * parent_global_xform;
  225. parent_shear_scaling = parent_global_rotation_m.affine_inverse() * parent_shear_rotation;
  226. local_shear_scaling = S;
  227. // Inherit type handler - we don't care about T here, just reordering RSrs etc.
  228. Transform global_rotation_scale;
  229. if (inherit_type == FBXDocParser::Transform_RrSs) {
  230. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_shear_scaling * local_shear_scaling;
  231. } else if (inherit_type == FBXDocParser::Transform_RSrs) {
  232. global_rotation_scale = parent_global_rotation_m * parent_shear_scaling * local_rotation_m * local_shear_scaling;
  233. } else if (inherit_type == FBXDocParser::Transform_Rrs) {
  234. Transform parent_global_shear_m_noLocal = parent_shear_scaling * parent_local_scaling_m.inverse();
  235. global_rotation_scale = parent_global_rotation_m * local_rotation_m * parent_global_shear_m_noLocal * local_shear_scaling;
  236. }
  237. LocalTransform = Transform();
  238. LocalTransform = T * Roff * Rp * Rpre * R * Rpost.affine_inverse() * Rp.affine_inverse() * Soff * Sp * S * Sp.affine_inverse();
  239. ERR_FAIL_COND_MSG(LocalTransform.basis.determinant() == 0, "invalid scale reset");
  240. Transform local_translation_pivoted = Transform(Basis(), LocalTransform.origin);
  241. GlobalTransform = Transform();
  242. //GlobalTransform = parent_global_xform * LocalTransform;
  243. Transform global_origin = Transform(Basis(), parent_translation);
  244. GlobalTransform = (global_origin * local_translation_pivoted) * global_rotation_scale;
  245. ImportUtils::debug_xform("local xform calculation", LocalTransform);
  246. print_verbose("scale of node: " + S.basis.get_scale_local());
  247. print_verbose("---------------------------------------------------------------");
  248. }
  249. void PivotTransform::Execute() {
  250. ReadTransformChain();
  251. ComputePivotTransform();
  252. ImportUtils::debug_xform("global xform: ", GlobalTransform);
  253. if (LocalTransform.basis.determinant() == 0) {
  254. print_error("Serious det == 0!");
  255. }
  256. if (GlobalTransform.basis.determinant() == 0) {
  257. print_error("Serious! node has det == 0!");
  258. }
  259. computed_global_xform = true;
  260. }