3
0

AnimGraphComponent.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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/Asset/AssetSerializer.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <MCore/Source/AttributeString.h>
  14. #include <MCore/Source/AzCoreConversions.h>
  15. #include <EMotionFX/Source/AnimGraph.h>
  16. #include <EMotionFX/Source/AnimGraphInstance.h>
  17. #include <EMotionFX/Source/ActorInstance.h>
  18. #include <EMotionFX/Source/MotionSet.h>
  19. #include <Integration/Components/AnimGraphComponent.h>
  20. namespace EMotionFX
  21. {
  22. namespace Integration
  23. {
  24. //////////////////////////////////////////////////////////////////////////
  25. class AnimGraphComponentNotificationBehaviorHandler
  26. : public AnimGraphComponentNotificationBus::Handler, public AZ::BehaviorEBusHandler
  27. {
  28. public:
  29. AZ_EBUS_BEHAVIOR_BINDER(AnimGraphComponentNotificationBehaviorHandler, "{ECFDB974-8C47-467C-8476-258BF57B3395}", AZ::SystemAllocator,
  30. OnAnimGraphInstanceCreated, OnAnimGraphInstanceDestroyed, OnAnimGraphFloatParameterChanged, OnAnimGraphBoolParameterChanged,
  31. OnAnimGraphStringParameterChanged, OnAnimGraphVector2ParameterChanged, OnAnimGraphVector3ParameterChanged, OnAnimGraphRotationParameterChanged);
  32. void OnAnimGraphInstanceCreated(EMotionFX::AnimGraphInstance* animGraphInstance) override
  33. {
  34. Call(FN_OnAnimGraphInstanceCreated, animGraphInstance);
  35. }
  36. void OnAnimGraphInstanceDestroyed(EMotionFX::AnimGraphInstance* animGraphInstance) override
  37. {
  38. Call(FN_OnAnimGraphInstanceDestroyed, animGraphInstance);
  39. }
  40. void OnAnimGraphFloatParameterChanged(EMotionFX::AnimGraphInstance* animGraphInstance, size_t parameterIndex, float beforeValue, float afterValue) override
  41. {
  42. Call(FN_OnAnimGraphFloatParameterChanged, animGraphInstance, parameterIndex, beforeValue, afterValue);
  43. }
  44. void OnAnimGraphBoolParameterChanged(EMotionFX::AnimGraphInstance* animGraphInstance, size_t parameterIndex, bool beforeValue, bool afterValue) override
  45. {
  46. Call(FN_OnAnimGraphBoolParameterChanged, animGraphInstance, parameterIndex, beforeValue, afterValue);
  47. }
  48. void OnAnimGraphStringParameterChanged(EMotionFX::AnimGraphInstance* animGraphInstance, size_t parameterIndex, const char* beforeValue, const char* afterValue) override
  49. {
  50. Call(FN_OnAnimGraphStringParameterChanged, animGraphInstance, parameterIndex, beforeValue, afterValue);
  51. }
  52. void OnAnimGraphVector2ParameterChanged(EMotionFX::AnimGraphInstance* animGraphInstance, size_t parameterIndex, const AZ::Vector2& beforeValue, const AZ::Vector2& afterValue) override
  53. {
  54. Call(FN_OnAnimGraphVector2ParameterChanged, animGraphInstance, parameterIndex, beforeValue, afterValue);
  55. }
  56. void OnAnimGraphVector3ParameterChanged(EMotionFX::AnimGraphInstance* animGraphInstance, size_t parameterIndex, const AZ::Vector3& beforeValue, const AZ::Vector3& afterValue) override
  57. {
  58. Call(FN_OnAnimGraphVector3ParameterChanged, animGraphInstance, parameterIndex, beforeValue, afterValue);
  59. }
  60. void OnAnimGraphRotationParameterChanged(EMotionFX::AnimGraphInstance* animGraphInstance, size_t parameterIndex, const AZ::Quaternion& beforeValue, const AZ::Quaternion& afterValue) override
  61. {
  62. Call(FN_OnAnimGraphVector3ParameterChanged, animGraphInstance, parameterIndex, beforeValue, afterValue);
  63. }
  64. };
  65. //////////////////////////////////////////////////////////////////////////
  66. AnimGraphComponent::ParameterDefaults::~ParameterDefaults()
  67. {
  68. Reset();
  69. }
  70. //////////////////////////////////////////////////////////////////////////
  71. void AnimGraphComponent::ParameterDefaults::Reset()
  72. {
  73. for (AZ::ScriptProperty* p : m_parameters)
  74. {
  75. delete p;
  76. }
  77. m_parameters.clear();
  78. }
  79. //////////////////////////////////////////////////////////////////////////
  80. void AnimGraphComponent::ParameterDefaults::Reflect(AZ::ReflectContext* context)
  81. {
  82. auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  83. if (serializeContext)
  84. {
  85. serializeContext->Class<ParameterDefaults>()
  86. ->Version(1)
  87. ->Field("Parameters", &ParameterDefaults::m_parameters)
  88. ;
  89. }
  90. }
  91. //////////////////////////////////////////////////////////////////////////
  92. void AnimGraphComponent::Configuration::Reflect(AZ::ReflectContext* context)
  93. {
  94. auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  95. if (serializeContext)
  96. {
  97. serializeContext->Class<Configuration>()
  98. ->Version(1)
  99. ->Field("AnimGraphAsset", &Configuration::m_animGraphAsset)
  100. ->Field("MotionSetAsset", &Configuration::m_motionSetAsset)
  101. ->Field("ActiveMotionSetName", &Configuration::m_activeMotionSetName)
  102. ->Field("ParameterDefaults", &Configuration::m_parameterDefaults)
  103. ->Field("DebugVisualize", &Configuration::m_visualize)
  104. ;
  105. }
  106. }
  107. //////////////////////////////////////////////////////////////////////////
  108. void AnimGraphComponent::Reflect(AZ::ReflectContext* context)
  109. {
  110. ParameterDefaults::Reflect(context);
  111. Configuration::Reflect(context);
  112. auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  113. if (serializeContext)
  114. {
  115. serializeContext->Class<AnimGraphComponent, AZ::Component>()
  116. ->Version(1)
  117. ->Field("Configuration", &AnimGraphComponent::m_configuration)
  118. ;
  119. }
  120. auto* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  121. if (behaviorContext)
  122. {
  123. behaviorContext->Constant("InvalidParameterIndex", BehaviorConstant(InvalidIndex));
  124. behaviorContext->EBus<AnimGraphComponentRequestBus>("AnimGraphComponentRequestBus")
  125. // General API
  126. ->Event("FindParameterIndex", &AnimGraphComponentRequestBus::Events::FindParameterIndex)
  127. ->Event("FindParameterName", &AnimGraphComponentRequestBus::Events::FindParameterName)
  128. ->Event("SetActiveMotionSet", &AnimGraphComponentRequestBus::Events::SetActiveMotionSet)
  129. // Setters
  130. ->Event("SetParameterFloat", &AnimGraphComponentRequestBus::Events::SetParameterFloat)
  131. ->Event("SetParameterBool", &AnimGraphComponentRequestBus::Events::SetParameterBool)
  132. ->Event("SetParameterString", &AnimGraphComponentRequestBus::Events::SetParameterString)
  133. ->Event("SetParameterVector2", &AnimGraphComponentRequestBus::Events::SetParameterVector2)
  134. ->Event("SetParameterVector3", &AnimGraphComponentRequestBus::Events::SetParameterVector3)
  135. ->Event("SetParameterRotationEuler", &AnimGraphComponentRequestBus::Events::SetParameterRotationEuler)
  136. ->Event("SetParameterRotation", &AnimGraphComponentRequestBus::Events::SetNamedParameterRotation)
  137. ->Event("SetNamedParameterFloat", &AnimGraphComponentRequestBus::Events::SetNamedParameterFloat)
  138. ->Event("SetNamedParameterBool", &AnimGraphComponentRequestBus::Events::SetNamedParameterBool)
  139. ->Event("SetNamedParameterString", &AnimGraphComponentRequestBus::Events::SetNamedParameterString)
  140. ->Event("SetNamedParameterVector2", &AnimGraphComponentRequestBus::Events::SetNamedParameterVector2)
  141. ->Event("SetNamedParameterVector3", &AnimGraphComponentRequestBus::Events::SetNamedParameterVector3)
  142. ->Event("SetNamedParameterRotationEuler", &AnimGraphComponentRequestBus::Events::SetNamedParameterRotationEuler)
  143. ->Event("SetNamedParameterRotation", &AnimGraphComponentRequestBus::Events::SetNamedParameterRotation)
  144. ->Event("SetVisualizeEnabled", &AnimGraphComponentRequestBus::Events::SetVisualizeEnabled)
  145. // Getters
  146. ->Event("GetParameterFloat", &AnimGraphComponentRequestBus::Events::GetParameterFloat)
  147. ->Event("GetParameterBool", &AnimGraphComponentRequestBus::Events::GetParameterBool)
  148. ->Event("GetParameterString", &AnimGraphComponentRequestBus::Events::GetParameterString)
  149. ->Event("GetParameterVector2", &AnimGraphComponentRequestBus::Events::GetParameterVector2)
  150. ->Event("GetParameterVector3", &AnimGraphComponentRequestBus::Events::GetParameterVector3)
  151. ->Event("GetParameterRotationEuler", &AnimGraphComponentRequestBus::Events::GetParameterRotationEuler)
  152. ->Event("GetParameterRotation", &AnimGraphComponentRequestBus::Events::GetNamedParameterRotation)
  153. ->Event("GetNamedParameterFloat", &AnimGraphComponentRequestBus::Events::GetNamedParameterFloat)
  154. ->Event("GetNamedParameterBool", &AnimGraphComponentRequestBus::Events::GetNamedParameterBool)
  155. ->Event("GetNamedParameterString", &AnimGraphComponentRequestBus::Events::GetNamedParameterString)
  156. ->Event("GetNamedParameterVector2", &AnimGraphComponentRequestBus::Events::GetNamedParameterVector2)
  157. ->Event("GetNamedParameterVector3", &AnimGraphComponentRequestBus::Events::GetNamedParameterVector3)
  158. ->Event("GetNamedParameterRotationEuler", &AnimGraphComponentRequestBus::Events::GetNamedParameterRotationEuler)
  159. ->Event("GetNamedParameterRotation", &AnimGraphComponentRequestBus::Events::GetNamedParameterRotation)
  160. ->Event("GetVisualizeEnabled", &AnimGraphComponentRequestBus::Events::GetVisualizeEnabled)
  161. // Anim Graph Sync
  162. ->Event("SyncAnimGraph", &AnimGraphComponentRequestBus::Events::SyncAnimGraph)
  163. ->Event("DesyncAnimGraph", &AnimGraphComponentRequestBus::Events::DesyncAnimGraph)
  164. ;
  165. behaviorContext->EBus<AnimGraphComponentNotificationBus>("AnimGraphComponentNotificationBus")
  166. ->Handler<AnimGraphComponentNotificationBehaviorHandler>()
  167. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::List)
  168. ;
  169. behaviorContext->EBus<AnimGraphComponentNetworkRequestBus>("AnimGraphComponentNetworkRequestBus")
  170. ->Attribute(AZ::Script::Attributes::Category, "Animation")
  171. ->Event("IsAssetReady", &AnimGraphComponentNetworkRequestBus::Events::IsAssetReady)
  172. ->Event("HasSnapshot", &AnimGraphComponentNetworkRequestBus::Events::HasSnapshot)
  173. ->Event("CreateSnapshot", &AnimGraphComponentNetworkRequestBus::Events::CreateSnapshot)
  174. ->Event("SetActiveStates", &AnimGraphComponentNetworkRequestBus::Events::SetActiveStates)
  175. ->Event("GetActiveStates", &AnimGraphComponentNetworkRequestBus::Events::GetActiveStates)
  176. ;
  177. }
  178. }
  179. //////////////////////////////////////////////////////////////////////////
  180. AnimGraphComponent::AnimGraphComponent(const Configuration* config)
  181. {
  182. if (config)
  183. {
  184. m_configuration = *config;
  185. }
  186. }
  187. //////////////////////////////////////////////////////////////////////////
  188. AnimGraphComponent::~AnimGraphComponent()
  189. {
  190. }
  191. //////////////////////////////////////////////////////////////////////////
  192. void AnimGraphComponent::Init()
  193. {
  194. }
  195. //////////////////////////////////////////////////////////////////////////
  196. void AnimGraphComponent::Activate()
  197. {
  198. m_animGraphInstance.reset();
  199. AZ::Data::AssetBus::MultiHandler::BusDisconnect();
  200. auto& cfg = m_configuration;
  201. if (cfg.m_animGraphAsset.GetId().IsValid())
  202. {
  203. AZ::Data::AssetBus::MultiHandler::BusConnect(cfg.m_animGraphAsset.GetId());
  204. cfg.m_animGraphAsset.QueueLoad();
  205. if (cfg.m_motionSetAsset.GetId().IsValid())
  206. {
  207. AZ::Data::AssetBus::MultiHandler::BusConnect(cfg.m_motionSetAsset.GetId());
  208. cfg.m_motionSetAsset.QueueLoad();
  209. }
  210. }
  211. ActorComponentNotificationBus::Handler::BusConnect(GetEntityId());
  212. AnimGraphComponentRequestBus::Handler::BusConnect(GetEntityId());
  213. AnimGraphComponentNotificationBus::Handler::BusConnect(GetEntityId());
  214. AnimGraphComponentNetworkRequestBus::Handler::BusConnect(GetEntityId());
  215. }
  216. //////////////////////////////////////////////////////////////////////////
  217. void AnimGraphComponent::Deactivate()
  218. {
  219. AnimGraphComponentNetworkRequestBus::Handler::BusDisconnect();
  220. AnimGraphComponentNotificationBus::Handler::BusDisconnect();
  221. AnimGraphComponentRequestBus::Handler::BusDisconnect();
  222. ActorComponentNotificationBus::Handler::BusDisconnect();
  223. AZ::Data::AssetBus::MultiHandler::BusDisconnect();
  224. m_actorInstance.reset();
  225. DestroyAnimGraphInstance();
  226. m_configuration.m_animGraphAsset.Release();
  227. }
  228. //////////////////////////////////////////////////////////////////////////
  229. void AnimGraphComponent::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  230. {
  231. OnAssetReady(asset);
  232. }
  233. //////////////////////////////////////////////////////////////////////////
  234. void AnimGraphComponent::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
  235. {
  236. auto& cfg = m_configuration;
  237. // Keep the previous asset around until the anim graph instances are removed
  238. AZ::Data::Asset<AZ::Data::AssetData> prevAnimGraphAsset = cfg.m_animGraphAsset;
  239. AZ::Data::Asset<AZ::Data::AssetData> prevMotionSetAsset = cfg.m_motionSetAsset;
  240. if (asset == cfg.m_animGraphAsset)
  241. {
  242. cfg.m_animGraphAsset = asset;
  243. }
  244. else if (asset == cfg.m_motionSetAsset)
  245. {
  246. cfg.m_motionSetAsset = asset;
  247. }
  248. CheckCreateAnimGraphInstance();
  249. }
  250. void AnimGraphComponent::SetAnimGraphAssetId(const AZ::Data::AssetId& assetId)
  251. {
  252. m_configuration.m_animGraphAsset = AZ::Data::Asset<AnimGraphAsset>(assetId, azrtti_typeid<AnimGraphAsset>());
  253. }
  254. void AnimGraphComponent::SetMotionSetAssetId(const AZ::Data::AssetId& assetId)
  255. {
  256. m_configuration.m_motionSetAsset = AZ::Data::Asset<MotionSetAsset>(assetId, azrtti_typeid<MotionSetAsset>());
  257. }
  258. //////////////////////////////////////////////////////////////////////////
  259. void AnimGraphComponent::OnActorInstanceCreated(EMotionFX::ActorInstance* actorInstance)
  260. {
  261. m_actorInstance = actorInstance;
  262. CheckCreateAnimGraphInstance();
  263. }
  264. //////////////////////////////////////////////////////////////////////////
  265. void AnimGraphComponent::OnActorInstanceDestroyed(EMotionFX::ActorInstance* /*actorInstance*/)
  266. {
  267. DestroyAnimGraphInstance();
  268. m_actorInstance.reset();
  269. }
  270. //////////////////////////////////////////////////////////////////////////
  271. void AnimGraphComponent::OnAnimGraphSynced(EMotionFX::AnimGraphInstance* animGraphInstance)
  272. {
  273. if (m_animGraphInstance)
  274. {
  275. m_animGraphInstance->AddFollowerGraph(animGraphInstance, true);
  276. }
  277. }
  278. //////////////////////////////////////////////////////////////////////////
  279. void AnimGraphComponent::OnAnimGraphDesynced(EMotionFX::AnimGraphInstance* animGraphInstance)
  280. {
  281. if (m_animGraphInstance)
  282. {
  283. m_animGraphInstance->RemoveFollowerGraph(animGraphInstance, true);
  284. }
  285. }
  286. bool AnimGraphComponent::IsAssetReady() const
  287. {
  288. return (m_actorInstance && m_animGraphInstance);
  289. }
  290. bool AnimGraphComponent::HasSnapshot() const
  291. {
  292. if (m_animGraphInstance && m_animGraphInstance->GetSnapshot())
  293. {
  294. return true;
  295. }
  296. return false;
  297. }
  298. void AnimGraphComponent::CreateSnapshot(bool isAuthoritative)
  299. {
  300. if (m_animGraphInstance)
  301. {
  302. m_animGraphInstance->CreateSnapshot(isAuthoritative);
  303. m_animGraphInstance->OnNetworkConnected();
  304. // This will stop the MCore Job schedule update the actor instance and anim graph for authoritative entity.
  305. // After doing so, we will have to update this actor manually in the networking update.
  306. m_animGraphInstance->GetActorInstance()->SetIsEnabled(!isAuthoritative);
  307. }
  308. else
  309. {
  310. AZ_ErrorOnce("EMotionFX", false, "Cannot create snapshot as anim graph instance has not been created yet. "
  311. "Please make sure you selected an anim graph in the anim graph component.");
  312. }
  313. }
  314. void AnimGraphComponent::SetActiveStates(const AZStd::vector<AZ::u32>& activeStates)
  315. {
  316. if (m_animGraphInstance)
  317. {
  318. m_animGraphInstance->OnNetworkActiveNodesUpdate(activeStates);
  319. }
  320. }
  321. void AnimGraphComponent::SetMotionPlaytimes(const MotionNodePlaytimeContainer& motionNodePlaytimes)
  322. {
  323. if (m_animGraphInstance)
  324. {
  325. m_animGraphInstance->OnNetworkMotionNodePlaytimesUpdate(motionNodePlaytimes);
  326. }
  327. }
  328. NodeIndexContainer AnimGraphComponent::s_emptyNodeIndexContainer = {};
  329. const NodeIndexContainer& AnimGraphComponent::GetActiveStates() const
  330. {
  331. if (m_animGraphInstance)
  332. {
  333. const AZStd::shared_ptr<AnimGraphSnapshot> snapshot = m_animGraphInstance->GetSnapshot();
  334. if (snapshot)
  335. {
  336. AZ_Warning("EMotionFX", false, "Call GetActiveStates function but no snapshot is created for this instance.");
  337. return snapshot->GetActiveNodes();
  338. }
  339. }
  340. return s_emptyNodeIndexContainer;
  341. }
  342. MotionNodePlaytimeContainer AnimGraphComponent::s_emptyMotionNodePlaytimeContainer = {};
  343. const MotionNodePlaytimeContainer& AnimGraphComponent::GetMotionPlaytimes() const
  344. {
  345. if (m_animGraphInstance)
  346. {
  347. const AZStd::shared_ptr<AnimGraphSnapshot> snapshot = m_animGraphInstance->GetSnapshot();
  348. if (snapshot)
  349. {
  350. AZ_Warning("EMotionFX", false, "Call GetActiveStates function but no snapshot is created for this instance.");
  351. return snapshot->GetMotionNodePlaytimes();
  352. }
  353. }
  354. return s_emptyMotionNodePlaytimeContainer;
  355. }
  356. void AnimGraphComponent::UpdateActorExternal(float deltatime)
  357. {
  358. if (m_actorInstance)
  359. {
  360. m_actorInstance->UpdateTransformations(deltatime);
  361. }
  362. }
  363. void AnimGraphComponent::SetNetworkRandomSeed(AZ::u64 seed)
  364. {
  365. if (m_animGraphInstance)
  366. {
  367. m_animGraphInstance->GetLcgRandom().SetSeed(seed);
  368. }
  369. }
  370. AZ::u64 AnimGraphComponent::GetNetworkRandomSeed() const
  371. {
  372. if (m_animGraphInstance)
  373. {
  374. return m_animGraphInstance->GetLcgRandom().GetSeed();
  375. }
  376. return 0;
  377. }
  378. void AnimGraphComponent::SetActorThreadIndex(AZ::u32 threadIndex)
  379. {
  380. if (m_actorInstance)
  381. {
  382. m_actorInstance->SetThreadIndex(threadIndex);
  383. }
  384. }
  385. AZ::u32 AnimGraphComponent::GetActorThreadIndex() const
  386. {
  387. if (m_actorInstance)
  388. {
  389. return m_actorInstance->GetThreadIndex();
  390. }
  391. return 0;
  392. }
  393. void AnimGraphComponent::CheckCreateAnimGraphInstance()
  394. {
  395. auto& cfg = m_configuration;
  396. if (m_actorInstance && cfg.m_animGraphAsset.IsReady() && cfg.m_motionSetAsset.IsReady())
  397. {
  398. DestroyAnimGraphInstance();
  399. EMotionFX::MotionSet* rootMotionSet = cfg.m_motionSetAsset.Get()->m_emfxMotionSet.get();
  400. EMotionFX::MotionSet* motionSet = rootMotionSet;
  401. if (!cfg.m_activeMotionSetName.empty())
  402. {
  403. motionSet = rootMotionSet->RecursiveFindMotionSetByName(cfg.m_activeMotionSetName, true);
  404. if (!motionSet)
  405. {
  406. AZ_Warning("EMotionFX", false, "Failed to find motion set \"%s\" in motion set file %s.",
  407. cfg.m_activeMotionSetName.c_str(),
  408. rootMotionSet->GetName());
  409. motionSet = rootMotionSet;
  410. }
  411. }
  412. m_animGraphInstance = cfg.m_animGraphAsset.Get()->CreateInstance(m_actorInstance.get(), motionSet);
  413. if (!m_animGraphInstance)
  414. {
  415. AZ_Error("EMotionFX", false, "Failed to create anim graph instance for entity \"%s\" %s.",
  416. GetEntity()->GetName().c_str(),
  417. GetEntityId().ToString().c_str());
  418. return;
  419. }
  420. m_animGraphInstance->SetVisualizationEnabled(cfg.m_visualize);
  421. m_actorInstance->SetAnimGraphInstance(m_animGraphInstance.get());
  422. AnimGraphInstancePostCreate();
  423. // Apply parameter defaults.
  424. for (AZ::ScriptProperty* parameter : cfg.m_parameterDefaults.m_parameters)
  425. {
  426. const char* paramName = parameter->m_name.c_str();
  427. if (azrtti_istypeof<AZ::ScriptPropertyNumber>(parameter))
  428. {
  429. // This will handle float and integer types.
  430. SetNamedParameterFloat(paramName, aznumeric_caster(static_cast<AZ::ScriptPropertyNumber*>(parameter)->m_value));
  431. }
  432. else if (azrtti_istypeof<AZ::ScriptPropertyBoolean>(parameter))
  433. {
  434. SetNamedParameterBool(paramName, static_cast<AZ::ScriptPropertyBoolean*>(parameter)->m_value);
  435. }
  436. else if (azrtti_istypeof<AZ::ScriptPropertyString>(parameter))
  437. {
  438. SetNamedParameterString(paramName, static_cast<AZ::ScriptPropertyString*>(parameter)->m_value.c_str());
  439. }
  440. else
  441. {
  442. AZ_Warning("EMotionFX", false, "Invalid type for anim graph parameter \"%s\".", paramName);
  443. }
  444. }
  445. // Notify listeners that the anim graph is ready.
  446. AnimGraphComponentNotificationBus::Event(
  447. GetEntityId(),
  448. &AnimGraphComponentNotificationBus::Events::OnAnimGraphInstanceCreated,
  449. m_animGraphInstance.get());
  450. }
  451. }
  452. //////////////////////////////////////////////////////////////////////////
  453. void AnimGraphComponent::DestroyAnimGraphInstance()
  454. {
  455. if (m_animGraphInstance)
  456. {
  457. AnimGraphComponentNotificationBus::Event(
  458. GetEntityId(),
  459. &AnimGraphComponentNotificationBus::Events::OnAnimGraphInstanceDestroyed,
  460. m_animGraphInstance.get());
  461. AnimGraphInstancePreDestroy();
  462. m_animGraphInstance.reset();
  463. }
  464. }
  465. //////////////////////////////////////////////////////////////////////////
  466. void AnimGraphComponent::AnimGraphInstancePostCreate()
  467. {
  468. // Reference is not incremented when the instance is assigned to the actor, but is
  469. // decremented when actor is destroyed. Add a ref here to account for this.
  470. m_animGraphInstance.get()->IncreaseReferenceCount();
  471. }
  472. //////////////////////////////////////////////////////////////////////////
  473. void AnimGraphComponent::AnimGraphInstancePreDestroy()
  474. {
  475. // If the anim graph is still active on the actor, deactivate it.
  476. // Also remove the extra reference we added to account for the actor's ownership
  477. // over it (see corresponding logic in OnAnimGraphInstanceCreated()), since we're
  478. // relinquishing that ownership.
  479. if (m_actorInstance && m_animGraphInstance && m_actorInstance->GetAnimGraphInstance() == m_animGraphInstance.get())
  480. {
  481. m_actorInstance->SetAnimGraphInstance(nullptr);
  482. m_animGraphInstance->DecreaseReferenceCount();
  483. }
  484. }
  485. EMotionFX::AnimGraphInstance* AnimGraphComponent::GetAnimGraphInstance()
  486. {
  487. return m_animGraphInstance ? m_animGraphInstance.get() : nullptr;
  488. }
  489. //////////////////////////////////////////////////////////////////////////
  490. size_t AnimGraphComponent::FindParameterIndex(const char* parameterName)
  491. {
  492. if (m_animGraphInstance)
  493. {
  494. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  495. if (parameterIndex.IsSuccess())
  496. {
  497. return parameterIndex.GetValue();
  498. }
  499. }
  500. return InvalidIndex;
  501. }
  502. //////////////////////////////////////////////////////////////////////////
  503. const char* AnimGraphComponent::FindParameterName(size_t parameterIndex)
  504. {
  505. if (parameterIndex == InvalidIndex || !m_animGraphInstance || !m_animGraphInstance->GetAnimGraph())
  506. {
  507. return "";
  508. }
  509. return m_animGraphInstance->GetAnimGraph()->FindParameter(parameterIndex)->GetName().c_str();
  510. }
  511. //////////////////////////////////////////////////////////////////////////
  512. void AnimGraphComponent::SetParameterFloat(size_t parameterIndex, float value)
  513. {
  514. if (parameterIndex == InvalidIndex)
  515. {
  516. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  517. return;
  518. }
  519. if (m_animGraphInstance)
  520. {
  521. MCore::Attribute* param = m_animGraphInstance->GetParameterValue(parameterIndex);
  522. float previousValue;
  523. switch (param->GetType())
  524. {
  525. case MCore::AttributeFloat::TYPE_ID:
  526. {
  527. MCore::AttributeFloat* floatParam = static_cast<MCore::AttributeFloat*>(param);
  528. previousValue = floatParam->GetValue();
  529. floatParam->SetValue(value);
  530. break;
  531. }
  532. case MCore::AttributeBool::TYPE_ID:
  533. {
  534. MCore::AttributeBool* boolParam = static_cast<MCore::AttributeBool*>(param);
  535. previousValue = boolParam->GetValue();
  536. boolParam->SetValue(!MCore::Math::IsFloatZero(value));
  537. break;
  538. }
  539. case MCore::AttributeInt32::TYPE_ID:
  540. {
  541. MCore::AttributeInt32* intParam = static_cast<MCore::AttributeInt32*>(param);
  542. previousValue = static_cast<float>(intParam->GetValue());
  543. intParam->SetValue(static_cast<int32>(value));
  544. break;
  545. }
  546. default:
  547. {
  548. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu can not be set as float, is of type: %s", parameterIndex, param->GetTypeString());
  549. return;
  550. }
  551. }
  552. // Notify listeners about the parameter change
  553. AnimGraphComponentNotificationBus::Event(
  554. GetEntityId(),
  555. &AnimGraphComponentNotificationBus::Events::OnAnimGraphFloatParameterChanged,
  556. m_animGraphInstance.get(),
  557. parameterIndex,
  558. previousValue,
  559. value);
  560. }
  561. }
  562. //////////////////////////////////////////////////////////////////////////
  563. void AnimGraphComponent::SetParameterBool(size_t parameterIndex, bool value)
  564. {
  565. if (parameterIndex == InvalidIndex)
  566. {
  567. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  568. return;
  569. }
  570. if (m_animGraphInstance)
  571. {
  572. MCore::Attribute* param = m_animGraphInstance->GetParameterValue(parameterIndex);
  573. bool previousValue;
  574. switch (param->GetType())
  575. {
  576. case MCore::AttributeBool::TYPE_ID:
  577. {
  578. MCore::AttributeBool* boolParam = static_cast<MCore::AttributeBool*>(param);
  579. previousValue = boolParam->GetValue();
  580. boolParam->SetValue(value);
  581. break;
  582. }
  583. case MCore::AttributeFloat::TYPE_ID:
  584. {
  585. MCore::AttributeFloat* floatParam = static_cast<MCore::AttributeFloat*>(param);
  586. previousValue = !MCore::Math::IsFloatZero(floatParam->GetValue());
  587. floatParam->SetValue(value);
  588. break;
  589. }
  590. case MCore::AttributeInt32::TYPE_ID:
  591. {
  592. MCore::AttributeInt32* intParam = static_cast<MCore::AttributeInt32*>(param);
  593. previousValue = intParam->GetValue() != 0;
  594. intParam->SetValue(value);
  595. break;
  596. }
  597. default:
  598. {
  599. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu can not be set as bool, is of type: %s", parameterIndex, param->GetTypeString());
  600. return;
  601. }
  602. }
  603. // Notify listeners about the parameter change
  604. AnimGraphComponentNotificationBus::Event(
  605. GetEntityId(),
  606. &AnimGraphComponentNotificationBus::Events::OnAnimGraphBoolParameterChanged,
  607. m_animGraphInstance.get(),
  608. parameterIndex,
  609. previousValue,
  610. value);
  611. }
  612. }
  613. //////////////////////////////////////////////////////////////////////////
  614. void AnimGraphComponent::SetParameterString(size_t parameterIndex, const char* value)
  615. {
  616. if (parameterIndex == InvalidIndex)
  617. {
  618. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  619. return;
  620. }
  621. if (m_animGraphInstance)
  622. {
  623. MCore::AttributeString* param = m_animGraphInstance->GetParameterValueChecked<MCore::AttributeString>(parameterIndex);
  624. if (param)
  625. {
  626. // Since the event is sent out synchronously we just need to keep a copy of the
  627. // previous value. The new value can be reused from "value"
  628. // If the event were to change to a queued event, the parameters should be changed
  629. // to AZStd::string for safety.
  630. const AZStd::string previousValue(param->GetValue());
  631. param->SetValue(value);
  632. // Notify listeners about the parameter change
  633. AnimGraphComponentNotificationBus::Event(
  634. GetEntityId(),
  635. &AnimGraphComponentNotificationBus::Events::OnAnimGraphStringParameterChanged,
  636. m_animGraphInstance.get(),
  637. parameterIndex,
  638. previousValue.c_str(),
  639. value);
  640. }
  641. else
  642. {
  643. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu is not a string", parameterIndex);
  644. }
  645. }
  646. }
  647. //////////////////////////////////////////////////////////////////////////
  648. void AnimGraphComponent::SetParameterVector2(size_t parameterIndex, const AZ::Vector2& value)
  649. {
  650. if (parameterIndex == InvalidIndex)
  651. {
  652. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  653. return;
  654. }
  655. if (m_animGraphInstance)
  656. {
  657. MCore::AttributeVector2* param = m_animGraphInstance->GetParameterValueChecked<MCore::AttributeVector2>(parameterIndex);
  658. if (param)
  659. {
  660. const AZ::Vector2 previousValue = param->GetValue();
  661. param->SetValue(value);
  662. // Notify listeners about the parameter change
  663. AnimGraphComponentNotificationBus::Event(
  664. GetEntityId(),
  665. &AnimGraphComponentNotificationBus::Events::OnAnimGraphVector2ParameterChanged,
  666. m_animGraphInstance.get(),
  667. parameterIndex,
  668. previousValue,
  669. value);
  670. }
  671. else
  672. {
  673. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu is not a vector2", parameterIndex);
  674. }
  675. }
  676. }
  677. //////////////////////////////////////////////////////////////////////////
  678. void AnimGraphComponent::SetParameterVector3(size_t parameterIndex, const AZ::Vector3& value)
  679. {
  680. if (parameterIndex == InvalidIndex)
  681. {
  682. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  683. return;
  684. }
  685. if (m_animGraphInstance)
  686. {
  687. MCore::AttributeVector3* param = m_animGraphInstance->GetParameterValueChecked<MCore::AttributeVector3>(parameterIndex);
  688. if (param)
  689. {
  690. const AZ::Vector3 previousValue = param->GetValue();
  691. param->SetValue(value);
  692. // Notify listeners about the parameter change
  693. AnimGraphComponentNotificationBus::Event(
  694. GetEntityId(),
  695. &AnimGraphComponentNotificationBus::Events::OnAnimGraphVector3ParameterChanged,
  696. m_animGraphInstance.get(),
  697. parameterIndex,
  698. previousValue,
  699. value);
  700. }
  701. else
  702. {
  703. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu is not a vector3", parameterIndex);
  704. }
  705. }
  706. }
  707. //////////////////////////////////////////////////////////////////////////
  708. void AnimGraphComponent::SetParameterRotationEuler(size_t parameterIndex, const AZ::Vector3& value)
  709. {
  710. if (parameterIndex == InvalidIndex)
  711. {
  712. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  713. return;
  714. }
  715. if (m_animGraphInstance)
  716. {
  717. MCore::Attribute* param = m_animGraphInstance->GetParameterValue(parameterIndex);
  718. AZ::Quaternion previousValue;
  719. switch (param->GetType())
  720. {
  721. case MCore::AttributeQuaternion::TYPE_ID:
  722. {
  723. MCore::AttributeQuaternion* quaternionParam = static_cast<MCore::AttributeQuaternion*>(param);
  724. previousValue = quaternionParam->GetValue();
  725. quaternionParam->SetValue(MCore::AzEulerAnglesToAzQuat(value));
  726. break;
  727. }
  728. default:
  729. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu can not be set as rotation euler, is of type: %s", parameterIndex, param->GetTypeString());
  730. return;
  731. }
  732. // Notify listeners about the parameter change
  733. AnimGraphComponentNotificationBus::Event(
  734. GetEntityId(),
  735. &AnimGraphComponentNotificationBus::Events::OnAnimGraphRotationParameterChanged,
  736. m_animGraphInstance.get(),
  737. parameterIndex,
  738. previousValue,
  739. MCore::AzEulerAnglesToAzQuat(value));
  740. }
  741. }
  742. //////////////////////////////////////////////////////////////////////////
  743. void AnimGraphComponent::SetParameterRotation(size_t parameterIndex, const AZ::Quaternion& value)
  744. {
  745. if (parameterIndex == InvalidIndex)
  746. {
  747. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  748. return;
  749. }
  750. if (m_animGraphInstance)
  751. {
  752. MCore::Attribute* param = m_animGraphInstance->GetParameterValue(parameterIndex);
  753. AZ::Quaternion previousValue;
  754. switch (param->GetType())
  755. {
  756. case MCore::AttributeQuaternion::TYPE_ID:
  757. {
  758. MCore::AttributeQuaternion* quaternionParam = static_cast<MCore::AttributeQuaternion*>(param);
  759. previousValue = quaternionParam->GetValue();
  760. quaternionParam->SetValue(value);
  761. break;
  762. }
  763. default:
  764. AZ_Warning("EMotionFX", false, "Anim graph parameter index: %zu can not be set as rotation, is of type: %s", parameterIndex, param->GetTypeString());
  765. return;
  766. }
  767. // Notify listeners about the parameter change
  768. AnimGraphComponentNotificationBus::Event(
  769. GetEntityId(),
  770. &AnimGraphComponentNotificationBus::Events::OnAnimGraphRotationParameterChanged,
  771. m_animGraphInstance.get(),
  772. parameterIndex,
  773. previousValue,
  774. value);
  775. }
  776. }
  777. //////////////////////////////////////////////////////////////////////////
  778. void AnimGraphComponent::SetNamedParameterFloat(const char* parameterName, float value)
  779. {
  780. if (m_animGraphInstance)
  781. {
  782. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  783. if (!parameterIndex.IsSuccess())
  784. {
  785. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  786. return;
  787. }
  788. SetParameterFloat(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  789. }
  790. }
  791. //////////////////////////////////////////////////////////////////////////
  792. void AnimGraphComponent::SetNamedParameterBool(const char* parameterName, bool value)
  793. {
  794. if (m_animGraphInstance)
  795. {
  796. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  797. if (!parameterIndex.IsSuccess())
  798. {
  799. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  800. return;
  801. }
  802. SetParameterBool(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  803. }
  804. }
  805. //////////////////////////////////////////////////////////////////////////
  806. void AnimGraphComponent::SetNamedParameterString(const char* parameterName, const char* value)
  807. {
  808. if (m_animGraphInstance)
  809. {
  810. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  811. if (!parameterIndex.IsSuccess())
  812. {
  813. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  814. return;
  815. }
  816. SetParameterString(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  817. }
  818. }
  819. //////////////////////////////////////////////////////////////////////////
  820. void AnimGraphComponent::SetNamedParameterVector2(const char* parameterName, const AZ::Vector2& value)
  821. {
  822. if (m_animGraphInstance)
  823. {
  824. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  825. if (!parameterIndex.IsSuccess())
  826. {
  827. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  828. return;
  829. }
  830. SetParameterVector2(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  831. }
  832. }
  833. //////////////////////////////////////////////////////////////////////////
  834. void AnimGraphComponent::SetNamedParameterVector3(const char* parameterName, const AZ::Vector3& value)
  835. {
  836. if (m_animGraphInstance)
  837. {
  838. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  839. if (!parameterIndex.IsSuccess())
  840. {
  841. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  842. return;
  843. }
  844. SetParameterVector3(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  845. }
  846. }
  847. //////////////////////////////////////////////////////////////////////////
  848. void AnimGraphComponent::SetNamedParameterRotationEuler(const char* parameterName, const AZ::Vector3& value)
  849. {
  850. if (m_animGraphInstance)
  851. {
  852. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  853. if (!parameterIndex.IsSuccess())
  854. {
  855. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  856. return;
  857. }
  858. SetParameterRotationEuler(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  859. }
  860. }
  861. //////////////////////////////////////////////////////////////////////////
  862. void AnimGraphComponent::SetNamedParameterRotation(const char* parameterName, const AZ::Quaternion& value)
  863. {
  864. if (m_animGraphInstance)
  865. {
  866. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  867. if (!parameterIndex.IsSuccess())
  868. {
  869. AZ_Warning("EmotionFX", false, "Invalid anim graph parameter name: %s", parameterName);
  870. return;
  871. }
  872. SetParameterRotation(static_cast<AZ::u32>(parameterIndex.GetValue()), value);
  873. }
  874. }
  875. void AnimGraphComponent::SetVisualizeEnabled(bool enabled)
  876. {
  877. if (m_animGraphInstance)
  878. {
  879. m_animGraphInstance->SetVisualizationEnabled(enabled);
  880. }
  881. }
  882. bool AnimGraphComponent::GetVisualizeEnabled()
  883. {
  884. if (m_animGraphInstance)
  885. {
  886. return m_animGraphInstance->GetVisualizationEnabled();
  887. }
  888. return false;
  889. }
  890. //////////////////////////////////////////////////////////////////////////
  891. float AnimGraphComponent::GetParameterFloat(size_t parameterIndex)
  892. {
  893. if (parameterIndex == InvalidIndex)
  894. {
  895. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  896. return 0.f;
  897. }
  898. float value = 0.f;
  899. if (m_animGraphInstance)
  900. {
  901. m_animGraphInstance->GetParameterValueAsFloat(parameterIndex, &value);
  902. }
  903. return value;
  904. }
  905. //////////////////////////////////////////////////////////////////////////
  906. bool AnimGraphComponent::GetParameterBool(size_t parameterIndex)
  907. {
  908. if (parameterIndex == InvalidIndex)
  909. {
  910. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  911. return false;
  912. }
  913. bool value = false;
  914. if (m_animGraphInstance)
  915. {
  916. m_animGraphInstance->GetParameterValueAsBool(parameterIndex, &value);
  917. }
  918. return value;
  919. }
  920. //////////////////////////////////////////////////////////////////////////
  921. AZStd::string AnimGraphComponent::GetParameterString(size_t parameterIndex)
  922. {
  923. if (parameterIndex == InvalidIndex)
  924. {
  925. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  926. return AZStd::string();
  927. }
  928. if (m_animGraphInstance)
  929. {
  930. MCore::AttributeString* param = m_animGraphInstance->GetParameterValueChecked<MCore::AttributeString>(parameterIndex);
  931. if (param)
  932. {
  933. return AZStd::string(param->GetValue().c_str());
  934. }
  935. }
  936. return AZStd::string();
  937. }
  938. //////////////////////////////////////////////////////////////////////////
  939. AZ::Vector2 AnimGraphComponent::GetParameterVector2(size_t parameterIndex)
  940. {
  941. if (parameterIndex == InvalidIndex)
  942. {
  943. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  944. return AZ::Vector2::CreateZero();
  945. }
  946. if (m_animGraphInstance)
  947. {
  948. AZ::Vector2 value;
  949. m_animGraphInstance->GetVector2ParameterValue(parameterIndex, &value);
  950. return value;
  951. }
  952. return AZ::Vector2::CreateZero();
  953. }
  954. //////////////////////////////////////////////////////////////////////////
  955. AZ::Vector3 AnimGraphComponent::GetParameterVector3(size_t parameterIndex)
  956. {
  957. if (parameterIndex == InvalidIndex)
  958. {
  959. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  960. return AZ::Vector3::CreateZero();
  961. }
  962. if (m_animGraphInstance)
  963. {
  964. AZ::Vector3 value;
  965. m_animGraphInstance->GetVector3ParameterValue(parameterIndex, &value);
  966. return value;
  967. }
  968. return AZ::Vector3::CreateZero();
  969. }
  970. //////////////////////////////////////////////////////////////////////////
  971. AZ::Vector3 AnimGraphComponent::GetParameterRotationEuler(size_t parameterIndex)
  972. {
  973. if (parameterIndex == InvalidIndex)
  974. {
  975. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  976. return AZ::Vector3::CreateZero();
  977. }
  978. if (m_animGraphInstance)
  979. {
  980. AZ::Quaternion value;
  981. m_animGraphInstance->GetRotationParameterValue(parameterIndex, &value);
  982. return MCore::AzQuaternionToEulerAngles(value);
  983. }
  984. return AZ::Vector3::CreateZero();
  985. }
  986. //////////////////////////////////////////////////////////////////////////
  987. AZ::Quaternion AnimGraphComponent::GetParameterRotation(size_t parameterIndex)
  988. {
  989. if (parameterIndex == InvalidIndex)
  990. {
  991. AZ_Warning("EMotionFX", false, "Invalid anim graph parameter index: %zu", parameterIndex);
  992. return AZ::Quaternion::CreateZero();
  993. }
  994. if (m_animGraphInstance)
  995. {
  996. AZ::Quaternion value;
  997. m_animGraphInstance->GetRotationParameterValue(parameterIndex, &value);
  998. return value;
  999. }
  1000. return AZ::Quaternion::CreateIdentity();
  1001. }
  1002. //////////////////////////////////////////////////////////////////////////
  1003. float AnimGraphComponent::GetNamedParameterFloat(const char* parameterName)
  1004. {
  1005. if (m_animGraphInstance)
  1006. {
  1007. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1008. if (parameterIndex.IsSuccess())
  1009. {
  1010. return GetParameterFloat(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1011. }
  1012. }
  1013. return 0.f;
  1014. }
  1015. //////////////////////////////////////////////////////////////////////////
  1016. bool AnimGraphComponent::GetNamedParameterBool(const char* parameterName)
  1017. {
  1018. if (m_animGraphInstance)
  1019. {
  1020. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1021. if (parameterIndex.IsSuccess())
  1022. {
  1023. return GetParameterBool(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1024. }
  1025. }
  1026. return false;
  1027. }
  1028. //////////////////////////////////////////////////////////////////////////
  1029. AZStd::string AnimGraphComponent::GetNamedParameterString(const char* parameterName)
  1030. {
  1031. if (m_animGraphInstance)
  1032. {
  1033. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1034. if (parameterIndex.IsSuccess())
  1035. {
  1036. return GetParameterString(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1037. }
  1038. }
  1039. return AZStd::string();
  1040. }
  1041. //////////////////////////////////////////////////////////////////////////
  1042. AZ::Vector2 AnimGraphComponent::GetNamedParameterVector2(const char* parameterName)
  1043. {
  1044. if (m_animGraphInstance)
  1045. {
  1046. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1047. if (parameterIndex.IsSuccess())
  1048. {
  1049. return GetParameterVector2(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1050. }
  1051. }
  1052. return AZ::Vector2::CreateZero();
  1053. }
  1054. //////////////////////////////////////////////////////////////////////////
  1055. AZ::Vector3 AnimGraphComponent::GetNamedParameterVector3(const char* parameterName)
  1056. {
  1057. if (m_animGraphInstance)
  1058. {
  1059. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1060. if (parameterIndex.IsSuccess())
  1061. {
  1062. return GetParameterVector3(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1063. }
  1064. }
  1065. return AZ::Vector3::CreateZero();
  1066. }
  1067. //////////////////////////////////////////////////////////////////////////
  1068. AZ::Vector3 AnimGraphComponent::GetNamedParameterRotationEuler(const char* parameterName)
  1069. {
  1070. if (m_animGraphInstance)
  1071. {
  1072. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1073. if (parameterIndex.IsSuccess())
  1074. {
  1075. return GetParameterRotationEuler(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1076. }
  1077. }
  1078. return AZ::Vector3::CreateZero();
  1079. }
  1080. //////////////////////////////////////////////////////////////////////////
  1081. AZ::Quaternion AnimGraphComponent::GetNamedParameterRotation(const char* parameterName)
  1082. {
  1083. if (m_animGraphInstance)
  1084. {
  1085. const AZ::Outcome<size_t> parameterIndex = m_animGraphInstance->FindParameterIndex(parameterName);
  1086. if (parameterIndex.IsSuccess())
  1087. {
  1088. return GetParameterRotation(static_cast<AZ::u32>(parameterIndex.GetValue()));
  1089. }
  1090. }
  1091. return AZ::Quaternion::CreateIdentity();
  1092. }
  1093. //////////////////////////////////////////////////////////////////////////
  1094. void AnimGraphComponent::SyncAnimGraph(AZ::EntityId leaderEntityId)
  1095. {
  1096. if (m_animGraphInstance)
  1097. {
  1098. AnimGraphComponentNotificationBus::Event(
  1099. leaderEntityId,
  1100. &AnimGraphComponentNotificationBus::Events::OnAnimGraphSynced,
  1101. m_animGraphInstance.get());
  1102. }
  1103. }
  1104. void AnimGraphComponent::DesyncAnimGraph(AZ::EntityId leaderEntityId)
  1105. {
  1106. if (m_animGraphInstance)
  1107. {
  1108. AnimGraphComponentNotificationBus::Event(
  1109. leaderEntityId,
  1110. &AnimGraphComponentNotificationBus::Events::OnAnimGraphDesynced,
  1111. m_animGraphInstance.get());
  1112. }
  1113. }
  1114. void AnimGraphComponent::SetActiveMotionSet(const char* activeMotionSetName)
  1115. {
  1116. m_configuration.m_activeMotionSetName = activeMotionSetName;
  1117. CheckCreateAnimGraphInstance();
  1118. }
  1119. } // namespace Integration
  1120. } // namespace EMotionFXAnimation