AnimGraphComponent.cpp 54 KB

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