Vector3ParameterTests.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <EMotionFX/Source/AnimGraph.h>
  9. #include <EMotionFX/Source/AnimGraphBindPoseNode.h>
  10. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  11. #include <EMotionFX/Source/BlendTree.h>
  12. #include <EMotionFX/Source/BlendTreeParameterNode.h>
  13. #include <EMotionFX/Source/BlendTreeTwoLinkIKNode.h>
  14. #include <EMotionFX/Source/EMotionFXManager.h>
  15. #include <EMotionFX/Source/Parameter/Vector3Parameter.h>
  16. #include <MCore/Source/ReflectionSerializer.h>
  17. #include <Tests/AnimGraphFixture.h>
  18. namespace EMotionFX
  19. {
  20. class Vector3ParameterFixture
  21. : public AnimGraphFixture
  22. , public ::testing::WithParamInterface<AZ::Vector3>
  23. {
  24. public:
  25. void ConstructGraph() override
  26. {
  27. AnimGraphFixture::ConstructGraph();
  28. m_param = GetParam();
  29. m_blendTreeAnimGraph = AnimGraphFactory::Create<OneBlendTreeNodeAnimGraph>();
  30. m_rootStateMachine = m_blendTreeAnimGraph->GetRootStateMachine();
  31. m_blendTree = m_blendTreeAnimGraph->GetBlendTreeNode();
  32. AddParameter<Vector3Parameter, AZ::Vector3>("vec3Test", m_param);
  33. /*
  34. +------------+
  35. |bindPoseNode+---+
  36. +------------+ |
  37. +-->+-------------+ +---------+
  38. +-----------+ |twoLinkIKNode+---->+finalNode|
  39. |m_paramNode+------>+-------------+ +---------+
  40. +-----------+
  41. */
  42. BlendTreeFinalNode* finalNode = aznew BlendTreeFinalNode();
  43. AnimGraphBindPoseNode* bindPoseNode = aznew AnimGraphBindPoseNode();
  44. m_paramNode = aznew BlendTreeParameterNode();
  45. // Using two link IK Node because its GoalPos input port uses Vector3
  46. m_twoLinkIKNode = aznew BlendTreeTwoLinkIKNode();
  47. m_blendTree->AddChildNode(finalNode);
  48. m_blendTree->AddChildNode(m_twoLinkIKNode);
  49. m_blendTree->AddChildNode(bindPoseNode);
  50. m_blendTree->AddChildNode(m_paramNode);
  51. m_twoLinkIKNode->AddConnection(bindPoseNode, AnimGraphBindPoseNode::PORTID_OUTPUT_POSE, BlendTreeTwoLinkIKNode::PORTID_INPUT_POSE);
  52. finalNode->AddConnection(m_twoLinkIKNode, BlendTreeTwoLinkIKNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  53. m_blendTreeAnimGraph->InitAfterLoading();
  54. }
  55. void SetUp() override
  56. {
  57. AnimGraphFixture::SetUp();
  58. m_animGraphInstance->Destroy();
  59. m_animGraphInstance = m_blendTreeAnimGraph->GetAnimGraphInstance(m_actorInstance, m_motionSet);
  60. }
  61. template <class paramType, class inputType>
  62. void ParamSetValue(const AZStd::string& paramName, const inputType& value)
  63. {
  64. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(paramName);
  65. MCore::Attribute* param = m_animGraphInstance->GetParameterValue(parameterIndex.GetValue());
  66. paramType* typeParam = static_cast<paramType*>(param);
  67. typeParam->SetValue(value);
  68. }
  69. protected:
  70. BlendTree* m_blendTree = nullptr;
  71. BlendTreeParameterNode* m_paramNode = nullptr;
  72. BlendTreeTwoLinkIKNode* m_twoLinkIKNode = nullptr;
  73. AZ::Vector3 m_param;
  74. private:
  75. template<class ParameterType, class ValueType>
  76. void AddParameter(const AZStd::string& name, const ValueType& defaultValue)
  77. {
  78. ParameterType* parameter = aznew ParameterType();
  79. parameter->SetName(name);
  80. parameter->SetDefaultValue(defaultValue);
  81. m_blendTreeAnimGraph->AddParameter(parameter);
  82. }
  83. };
  84. TEST_P(Vector3ParameterFixture, ParameterOutputsCorrectVector3Floats)
  85. {
  86. // Parameter node needs to connect to another node, otherwise it will not be updated
  87. m_twoLinkIKNode->AddConnection(m_paramNode, aznumeric_caster(m_paramNode->FindOutputPortIndex("vec3Test")), BlendTreeTwoLinkIKNode::PORTID_INPUT_GOALPOS);
  88. GetEMotionFX().Update(1.0f / 60.0f);
  89. // Check correct output for vector3 parameter.
  90. const AZ::Vector3& vec3TestParam = m_paramNode->GetOutputVector3(m_animGraphInstance,
  91. m_paramNode->FindOutputPortIndex("vec3Test"))->GetValue();
  92. EXPECT_FLOAT_EQ(vec3TestParam.GetX(), m_param.GetX()) << "Vector3 X value should be the same as expected vector3 X value.";
  93. EXPECT_FLOAT_EQ(vec3TestParam.GetY(), m_param.GetY()) << "Vector3 Y value should be the same as expected vector3 Y value.";
  94. EXPECT_FLOAT_EQ(vec3TestParam.GetZ(), m_param.GetZ()) << "Vector3 Z value should be the same as expected vector3 Z value.";
  95. };
  96. TEST_P(Vector3ParameterFixture, Vec3SetValueOutputsCorrectVector3Floats)
  97. {
  98. m_twoLinkIKNode->AddConnection(m_paramNode, aznumeric_caster(m_paramNode->FindOutputPortIndex("vec3Test")), BlendTreeTwoLinkIKNode::PORTID_INPUT_GOALPOS);
  99. GetEMotionFX().Update(1.0f / 60.0f);
  100. // Shuffle the vector3 parameter values to check changing vector3 values will be processed correctly.
  101. ParamSetValue<MCore::AttributeVector3, AZ::Vector3>("vec3Test", AZ::Vector3(m_param.GetY(), m_param.GetZ(), m_param.GetX()));
  102. GetEMotionFX().Update(1.0f / 60.0f);
  103. const AZ::Vector3& vec3TestParam = m_paramNode->GetOutputVector3(m_animGraphInstance,
  104. m_paramNode->FindOutputPortIndex("vec3Test"))->GetValue();
  105. EXPECT_FLOAT_EQ(vec3TestParam.GetX(), m_param.GetY()) << "Input vector3 X value should be the same as expected vector3 Y value.";
  106. EXPECT_FLOAT_EQ(vec3TestParam.GetY(), m_param.GetZ()) << "Input vector3 Y value should be the same as expected vector3 Z value.";
  107. EXPECT_FLOAT_EQ(vec3TestParam.GetZ(), m_param.GetX()) << "Input vector3 Z value should be the same as expected vector3 X value.";
  108. };
  109. std::vector<AZ::Vector3> Vector3ParameterTestData
  110. {
  111. AZ::Vector3(0.0f, 0.0f, 0.0f),
  112. AZ::Vector3(1.0f, 0.5f, -0.5f),
  113. AZ::Vector3(AZ::Constants::FloatMax, -AZ::Constants::FloatMax, AZ::Constants::FloatEpsilon)
  114. };
  115. INSTANTIATE_TEST_CASE_P(Vector3Parameter_ValidOutputTests,
  116. Vector3ParameterFixture,
  117. ::testing::ValuesIn(Vector3ParameterTestData)
  118. );
  119. } // end namespace EMotionFX