3
0

CompoundSplineTrack.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/Math/Transform.h>
  10. #include "CompoundSplineTrack.h"
  11. #include "AnimSplineTrack.h"
  12. #include "Maestro/Types/AnimParamType.h"
  13. #include "Maestro/Types/AnimValueType.h"
  14. CCompoundSplineTrack::CCompoundSplineTrack(int nDims, AnimValueType inValueType, CAnimParamType subTrackParamTypes[MAX_SUBTRACKS], bool expanded)
  15. : m_refCount(0)
  16. , m_expanded(expanded)
  17. {
  18. assert(nDims > 0 && nDims <= MAX_SUBTRACKS);
  19. m_node = nullptr;
  20. m_nDimensions = nDims;
  21. m_valueType = inValueType;
  22. m_nParamType = AnimParamType::Invalid;
  23. m_flags = 0;
  24. m_subTracks.resize(MAX_SUBTRACKS);
  25. for (int i = 0; i < m_nDimensions; i++)
  26. {
  27. m_subTracks[i].reset(aznew C2DSplineTrack());
  28. m_subTracks[i]->SetParameterType(subTrackParamTypes[i]);
  29. if (inValueType == AnimValueType::RGB)
  30. {
  31. m_subTracks[i]->SetKeyValueRange(0.0f, 255.f);
  32. }
  33. }
  34. m_subTrackNames.resize(MAX_SUBTRACKS);
  35. m_subTrackNames[0] = "X";
  36. m_subTrackNames[1] = "Y";
  37. m_subTrackNames[2] = "Z";
  38. m_subTrackNames[3] = "W";
  39. #ifdef MOVIESYSTEM_SUPPORT_EDITING
  40. m_bCustomColorSet = false;
  41. #endif
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. // Need default constructor for AZ Serialization
  45. CCompoundSplineTrack::CCompoundSplineTrack()
  46. : m_refCount(0)
  47. , m_nDimensions(0)
  48. , m_valueType(AnimValueType::Float)
  49. #ifdef MOVIESYSTEM_SUPPORT_EDITING
  50. , m_bCustomColorSet(false)
  51. #endif
  52. , m_expanded(false)
  53. {
  54. }
  55. void CCompoundSplineTrack::SetNode(IAnimNode* node)
  56. {
  57. m_node = node;
  58. for (int i = 0; i < m_nDimensions; i++)
  59. {
  60. m_subTracks[i]->SetNode(node);
  61. }
  62. }
  63. //////////////////////////////////////////////////////////////////////////
  64. void CCompoundSplineTrack::SetTimeRange(const Range& timeRange)
  65. {
  66. for (int i = 0; i < m_nDimensions; i++)
  67. {
  68. m_subTracks[i]->SetTimeRange(timeRange);
  69. }
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. /// @deprecated Serialization for Sequence data in Component Entity Sequences now occurs through AZ::SerializeContext and the Sequence Component
  73. bool CCompoundSplineTrack::Serialize(XmlNodeRef& xmlNode, bool bLoading, bool bLoadEmptyTracks /*=true */)
  74. {
  75. #ifdef MOVIESYSTEM_SUPPORT_EDITING
  76. if (bLoading)
  77. {
  78. int flags = m_flags;
  79. xmlNode->getAttr("Flags", flags);
  80. SetFlags(flags);
  81. xmlNode->getAttr("HasCustomColor", m_bCustomColorSet);
  82. if (m_bCustomColorSet)
  83. {
  84. unsigned int abgr;
  85. xmlNode->getAttr("CustomColor", abgr);
  86. m_customColor = ColorB(abgr);
  87. }
  88. xmlNode->getAttr("Id", m_id);
  89. }
  90. else
  91. {
  92. int flags = GetFlags();
  93. xmlNode->setAttr("Flags", flags);
  94. xmlNode->setAttr("HasCustomColor", m_bCustomColorSet);
  95. if (m_bCustomColorSet)
  96. {
  97. xmlNode->setAttr("CustomColor", m_customColor.pack_abgr8888());
  98. }
  99. xmlNode->setAttr("Id", m_id);
  100. }
  101. #endif
  102. for (int i = 0; i < m_nDimensions; i++)
  103. {
  104. XmlNodeRef subTrackNode;
  105. if (bLoading)
  106. {
  107. subTrackNode = xmlNode->getChild(i);
  108. }
  109. else
  110. {
  111. subTrackNode = xmlNode->newChild("NewSubTrack");
  112. }
  113. m_subTracks[i]->Serialize(subTrackNode, bLoading, bLoadEmptyTracks);
  114. }
  115. return true;
  116. }
  117. //////////////////////////////////////////////////////////////////////////
  118. bool CCompoundSplineTrack::SerializeSelection(XmlNodeRef& xmlNode, bool bLoading, bool bCopySelected /*=false*/, float fTimeOffset /*=0*/)
  119. {
  120. for (int i = 0; i < m_nDimensions; i++)
  121. {
  122. XmlNodeRef subTrackNode;
  123. if (bLoading)
  124. {
  125. subTrackNode = xmlNode->getChild(i);
  126. }
  127. else
  128. {
  129. subTrackNode = xmlNode->newChild("NewSubTrack");
  130. }
  131. m_subTracks[i]->SerializeSelection(subTrackNode, bLoading, bCopySelected, fTimeOffset);
  132. }
  133. return true;
  134. }
  135. //////////////////////////////////////////////////////////////////////////
  136. void CCompoundSplineTrack::GetValue(float time, float& value, bool applyMultiplier)
  137. {
  138. for (int i = 0; i < 1 && i < m_nDimensions; i++)
  139. {
  140. m_subTracks[i]->GetValue(time, value, applyMultiplier);
  141. }
  142. }
  143. //////////////////////////////////////////////////////////////////////////
  144. void CCompoundSplineTrack::GetValue(float time, Vec3& value, bool applyMultiplier)
  145. {
  146. for (int i = 0; i < m_nDimensions; i++)
  147. {
  148. float v = value[i];
  149. m_subTracks[i]->GetValue(time, v, applyMultiplier);
  150. value[i] = v;
  151. }
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. void CCompoundSplineTrack::GetValue(float time, Vec4& value, bool applyMultiplier)
  155. {
  156. for (int i = 0; i < m_nDimensions; i++)
  157. {
  158. float v = value[i];
  159. m_subTracks[i]->GetValue(time, v, applyMultiplier);
  160. value[i] = v;
  161. }
  162. }
  163. //////////////////////////////////////////////////////////////////////////
  164. void CCompoundSplineTrack::GetValue(float time, Quat& value)
  165. {
  166. if (m_nDimensions == 3)
  167. {
  168. // Assume Euler Angles XYZ
  169. float angles[3] = {0, 0, 0};
  170. for (int i = 0; i < m_nDimensions; i++)
  171. {
  172. m_subTracks[i]->GetValue(time, angles[i]);
  173. }
  174. value = Quat::CreateRotationZYX(Ang3(DEG2RAD(angles[0]), DEG2RAD(angles[1]), DEG2RAD(angles[2])));
  175. }
  176. else
  177. {
  178. assert(0);
  179. value.SetIdentity();
  180. }
  181. }
  182. //////////////////////////////////////////////////////////////////////////
  183. void CCompoundSplineTrack::SetValue(float time, const float& value, bool bDefault, bool applyMultiplier)
  184. {
  185. for (int i = 0; i < m_nDimensions; i++)
  186. {
  187. m_subTracks[i]->SetValue(time, value, bDefault, applyMultiplier);
  188. }
  189. }
  190. //////////////////////////////////////////////////////////////////////////
  191. void CCompoundSplineTrack::SetValue(float time, const Vec3& value, bool bDefault, bool applyMultiplier)
  192. {
  193. for (int i = 0; i < m_nDimensions; i++)
  194. {
  195. m_subTracks[i]->SetValue(time, value[i], bDefault, applyMultiplier);
  196. }
  197. }
  198. //////////////////////////////////////////////////////////////////////////
  199. void CCompoundSplineTrack::SetValue(float time, const Vec4& value, bool bDefault, bool applyMultiplier)
  200. {
  201. for (int i = 0; i < m_nDimensions; i++)
  202. {
  203. m_subTracks[i]->SetValue(time, value[i], bDefault, applyMultiplier);
  204. }
  205. }
  206. //////////////////////////////////////////////////////////////////////////
  207. void CCompoundSplineTrack::SetValue(float time, const Quat& value, bool bDefault)
  208. {
  209. if (m_nDimensions == 3)
  210. {
  211. // Assume Euler Angles XYZ
  212. Ang3 angles = Ang3::GetAnglesXYZ(value);
  213. for (int i = 0; i < 3; i++)
  214. {
  215. float degree = RAD2DEG(angles[i]);
  216. if (false == bDefault)
  217. {
  218. // Try to prefer the shortest path of rotation.
  219. float degree0 = 0.0f;
  220. m_subTracks[i]->GetValue(time, degree0);
  221. degree = PreferShortestRotPath(degree, degree0);
  222. }
  223. m_subTracks[i]->SetValue(time, degree, bDefault);
  224. }
  225. }
  226. else
  227. {
  228. assert(0);
  229. }
  230. }
  231. //////////////////////////////////////////////////////////////////////////
  232. void CCompoundSplineTrack::OffsetKeyPosition(const Vec3& offset)
  233. {
  234. if (m_nDimensions == 3)
  235. {
  236. for (int i = 0; i < 3; i++)
  237. {
  238. IAnimTrack* pSubTrack = m_subTracks[i].get();
  239. // Iterate over all keys.
  240. for (int k = 0, num = pSubTrack->GetNumKeys(); k < num; k++)
  241. {
  242. // Offset each key.
  243. float time = pSubTrack->GetKeyTime(k);
  244. float value = 0;
  245. pSubTrack->GetValue(time, value);
  246. value = value + offset[i];
  247. pSubTrack->SetValue(time, value);
  248. }
  249. }
  250. }
  251. else
  252. {
  253. assert(0);
  254. }
  255. }
  256. //////////////////////////////////////////////////////////////////////////
  257. void CCompoundSplineTrack::UpdateKeyDataAfterParentChanged(const AZ::Transform& oldParentWorldTM, const AZ::Transform& newParentWorldTM)
  258. {
  259. // Only update the position tracks
  260. if (m_nParamType.GetType() != AnimParamType::Position)
  261. {
  262. return;
  263. }
  264. AZ_Assert(m_nDimensions == 3, "Expected 3 dimensions, position, rotation or scale.");
  265. struct KeyValues
  266. {
  267. KeyValues(int i, float t, float v) : index(i), time(t), value(v) {};
  268. int index;
  269. float time;
  270. float value;
  271. };
  272. // Don't actually set the key data until we are done calculating all the new values.
  273. // In the case where not all 3 tracks have key data, data from other keys may be referenced
  274. // and used. So we don't want to muck with those other keys until we are done transforming all of
  275. // the key data.
  276. AZStd::vector<KeyValues> newKeyValues;
  277. // Collect all times that have key data on any track.
  278. for (int subTrackIndex = 0; subTrackIndex < 3; subTrackIndex++)
  279. {
  280. IAnimTrack* subTrack = m_subTracks[subTrackIndex].get();
  281. for (int k = 0, num = subTrack->GetNumKeys(); k < num; k++)
  282. {
  283. // If this key time is not already in the list, add it.
  284. float time = subTrack->GetKeyTime(k);
  285. // Create a 3 float vector with values from the 3 tracks.
  286. AZ::Vector3 vector;
  287. for (int i = 0; i < 3; i++)
  288. {
  289. float value = 0;
  290. m_subTracks[i]->GetValue(time, value);
  291. vector.SetElement(i, value);
  292. }
  293. // Use the old parent world transform to get the current key data into world space.
  294. AZ::Vector3 worldPosition = oldParentWorldTM.GetTranslation() + vector;
  295. // Get the world location into local space relative to the new parent.
  296. vector = worldPosition - newParentWorldTM.GetTranslation();
  297. // Store the new key data in a list to be set to keys later.
  298. newKeyValues.push_back(KeyValues(subTrackIndex, time, vector.GetElement(subTrackIndex)));
  299. }
  300. }
  301. // Set key data for each time gathered from the keys.
  302. for (auto valuePair : newKeyValues)
  303. {
  304. m_subTracks[valuePair.index]->SetValue(valuePair.time, valuePair.value);
  305. }
  306. }
  307. //////////////////////////////////////////////////////////////////////////
  308. IAnimTrack* CCompoundSplineTrack::GetSubTrack(int nIndex) const
  309. {
  310. assert(nIndex >= 0 && nIndex < m_nDimensions);
  311. return m_subTracks[nIndex].get();
  312. }
  313. //////////////////////////////////////////////////////////////////////////
  314. AZStd::string CCompoundSplineTrack::GetSubTrackName(int nIndex) const
  315. {
  316. assert(nIndex >= 0 && nIndex < m_nDimensions);
  317. return m_subTrackNames[nIndex];
  318. }
  319. //////////////////////////////////////////////////////////////////////////
  320. void CCompoundSplineTrack::SetSubTrackName(int nIndex, const char* name)
  321. {
  322. assert(nIndex >= 0 && nIndex < m_nDimensions);
  323. assert(name);
  324. m_subTrackNames[nIndex] = name;
  325. }
  326. //////////////////////////////////////////////////////////////////////////
  327. int CCompoundSplineTrack::GetNumKeys() const
  328. {
  329. int nKeys = 0;
  330. for (int i = 0; i < m_nDimensions; i++)
  331. {
  332. nKeys += m_subTracks[i]->GetNumKeys();
  333. }
  334. return nKeys;
  335. }
  336. //////////////////////////////////////////////////////////////////////////
  337. bool CCompoundSplineTrack::HasKeys() const
  338. {
  339. for (int i = 0; i < m_nDimensions; i++)
  340. {
  341. if (m_subTracks[i]->GetNumKeys())
  342. {
  343. return true;
  344. }
  345. }
  346. return false;
  347. }
  348. float CCompoundSplineTrack::PreferShortestRotPath(float degree, float degree0) const
  349. {
  350. // Assumes the degree is in (-PI, PI).
  351. assert(-181.0f < degree && degree < 181.0f);
  352. float degree00 = degree0;
  353. degree0 = fmod_tpl(degree0, 360.0f);
  354. float n = (degree00 - degree0) / 360.0f;
  355. float degreeAlt;
  356. if (degree >= 0)
  357. {
  358. degreeAlt = degree - 360.0f;
  359. }
  360. else
  361. {
  362. degreeAlt = degree + 360.0f;
  363. }
  364. if (fabs(degreeAlt - degree0) < fabs(degree - degree0))
  365. {
  366. return degreeAlt + n * 360.0f;
  367. }
  368. else
  369. {
  370. return degree + n * 360.0f;
  371. }
  372. }
  373. int CCompoundSplineTrack::GetSubTrackIndex(int& key) const
  374. {
  375. assert(key >= 0 && key < GetNumKeys());
  376. int count = 0;
  377. for (int i = 0; i < m_nDimensions; i++)
  378. {
  379. if (key < count + m_subTracks[i]->GetNumKeys())
  380. {
  381. key = key - count;
  382. return i;
  383. }
  384. count += m_subTracks[i]->GetNumKeys();
  385. }
  386. return -1;
  387. }
  388. void CCompoundSplineTrack::RemoveKey(int num)
  389. {
  390. assert(num >= 0 && num < GetNumKeys());
  391. int i = GetSubTrackIndex(num);
  392. assert(i >= 0);
  393. if (i < 0)
  394. {
  395. return;
  396. }
  397. m_subTracks[i]->RemoveKey(num);
  398. }
  399. void CCompoundSplineTrack::GetKeyInfo(int key, const char*& description, float& duration)
  400. {
  401. static char str[64];
  402. duration = 0;
  403. description = str;
  404. const char* subDesc = NULL;
  405. float time = GetKeyTime(key);
  406. int m = 0;
  407. /// Using the time obtained, combine descriptions from keys of the same time
  408. /// in sub-tracks if any into one compound description.
  409. str[0] = 0;
  410. // A head case
  411. for (m = 0; m < m_subTracks[0]->GetNumKeys(); ++m)
  412. {
  413. if (m_subTracks[0]->GetKeyTime(m) == time)
  414. {
  415. float dummy;
  416. m_subTracks[0]->GetKeyInfo(m, subDesc, dummy);
  417. azstrcat(str, AZ_ARRAY_SIZE(str), subDesc);
  418. break;
  419. }
  420. }
  421. if (m == m_subTracks[0]->GetNumKeys())
  422. {
  423. azstrcat(str, AZ_ARRAY_SIZE(str), m_subTrackNames[0].c_str());
  424. }
  425. // Tail cases
  426. for (int i = 1; i < GetSubTrackCount(); ++i)
  427. {
  428. azstrcat(str, AZ_ARRAY_SIZE(str), ",");
  429. for (m = 0; m < m_subTracks[i]->GetNumKeys(); ++m)
  430. {
  431. if (m_subTracks[i]->GetKeyTime(m) == time)
  432. {
  433. float dummy;
  434. m_subTracks[i]->GetKeyInfo(m, subDesc, dummy);
  435. azstrcat(str, AZ_ARRAY_SIZE(str), subDesc);
  436. break;
  437. }
  438. }
  439. if (m == m_subTracks[i]->GetNumKeys())
  440. {
  441. azstrcat(str, AZ_ARRAY_SIZE(str), m_subTrackNames[i].c_str());
  442. }
  443. }
  444. }
  445. float CCompoundSplineTrack::GetKeyTime(int index) const
  446. {
  447. assert(index >= 0 && index < GetNumKeys());
  448. int i = GetSubTrackIndex(index);
  449. assert(i >= 0);
  450. if (i < 0)
  451. {
  452. return 0;
  453. }
  454. return m_subTracks[i]->GetKeyTime(index);
  455. }
  456. void CCompoundSplineTrack::SetKeyTime(int index, float time)
  457. {
  458. assert(index >= 0 && index < GetNumKeys());
  459. int i = GetSubTrackIndex(index);
  460. assert(i >= 0);
  461. if (i < 0)
  462. {
  463. return;
  464. }
  465. m_subTracks[i]->SetKeyTime(index, time);
  466. }
  467. bool CCompoundSplineTrack::IsKeySelected(int key) const
  468. {
  469. assert(key >= 0 && key < GetNumKeys());
  470. int i = GetSubTrackIndex(key);
  471. assert(i >= 0);
  472. if (i < 0)
  473. {
  474. return false;
  475. }
  476. return m_subTracks[i]->IsKeySelected(key);
  477. }
  478. void CCompoundSplineTrack::SelectKey(int key, bool select)
  479. {
  480. assert(key >= 0 && key < GetNumKeys());
  481. int i = GetSubTrackIndex(key);
  482. assert(i >= 0);
  483. if (i < 0)
  484. {
  485. return;
  486. }
  487. float keyTime = m_subTracks[i]->GetKeyTime(key);
  488. // In the case of compound tracks, animators want to
  489. // select all keys of the same time in the sub-tracks together.
  490. const float timeEpsilon = 0.001f;
  491. for (int k = 0; k < m_nDimensions; ++k)
  492. {
  493. for (int m = 0; m < m_subTracks[k]->GetNumKeys(); ++m)
  494. {
  495. if (fabs(m_subTracks[k]->GetKeyTime(m) - keyTime) < timeEpsilon)
  496. {
  497. m_subTracks[k]->SelectKey(m, select);
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. int CCompoundSplineTrack::NextKeyByTime(int key) const
  504. {
  505. assert(key >= 0 && key < GetNumKeys());
  506. float time = GetKeyTime(key);
  507. int count = 0, result = -1;
  508. float timeNext = FLT_MAX;
  509. for (int i = 0; i < GetSubTrackCount(); ++i)
  510. {
  511. for (int k = 0; k < m_subTracks[i]->GetNumKeys(); ++k)
  512. {
  513. float t = m_subTracks[i]->GetKeyTime(k);
  514. if (t > time)
  515. {
  516. if (t < timeNext)
  517. {
  518. timeNext = t;
  519. result = count + k;
  520. }
  521. break;
  522. }
  523. }
  524. count += m_subTracks[i]->GetNumKeys();
  525. }
  526. return result;
  527. }
  528. //////////////////////////////////////////////////////////////////////////
  529. void CCompoundSplineTrack::SetExpanded(bool expanded)
  530. {
  531. m_expanded = expanded;
  532. }
  533. //////////////////////////////////////////////////////////////////////////
  534. bool CCompoundSplineTrack::GetExpanded() const
  535. {
  536. return m_expanded;
  537. }
  538. //////////////////////////////////////////////////////////////////////////
  539. unsigned int CCompoundSplineTrack::GetId() const
  540. {
  541. return m_id;
  542. }
  543. //////////////////////////////////////////////////////////////////////////
  544. void CCompoundSplineTrack::SetId(unsigned int id)
  545. {
  546. m_id = id;
  547. }
  548. //////////////////////////////////////////////////////////////////////////
  549. static bool CompoundSplineTrackVersionConverter(
  550. AZ::SerializeContext& serializeContext,
  551. AZ::SerializeContext::DataElementNode& rootElement)
  552. {
  553. if (rootElement.GetVersion() < 4)
  554. {
  555. rootElement.AddElement(serializeContext, "BaseClass1", azrtti_typeid<IAnimTrack>());
  556. }
  557. return true;
  558. }
  559. void CCompoundSplineTrack::Reflect(AZ::ReflectContext* context)
  560. {
  561. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  562. {
  563. serializeContext->Class<CCompoundSplineTrack, IAnimTrack>()
  564. ->Version(4, &CompoundSplineTrackVersionConverter)
  565. ->Field("Flags", &CCompoundSplineTrack::m_flags)
  566. ->Field("ParamType", &CCompoundSplineTrack::m_nParamType)
  567. ->Field("NumSubTracks", &CCompoundSplineTrack::m_nDimensions)
  568. ->Field("SubTracks", &CCompoundSplineTrack::m_subTracks)
  569. ->Field("SubTrackNames", &CCompoundSplineTrack::m_subTrackNames)
  570. ->Field("ValueType", &CCompoundSplineTrack::m_valueType)
  571. ->Field("Expanded", &CCompoundSplineTrack::m_expanded)
  572. ->Field("Id", &CCompoundSplineTrack::m_id);
  573. }
  574. }