AnimGraphFixture.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #pragma once
  9. #include "SystemComponentFixture.h"
  10. #include <EMotionFX/Source/MotionSet.h>
  11. #include <AzCore/Debug/TraceMessageBus.h>
  12. #include <AzCore/Outcome/Outcome.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <MCore/Source/Attribute.h>
  16. #include <Tests/TestAssetCode/AnimGraphFactory.h>
  17. namespace EMotionFX
  18. {
  19. class Actor;
  20. class ActorInstance;
  21. class AnimGraph;
  22. class AnimGraphStateMachine;
  23. class AnimGraphStateTransition;
  24. class AnimGraphInstance;
  25. class AnimGraphTimeCondition;
  26. class Transform;
  27. class AnimGraphFixture
  28. : public SystemComponentFixture
  29. , public AZ::Debug::TraceMessageBus::Handler
  30. {
  31. public:
  32. void SetUp() override;
  33. void TearDown() override;
  34. // Derived classes should override and construct the rest of the graph at this point.
  35. // They should call this base ConstructGraph to get the anim graph and root state machine created.
  36. virtual void ConstructGraph();
  37. virtual void ConstructActor();
  38. AZStd::string SerializeAnimGraph() const;
  39. // Evaluates the graph
  40. void Evaluate();
  41. const Transform& GetOutputTransform(uint32 nodeIndex = 0);
  42. void AddValueParameter(const AZ::TypeId& typeId, const AZStd::string& name);
  43. template <class ParamType, class InputType>
  44. void ParamSetValue(const AZStd::string& paramName, const InputType& value)
  45. {
  46. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(paramName);
  47. const AZ::u32 paramIndex = static_cast<AZ::u32>(parameterIndex.GetValue());
  48. MCore::Attribute* param = m_animGraphInstance->GetParameterValue(paramIndex);
  49. ParamType* typeParam = static_cast<ParamType*>(param);
  50. typeParam->SetValue(value);
  51. }
  52. // Helper functions for state machine construction (Works on m_rootStateMachine).
  53. AnimGraphStateTransition* AddTransition(AnimGraphNode* source, AnimGraphNode* target, float time);
  54. AnimGraphTimeCondition* AddTimeCondition(AnimGraphStateTransition* transition, float countDownTime);
  55. AnimGraphStateTransition* AddTransitionWithTimeCondition(AnimGraphNode* source, AnimGraphNode* target, float blendTime, float countDownTime);
  56. // Helper function for motion set construction (Works on m_motionSet).
  57. MotionSet::MotionEntry* AddMotionEntry(const AZStd::string& motionId, float motionMaxTime);
  58. // TraceMessageBus - Intercepting to prevent dialog popup in AnimGraphReferenceNodeWithNoContentsTest.
  59. virtual bool OnError(const char* /*window*/, const char* /*message*/) override { return true; }
  60. using SimulateFrameCallback = std::function<void(AnimGraphInstance*, /*time*/ float, /*timeDelta*/ float, /*frame*/ int)>;
  61. using SimulateCallback = std::function<void(AnimGraphInstance*)>;
  62. /**
  63. * Simulation helper with callbacks before and after starting the simulation as well as
  64. * callbakcs before and after the anim graph update.
  65. * Example: expectedFps = 60, fpsVariance = 10 -> actual framerate = [55, 65]
  66. * @param[in] simulationTime Simulation time in seconds.
  67. * @param[in] expectedFps is the targeted frame rate
  68. * @param[in] fpsVariance is the range in which the instabilities happen.
  69. */
  70. void Simulate(float simulationTime, float expectedFps, float fpsVariance,
  71. SimulateCallback preCallback,
  72. SimulateCallback postCallback,
  73. SimulateFrameCallback preUpdateCallback,
  74. SimulateFrameCallback postUpdateCallback);
  75. protected:
  76. AZStd::unique_ptr<Actor> m_actor;
  77. ActorInstance* m_actorInstance = nullptr;
  78. AZStd::unique_ptr<AnimGraph> m_animGraph;
  79. AnimGraphStateMachine* m_rootStateMachine = nullptr;
  80. AnimGraphInstance* m_animGraphInstance = nullptr;
  81. MotionSet* m_motionSet = nullptr;
  82. AZStd::unique_ptr<OneBlendTreeNodeAnimGraph> m_blendTreeAnimGraph;
  83. };
  84. }