MotionMatchingEditorSystemComponent.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <EMotionFX/Source/ActorManager.h>
  10. #include <EMotionFX/Source/MotionSystem.h>
  11. #include <EMotionFX/Source/TransformData.h>
  12. #include <MotionMatchingEditorSystemComponent.h>
  13. #include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/ViewportPluginBus.h>
  14. namespace EMotionFX::MotionMatching
  15. {
  16. void MotionMatchingEditorSystemComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. serializeContext->Class<MotionMatchingEditorSystemComponent, MotionMatchingSystemComponent>()
  21. ->Version(0);
  22. }
  23. }
  24. MotionMatchingEditorSystemComponent::MotionMatchingEditorSystemComponent()
  25. {
  26. if (MotionMatchingEditorInterface::Get() == nullptr)
  27. {
  28. MotionMatchingEditorInterface::Register(this);
  29. }
  30. }
  31. MotionMatchingEditorSystemComponent::~MotionMatchingEditorSystemComponent()
  32. {
  33. if (MotionMatchingEditorInterface::Get() == this)
  34. {
  35. MotionMatchingEditorInterface::Unregister(this);
  36. }
  37. }
  38. void MotionMatchingEditorSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  39. {
  40. BaseSystemComponent::GetProvidedServices(provided);
  41. provided.push_back(AZ_CRC_CE("MotionMatchingEditorService"));
  42. }
  43. void MotionMatchingEditorSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  44. {
  45. BaseSystemComponent::GetIncompatibleServices(incompatible);
  46. incompatible.push_back(AZ_CRC_CE("MotionMatchingEditorService"));
  47. }
  48. void MotionMatchingEditorSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  49. {
  50. BaseSystemComponent::GetRequiredServices(required);
  51. }
  52. void MotionMatchingEditorSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  53. {
  54. BaseSystemComponent::GetDependentServices(dependent);
  55. }
  56. void MotionMatchingEditorSystemComponent::Activate()
  57. {
  58. MotionMatchingEditorRequestBus::Handler::BusConnect();
  59. MotionMatchingSystemComponent::Activate();
  60. AzToolsFramework::EditorEvents::Bus::Handler::BusConnect();
  61. AZ::TickBus::Handler::BusConnect();
  62. }
  63. void MotionMatchingEditorSystemComponent::Deactivate()
  64. {
  65. AZ::TickBus::Handler::BusDisconnect();
  66. AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect();
  67. MotionMatchingSystemComponent::Deactivate();
  68. MotionMatchingEditorRequestBus::Handler::BusDisconnect();
  69. }
  70. void MotionMatchingEditorSystemComponent::SetDebugDrawFeatureSchema(FeatureSchema* featureSchema)
  71. {
  72. m_debugVisFeatureSchema = featureSchema;
  73. m_data.reset();
  74. }
  75. FeatureSchema* MotionMatchingEditorSystemComponent::GetDebugDrawFeatureSchema() const
  76. {
  77. return m_debugVisFeatureSchema;
  78. }
  79. void MotionMatchingEditorSystemComponent::DebugDrawFrameFeatures(AzFramework::DebugDisplayRequests* debugDisplay)
  80. {
  81. // Skip directly in case there is no feature schema to be visualized.
  82. if (!m_debugVisFeatureSchema)
  83. {
  84. return;
  85. }
  86. // Find currently playing motion instance.
  87. MotionInstance* motionInstance = nullptr;
  88. {
  89. const ActorManager* actorManager = GetEMotionFX().GetActorManager();
  90. const size_t numActorInstances = actorManager->GetNumActorInstances();
  91. for (size_t i = 0; i < numActorInstances; ++i)
  92. {
  93. ActorInstance* actorInstance = actorManager->GetActorInstance(i);
  94. if (actorInstance->GetMotionSystem() &&
  95. actorInstance->GetMotionSystem()->GetIsPlaying())
  96. {
  97. motionInstance = actorInstance->GetMotionSystem()->GetMotionInstance(0);
  98. break;
  99. }
  100. }
  101. }
  102. // If there is a motion playing on an actor instance, draw the features given by the feature schema for the current playtime.
  103. if (motionInstance)
  104. {
  105. Motion* motion = motionInstance->GetMotion();
  106. ActorInstance* actorInstance = motionInstance->GetActorInstance();
  107. const FeatureSchema& featureSchema = *m_debugVisFeatureSchema;
  108. const Pose& currentPose = *actorInstance->GetTransformData()->GetCurrentPose();
  109. // Re-create the motion matching features in case the motion changed.
  110. if (!m_data ||
  111. m_lastMotionInstance != motionInstance)
  112. {
  113. m_data.reset(aznew MotionMatchingData(featureSchema));
  114. MotionMatchingData::InitSettings settings;
  115. settings.m_actorInstance = actorInstance;
  116. settings.m_motionList.emplace_back(motionInstance->GetMotion());
  117. m_data->Init(settings);
  118. }
  119. // Find the frame index in the frame database that belongs to the currently used pose.
  120. const size_t currentFrame = m_data->GetFrameDatabase().FindFrameIndex(motion, motionInstance->GetCurrentTime());
  121. if (currentFrame != InvalidIndex) // Render the feature debug visualizations for the current frame.
  122. {
  123. const AZStd::string currentFrameText = AZStd::string::format("Frame = %zu", currentFrame);
  124. debugDisplay->Draw2dTextLabel(10, 10, 1.0f, currentFrameText.c_str());
  125. for (Feature* feature : featureSchema.GetFeatures())
  126. {
  127. if (feature->GetDebugDrawEnabled())
  128. {
  129. feature->DebugDraw(*debugDisplay, currentPose, m_data->GetFeatureMatrix(), m_data->GetFeatureTransformer(), currentFrame);
  130. }
  131. }
  132. }
  133. }
  134. else
  135. {
  136. // Release the motion matching data in case the motion is not playing anymore.
  137. m_data.reset();
  138. motionInstance = nullptr;
  139. }
  140. m_lastMotionInstance = motionInstance;
  141. }
  142. void MotionMatchingEditorSystemComponent::DebugDraw(AZ::s32 debugDisplayId)
  143. {
  144. AZ_PROFILE_SCOPE(Animation, "MotionMatchingEditorSystemComponent::DebugDraw");
  145. if (debugDisplayId == -1)
  146. {
  147. return;
  148. }
  149. AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
  150. AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, debugDisplayId);
  151. AzFramework::DebugDisplayRequests* debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
  152. if (debugDisplay)
  153. {
  154. const AZ::u32 prevState = debugDisplay->GetState();
  155. DebugDrawFrameFeatures(debugDisplay);
  156. debugDisplay->SetState(prevState);
  157. }
  158. }
  159. void MotionMatchingEditorSystemComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  160. {
  161. // Draw the debug visualizations to the Animation Editor as well as the LY Editor viewport.
  162. AZ::s32 animationEditorViewportId = -1;
  163. EMStudio::ViewportPluginRequestBus::BroadcastResult(animationEditorViewportId, &EMStudio::ViewportPluginRequestBus::Events::GetViewportId);
  164. // Base system component
  165. MotionMatchingSystemComponent::OnTick(deltaTime, time);
  166. MotionMatchingSystemComponent::DebugDraw(animationEditorViewportId);
  167. // Editor system component
  168. MotionMatchingEditorSystemComponent::DebugDraw(AzFramework::g_defaultSceneEntityDebugDisplayId);
  169. MotionMatchingEditorSystemComponent::DebugDraw(animationEditorViewportId);
  170. }
  171. } // namespace EMotionFX::MotionMatching