3
0

Vector2ToVector3CompatibilityTests.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "AnimGraphFixture.h"
  9. #include <EMotionFX/Source/AnimGraph.h>
  10. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  11. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  12. #include <EMotionFX/Source/BlendTree.h>
  13. #include <EMotionFX/Source/BlendTreeBlendNNode.h>
  14. #include <EMotionFX/Source/BlendTreeVector3DecomposeNode.h>
  15. #include <EMotionFX/Source/BlendTreeVector2DecomposeNode.h>
  16. #include <EMotionFX/Source/EMotionFXManager.h>
  17. #include <EMotionFX/Source/MotionData/NonUniformMotionData.h>
  18. #include <EMotionFX/Source/MotionSet.h>
  19. #include <EMotionFX/Source/MotionInstance.h>
  20. #include <EMotionFX/Source/Motion.h>
  21. #include <EMotionFX/Source/Parameter/ParameterFactory.h>
  22. #include <EMotionFX/Source/Parameter/Vector2Parameter.h>
  23. #include <EMotionFX/Source/Parameter/Vector3Parameter.h>
  24. #include <EMotionFX/Source/Parameter/ValueParameter.h>
  25. #include <EMotionFX/Source/BlendTreeParameterNode.h>
  26. #include <AzCore/std/containers/vector.h>
  27. namespace EMotionFX
  28. {
  29. class Vector2ToVector3CompatibilityTests : public AnimGraphFixture
  30. {
  31. public:
  32. void ConstructGraph() override
  33. {
  34. AnimGraphFixture::ConstructGraph();
  35. m_blendTreeAnimGraph = AnimGraphFactory::Create<OneBlendTreeNodeAnimGraph>();
  36. m_rootStateMachine = m_blendTreeAnimGraph->GetRootStateMachine();
  37. m_blendTree = m_blendTreeAnimGraph->GetBlendTreeNode();
  38. m_blendNNode = aznew BlendTreeBlendNNode();
  39. m_blendTree->AddChildNode(m_blendNNode);
  40. BlendTreeFinalNode* finalNode = aznew BlendTreeFinalNode();
  41. m_blendTree->AddChildNode(finalNode);
  42. finalNode->AddUnitializedConnection(m_blendNNode, BlendTreeBlendNNode::PORTID_OUTPUT_POSE, BlendTreeFinalNode::PORTID_INPUT_POSE);
  43. const uint16 motionNodeCount = 3;
  44. for (uint16 i = 0; i < motionNodeCount; ++i)
  45. {
  46. AnimGraphMotionNode* motionNode = aznew AnimGraphMotionNode();
  47. m_blendTree->AddChildNode(motionNode);
  48. m_blendNNode->AddUnitializedConnection(motionNode, AnimGraphMotionNode::PORTID_OUTPUT_POSE, i);
  49. // The motion set keeps track of motions by their name. Each motion
  50. // within the motion set must have a unique name.
  51. const AZStd::string motionId = AZStd::string::format("testSkeletalMotion%i", i);
  52. Motion* motion = aznew Motion(motionId.c_str());
  53. motion->SetMotionData(aznew NonUniformMotionData());
  54. motion->GetMotionData()->SetDuration(1.0f);
  55. MotionSet::MotionEntry* motionEntry = aznew MotionSet::MotionEntry(motion->GetName(), motion->GetName(), motion);
  56. m_motionSet->AddMotionEntry(motionEntry);
  57. motionNode->AddMotionId(motionId.c_str());
  58. }
  59. {
  60. Parameter* parameter = ParameterFactory::Create(azrtti_typeid<Vector3Parameter>());
  61. parameter->SetName("parameter_vector3_test");
  62. m_blendTreeAnimGraph->AddParameter(parameter);
  63. }
  64. {
  65. Parameter* parameter = ParameterFactory::Create(azrtti_typeid<Vector2Parameter>());
  66. parameter->SetName("parameter_vector2_test");
  67. m_blendTreeAnimGraph->AddParameter(parameter);
  68. }
  69. BlendTreeParameterNode* parameterNode = aznew BlendTreeParameterNode();
  70. m_blendTree->AddChildNode(parameterNode);
  71. m_vector2DecomposeNode = aznew BlendTreeVector2DecomposeNode();
  72. m_blendTree->AddChildNode(m_vector2DecomposeNode);
  73. m_vector2DecomposeNode->AddUnitializedConnection(parameterNode, 0, BlendTreeVector2DecomposeNode::INPUTPORT_VECTOR);
  74. m_vector3DecomposeNode = aznew BlendTreeVector3DecomposeNode();
  75. m_blendTree->AddChildNode(m_vector3DecomposeNode);
  76. m_vector3DecomposeNode->AddUnitializedConnection(parameterNode, 1, BlendTreeVector3DecomposeNode::INPUTPORT_VECTOR);
  77. m_blendNNode->AddUnitializedConnection(m_vector3DecomposeNode, BlendTreeVector3DecomposeNode::OUTPUTPORT_X, BlendTreeBlendNNode::INPUTPORT_WEIGHT);
  78. m_blendTreeAnimGraph->InitAfterLoading();
  79. }
  80. void SetUp() override
  81. {
  82. AnimGraphFixture::SetUp();
  83. m_animGraphInstance->Destroy();
  84. m_animGraphInstance = m_blendTreeAnimGraph->GetAnimGraphInstance(m_actorInstance, m_motionSet);
  85. }
  86. BlendTreeBlendNNode* m_blendNNode = nullptr;
  87. BlendTree* m_blendTree = nullptr;
  88. BlendTreeVector3DecomposeNode* m_vector3DecomposeNode = nullptr;
  89. BlendTreeVector2DecomposeNode* m_vector2DecomposeNode = nullptr;
  90. };
  91. TEST_F(Vector2ToVector3CompatibilityTests, Evaluation)
  92. {
  93. AZ::Outcome<size_t> vector2ParamIndexOutcome = m_blendTreeAnimGraph->FindValueParameterIndexByName("parameter_vector2_test");
  94. ASSERT_TRUE(vector2ParamIndexOutcome.IsSuccess());
  95. AZ::Outcome<size_t> vector3ParamIndexOutcome = m_blendTreeAnimGraph->FindValueParameterIndexByName("parameter_vector3_test");
  96. ASSERT_TRUE(vector3ParamIndexOutcome.IsSuccess());
  97. MCore::AttributeVector2* testVector2Parameter = static_cast<MCore::AttributeVector2*>(m_animGraphInstance->GetParameterValue(static_cast<uint32>(vector2ParamIndexOutcome.GetValue())));
  98. testVector2Parameter->SetValue(AZ::Vector2(-1.0f, 0.5f));
  99. MCore::AttributeVector3* testVector3Parameter = static_cast<MCore::AttributeVector3*>(m_animGraphInstance->GetParameterValue(static_cast<uint32>(vector3ParamIndexOutcome.GetValue())));
  100. testVector3Parameter->SetValue(AZ::Vector3(1.0f, 2.5f, 3.5f));
  101. Evaluate();
  102. MCore::AttributeFloat* attributeFloatX = m_vector3DecomposeNode->GetOutputFloat(m_animGraphInstance, BlendTreeVector3DecomposeNode::OUTPUTPORT_X);
  103. ASSERT_TRUE(attributeFloatX);
  104. MCore::AttributeFloat* attributeFloatY = m_vector3DecomposeNode->GetOutputFloat(m_animGraphInstance, BlendTreeVector3DecomposeNode::OUTPUTPORT_Y);
  105. ASSERT_TRUE(attributeFloatY);
  106. MCore::AttributeFloat* attributeFloatZ = m_vector3DecomposeNode->GetOutputFloat(m_animGraphInstance, BlendTreeVector3DecomposeNode::OUTPUTPORT_Z);
  107. ASSERT_TRUE(attributeFloatZ);
  108. MCore::AttributeFloat* attributeFloatX1 = m_vector2DecomposeNode->GetOutputFloat(m_animGraphInstance, BlendTreeVector2DecomposeNode::OUTPUTPORT_X);
  109. ASSERT_TRUE(attributeFloatX1);
  110. MCore::AttributeFloat* attributeFloatY1 = m_vector2DecomposeNode->GetOutputFloat(m_animGraphInstance, BlendTreeVector2DecomposeNode::OUTPUTPORT_Y);
  111. ASSERT_TRUE(attributeFloatY1);
  112. const AZ::Vector2& testParameterVector2Value = testVector2Parameter->GetValue();
  113. const AZ::Vector3 testParameterVector3Value(testVector3Parameter->GetValue());
  114. const float tolerance = 0.001f;
  115. float outX = attributeFloatX->GetValue();
  116. float outY = attributeFloatY->GetValue();
  117. float outZ = attributeFloatZ->GetValue();
  118. AZ::Vector3 decomposedValuesVector3(outX, outY, outZ);
  119. AZ::Vector3 expectedDecomposedValuesVector3(testParameterVector2Value.GetX(), testParameterVector2Value.GetY(), 0.0f);
  120. float differenceLength = static_cast<float>((expectedDecomposedValuesVector3 - decomposedValuesVector3).GetLength());
  121. ASSERT_TRUE(AZ::IsClose(0, differenceLength, tolerance));
  122. m_blendNNode->RemoveConnection(m_vector3DecomposeNode, BlendTreeVector3DecomposeNode::OUTPUTPORT_X, BlendTreeBlendNNode::INPUTPORT_WEIGHT);
  123. m_blendNNode->AddConnection(m_vector2DecomposeNode, BlendTreeVector3DecomposeNode::OUTPUTPORT_X, BlendTreeBlendNNode::INPUTPORT_WEIGHT);
  124. Evaluate();
  125. outX = attributeFloatX1->GetValue();
  126. outY = attributeFloatY1->GetValue();
  127. AZ::Vector2 decomposedValuesVector2(outX, outY);
  128. AZ::Vector2 expectedDecomposedValuesVector2(testParameterVector3Value.GetX(), testParameterVector3Value.GetY());
  129. differenceLength = static_cast<float>((expectedDecomposedValuesVector2 - decomposedValuesVector2).GetLength());
  130. ASSERT_TRUE(AZ::IsClose(0, differenceLength, tolerance));
  131. }
  132. } // end namespace EMotionFX