MotionMatchingInstance.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <AzCore/Math/Vector3.h>
  10. #include <AzCore/Memory/Memory.h>
  11. #include <AzCore/RTTI/RTTI.h>
  12. #include <EMotionFX/Source/EMotionFXConfig.h>
  13. #include <Feature.h>
  14. #include <TrajectoryHistory.h>
  15. #include <TrajectoryQuery.h>
  16. #include <MotionMatching/MotionMatchingBus.h>
  17. namespace AZ
  18. {
  19. class ReflectContext;
  20. }
  21. namespace EMotionFX
  22. {
  23. class ActorInstance;
  24. class Motion;
  25. }
  26. namespace EMotionFX::MotionMatching
  27. {
  28. class MotionMatchingData;
  29. //! The instance is where everything comes together. It stores the trajectory history, the trajectory query along with the query vector, knows about the
  30. //! last lowest cost frame frame index and stores the time of the animation that the instance is currently playing. It is responsible for motion extraction,
  31. //! blending towards a new frame in the motion capture database in case the algorithm found a better matching frame and executes the actual search.
  32. class EMFX_API MotionMatchingInstance
  33. : public DebugDrawRequestBus::Handler
  34. {
  35. public:
  36. AZ_RTTI(MotionMatchingInstance, "{1ED03AD8-0FB2-431B-AF01-02F7E930EB73}")
  37. AZ_CLASS_ALLOCATOR_DECL
  38. MotionMatchingInstance() = default;
  39. virtual ~MotionMatchingInstance();
  40. struct EMFX_API InitSettings
  41. {
  42. ActorInstance* m_actorInstance = nullptr;
  43. MotionMatchingData* m_data = nullptr;
  44. };
  45. void Init(const InitSettings& settings);
  46. // DebugDrawRequestBus::Handler overrides
  47. void DebugDraw(AzFramework::DebugDisplayRequests& debugDisplay) override;
  48. void Update(
  49. float timePassedInSeconds,
  50. const AZ::Vector3& targetPos,
  51. const AZ::Vector3& targetFacingDir,
  52. bool useTargetFacingDir,
  53. TrajectoryQuery::EMode mode,
  54. float pathRadius,
  55. float pathSpeed);
  56. void PostUpdate(float timeDelta);
  57. void Output(Pose& outputPose);
  58. MotionInstance* GetMotionInstance() const { return m_motionInstance; }
  59. ActorInstance* GetActorInstance() const { return m_actorInstance; }
  60. MotionMatchingData* GetData() const { return m_data; }
  61. size_t GetLowestCostFrameIndex() const { return m_lowestCostFrameIndex; }
  62. void SetLowestCostSearchFrequency(float frequency) { m_lowestCostSearchFrequency = frequency; }
  63. float GetNewMotionTime() const { return m_newMotionTime; }
  64. //! Get the cached trajectory feature.
  65. //! The trajectory feature is searched in the feature schema used in the current instance at init time.
  66. FeatureTrajectory* GetTrajectoryFeature() const { return m_cachedTrajectoryFeature; }
  67. const TrajectoryQuery& GetTrajectoryQuery() const { return m_trajectoryQuery; }
  68. const TrajectoryHistory& GetTrajectoryHistory() const { return m_trajectoryHistory; }
  69. const Transform& GetMotionExtractionDelta() const { return m_motionExtractionDelta; }
  70. private:
  71. MotionInstance* CreateMotionInstance() const;
  72. void DebugDrawQueryPose(AzFramework::DebugDisplayRequests& debugDisplay, bool drawPose, bool drawVelocities) const;
  73. void SamplePose(MotionInstance* motionInstance, Pose& outputPose);
  74. void SamplePose(Motion* motion, Pose& outputPose, float sampleTime) const;
  75. size_t FindLowestCostFrameIndex(const Feature::QueryVectorContext& queryVectorContext, const Feature::FrameCostContext& frameCostContext);
  76. MotionMatchingData* m_data = nullptr;
  77. ActorInstance* m_actorInstance = nullptr;
  78. Pose m_blendSourcePose;
  79. Pose m_blendTargetPose;
  80. Pose m_queryPose; //! Input query pose for the motion matching search.
  81. MotionInstance* m_motionInstance = nullptr;
  82. MotionInstance* m_prevMotionInstance = nullptr;
  83. Transform m_motionExtractionDelta = Transform::CreateIdentity();
  84. QueryVector m_queryVector; //!< The input query features to be compared to every entry/row in the feature matrix with the motion matching search.
  85. /// Buffers used for the broad-phase KD-tree search.
  86. QueryVector m_kdTreeQueryVector; //!< The input query for only the features that are present in the KD-tree.
  87. AZStd::vector<size_t> m_nearestFrames; //!< Stores the nearest matching frames / search result from the KD-tree.
  88. FeatureTrajectory* m_cachedTrajectoryFeature = nullptr; //< Cached pointer to the trajectory feature in the feature schema.
  89. TrajectoryQuery m_trajectoryQuery;
  90. TrajectoryHistory m_trajectoryHistory;
  91. static constexpr float m_trajectorySecsToTrack = 5.0f;
  92. float m_timeSinceLastFrameSwitch = 0.0f;
  93. float m_newMotionTime = 0.0f;
  94. size_t m_lowestCostFrameIndex = InvalidIndex;
  95. float m_lowestCostSearchFrequency = 5.0f; //< How often the lowest cost frame shall be searched per second.
  96. bool m_blending = false;
  97. float m_blendWeight = 1.0f;
  98. float m_blendProgressTime = 0.0f; //< How long are we already blending? In seconds.
  99. /// Buffers used for FindLowestCostFrameIndex().
  100. AZStd::vector<float> m_tempCosts;
  101. AZStd::vector<float> m_minCosts;
  102. };
  103. } // namespace EMotionFX::MotionMatching