BlendTreeRotationLimitNodeTests.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <gtest/gtest_prod.h>
  9. #include "AnimGraphFixture.h"
  10. #include <EMotionFX/Source/AnimGraph.h>
  11. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  12. #include <EMotionFX/Source/BlendTree.h>
  13. #include <EMotionFX/Source/BlendTreeFinalNode.h>
  14. #include <EMotionFX/Source/BlendTreeParameterNode.h>
  15. #include <EMotionFX/Source/EMotionFXManager.h>
  16. #include <EMotionFX/Source/BlendTreeGetTransformNode.h>
  17. #include <EMotionFX/Source/BlendTreeSetTransformNode.h>
  18. #include <EMotionFX/Source/AnimGraphBindPoseNode.h>
  19. #include <EMotionFX/Source/BlendTreeRotationMath2Node.h>
  20. #include <EMotionFX/Source/BlendTreeRotationLimitNode.h>
  21. #include <EMotionFX/Source/Node.h>
  22. #include <EMotionFX/Source/Skeleton.h>
  23. namespace EMotionFX
  24. {
  25. class BlendTreeRotationLimitNodeTests : public AnimGraphFixture
  26. {
  27. public:
  28. void ConstructGraph() override
  29. {
  30. AnimGraphFixture::ConstructGraph();
  31. m_blendTreeAnimGraph = AnimGraphFactory::Create<OneBlendTreeNodeAnimGraph>();
  32. m_rootStateMachine = m_blendTreeAnimGraph->GetRootStateMachine();
  33. m_blendTree = m_blendTreeAnimGraph->GetBlendTreeNode();
  34. AnimGraphBindPoseNode* bindPoseNode = aznew AnimGraphBindPoseNode();
  35. BlendTreeFinalNode* finalNode = aznew BlendTreeFinalNode();
  36. BlendTreeRotationLimitNode* testBlendTreeRotationLimitNode = aznew BlendTreeRotationLimitNode();
  37. m_getTransformNode = aznew BlendTreeGetTransformNode();
  38. m_rotationMathNode = aznew BlendTreeRotationMath2Node();
  39. m_setTransformNode = aznew BlendTreeSetTransformNode();
  40. m_rotationMathNode->SetMathFunction(EMotionFX::BlendTreeRotationMath2Node::MATHFUNCTION_INVERSE_MULTIPLY);
  41. testBlendTreeRotationLimitNode->SetRotationLimitsX(-180.0f, 180.0f);
  42. testBlendTreeRotationLimitNode->SetRotationLimitsY(-180.0f, 180.0f);
  43. testBlendTreeRotationLimitNode->SetRotationLimitsZ(-45.0f, 45.0f);
  44. testBlendTreeRotationLimitNode->SetTwistAxis(EMotionFX::ConstraintTransformRotationAngles::EAxis::AXIS_Z);
  45. m_blendTree->AddChildNode(bindPoseNode);
  46. m_blendTree->AddChildNode(m_getTransformNode);
  47. m_blendTree->AddChildNode(m_rotationMathNode);
  48. m_blendTree->AddChildNode(m_setTransformNode);
  49. m_blendTree->AddChildNode(testBlendTreeRotationLimitNode);
  50. m_blendTree->AddChildNode(finalNode);
  51. m_getTransformNode->AddConnection(bindPoseNode, AnimGraphBindPoseNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  52. m_setTransformNode->AddConnection(bindPoseNode, AnimGraphBindPoseNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  53. m_rotationMathNode->AddConnection(m_getTransformNode, BlendTreeGetTransformNode::OUTPUTPORT_ROTATION, BlendTreeRotationMath2Node::INPUTPORT_Y);
  54. testBlendTreeRotationLimitNode->AddConnection(m_rotationMathNode, BlendTreeRotationMath2Node::OUTPUTPORT_RESULT_QUATERNION, BlendTreeRotationLimitNode::INPUTPORT_ROTATION);
  55. m_setTransformNode->AddConnection(testBlendTreeRotationLimitNode, BlendTreeRotationLimitNode::OUTPUTPORT_RESULT_QUATERNION, BlendTreeSetTransformNode::INPUTPORT_ROTATION);
  56. finalNode->AddConnection(m_setTransformNode, BlendTreeSetTransformNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  57. m_blendTreeAnimGraph->InitAfterLoading();
  58. }
  59. void SetUp() override
  60. {
  61. AnimGraphFixture::SetUp();
  62. m_animGraphInstance->Destroy();
  63. m_animGraphInstance = m_blendTreeAnimGraph->GetAnimGraphInstance(m_actorInstance, m_motionSet);
  64. }
  65. BlendTree* m_blendTree = nullptr;
  66. BlendTreeGetTransformNode* m_getTransformNode = nullptr;
  67. BlendTreeRotationMath2Node* m_rotationMathNode = nullptr;
  68. BlendTreeSetTransformNode* m_setTransformNode = nullptr;
  69. };
  70. TEST_F(BlendTreeRotationLimitNodeTests, RotationLimitTest)
  71. {
  72. const char* firstNodeName = m_actor->GetSkeleton()->GetNode(0)->GetName();
  73. m_getTransformNode->SetJointName(firstNodeName);
  74. m_getTransformNode->InvalidateUniqueData(m_animGraphInstance);
  75. m_setTransformNode->SetJointName(firstNodeName);
  76. m_setTransformNode->InvalidateUniqueData(m_animGraphInstance);
  77. AZ::Quaternion expectedRotation = AZ::Quaternion::CreateRotationZ(MCore::Math::pi * 0.25f);
  78. AZ::Quaternion desiredRotation = AZ::Quaternion::CreateRotationZ(MCore::Math::pi * 0.5f);
  79. m_rotationMathNode->SetDefaultValue(desiredRotation);
  80. Evaluate();
  81. Transform outputRoot = GetOutputTransform();
  82. Transform expected = Transform::CreateIdentity();
  83. expected.Set(AZ::Vector3::CreateZero(), expectedRotation);
  84. bool success = AZ::IsClose(expected.m_rotation.GetW(), outputRoot.m_rotation.GetW(), 0.0001f);
  85. success = success && AZ::IsClose(expected.m_rotation.GetX(), outputRoot.m_rotation.GetX(), 0.0001f);
  86. success = success && AZ::IsClose(expected.m_rotation.GetY(), outputRoot.m_rotation.GetY(), 0.0001f);
  87. success = success && AZ::IsClose(expected.m_rotation.GetZ(), outputRoot.m_rotation.GetZ(), 0.0001f);
  88. ASSERT_TRUE(success);
  89. }
  90. } // end namespace EMotionFX