Transform.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "rparticle/Utilities/Transform.h"
  2. NS_RRP_BEGIN
  3. #if REDREAM_EDITOR
  4. static cocos2d::Node* ContentLayerNode = nullptr;
  5. void Transform::SetContentLayerNode(cocos2d::Node* node)
  6. {
  7. ContentLayerNode = node;
  8. }
  9. Matrix4x4f Transform::GetContentToWorldMatrix()
  10. {
  11. return ContentLayerNode ? GetLocalToWorldMatrix(ContentLayerNode) : Matrix4x4f::IDENTITY;
  12. }
  13. #endif
  14. Matrix4x4f Transform::GetLocalToWorldMatrix (const cocos2d::Node* node)
  15. {
  16. Matrix4x4f m;
  17. GetLocalToWorldMatrix(node, m);
  18. return m;
  19. }
  20. const Matrix4x4f& Transform::GetLocalToWorldMatrix (const cocos2d::Node* node, Matrix4x4f& m)
  21. {
  22. Quaternionf rot; Vector3f pos;
  23. // GetPositionAndRotation(node, pos, rot);
  24. // m.SetTR (pos, rot);
  25. #if REDREAM_EDITOR
  26. m = node->getNodeToParentTransform(ContentLayerNode);
  27. return m;
  28. #else
  29. m = node->getNodeToWorldTransform();
  30. return m;
  31. #endif
  32. }
  33. Matrix4x4f Transform::GetLocalToWorldMatrixNoScale (const cocos2d::Node* node)
  34. {
  35. Matrix4x4f m;
  36. GetLocalToWorldMatrixNoScale(node, m);
  37. return m;
  38. }
  39. const Matrix4x4f& Transform::GetLocalToWorldMatrixNoScale (const cocos2d::Node* node, Matrix4x4f& m)
  40. {
  41. Quaternionf rot; Vector3f pos;
  42. // GetPositionAndRotation(node, pos, rot);
  43. // m.SetTR (pos, rot);
  44. #if REDREAM_EDITOR
  45. m = node->getNodeToParentTransform(ContentLayerNode);
  46. m.decompose(nullptr, &rot, &pos);
  47. #else
  48. m = node->getNodeToWorldTransform();
  49. m.decompose(nullptr, &rot, &pos);
  50. #endif
  51. m.SetTR(pos, rot);
  52. return m;
  53. }
  54. Matrix3x3f Transform::GetWorldScale (const cocos2d::Node* node)
  55. {
  56. Matrix3x3f invRotation;
  57. QuaternionToMatrix (Inverse (GetRotation (node)), invRotation);
  58. Matrix3x3f scaleAndRotation = GetWorldRotationAndScale (node);
  59. return invRotation * scaleAndRotation;
  60. }
  61. Vector3f Transform::GetWorldScaleLossy (const cocos2d::Node* node)
  62. {
  63. Matrix3x3f rot = GetWorldScale (node);
  64. return Vector3f (rot.Get (0, 0), rot.Get (1, 1), rot.Get (2, 2));
  65. }
  66. Vector3f Transform::GetPosition (const cocos2d::Node* node)
  67. {
  68. Vector3f pos;
  69. #if REDREAM_EDITOR
  70. auto m = node->getNodeToParentTransform(ContentLayerNode);
  71. pos = m.GetPosition();
  72. #else
  73. auto m = node->getNodeToWorldTransform();
  74. pos = m.GetPosition();
  75. #endif
  76. return pos;
  77. }
  78. Matrix3x3f Transform::GetWorldRotationAndScale (const cocos2d::Node* node)
  79. {
  80. Matrix3x3f scale;
  81. //scale.SetScale (m_LocalScale);
  82. scale.SetScale (Vector3f(node->getScaleX(), node->getScaleY(), node->getScaleZ()));
  83. Matrix3x3f rotation;
  84. //QuaternionToMatrix (m_LocalRotation, rotation);
  85. QuaternionToMatrix (node->getRotationQuat(), rotation);
  86. //Transform* parent = GetParent ();
  87. auto parent = node->getParent();
  88. #if REDREAM_EDITOR
  89. if (parent == ContentLayerNode) return rotation * scale;
  90. #endif
  91. if (parent)
  92. {
  93. ///@TODO optimize: Special case multiplication
  94. Matrix3x3f parentTransform = GetWorldRotationAndScale (parent);
  95. return parentTransform * rotation * scale;
  96. }
  97. return rotation * scale;
  98. }
  99. Quaternionf Transform::GetRotation (const cocos2d::Node* node)
  100. {
  101. //Quaternionf worldRot = m_LocalRotation;
  102. Quaternionf worldRot = node->getRotationQuat();
  103. //Transform* cur = GetParent ();
  104. auto cur = node->getParent();
  105. while (cur)
  106. {
  107. //worldRot = cur->m_LocalRotation * worldRot;
  108. //cur = cur->GetParent ();
  109. worldRot = cur->getRotationQuat() * worldRot;
  110. cur = cur->getParent();
  111. #if REDREAM_EDITOR
  112. if (cur == ContentLayerNode) break;
  113. #endif
  114. }
  115. return worldRot;
  116. }
  117. NS_RRP_END