TransformComponent.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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/Component/ComponentApplication.h>
  9. #include <AzCore/Math/MathUtils.h>
  10. #include <AzCore/Math/Matrix3x3.h>
  11. #include <AzCore/Math/Random.h>
  12. #include <AzCore/Serialization/Utils.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzCore/UserSettings/UserSettingsComponent.h>
  15. #include <AzFramework/Application/Application.h>
  16. #include <AzFramework/Components/TransformComponent.h>
  17. #include <AzToolsFramework/Application/ToolsApplication.h>
  18. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  19. #include <AzToolsFramework/ToolsComponents/TransformComponent.h>
  20. #include <AzToolsFramework/Entity/PrefabEditorEntityOwnershipInterface.h>
  21. #include <Prefab/PrefabTestFixture.h>
  22. #include <AZTestShared/Math/MathTestHelpers.h>
  23. using namespace AZ;
  24. using namespace AzFramework;
  25. namespace UnitTest
  26. {
  27. // Fixture base class for AzFramework::TransformComponent tests.
  28. class TransformComponentApplication
  29. : public LeakDetectionFixture
  30. {
  31. protected:
  32. void SetUp() override
  33. {
  34. LeakDetectionFixture::SetUp();
  35. ComponentApplication::Descriptor desc;
  36. desc.m_useExistingAllocator = true;
  37. AZ::ComponentApplication::StartupParameters startupParameters;
  38. startupParameters.m_loadSettingsRegistry = false;
  39. m_app.Start(desc, startupParameters);
  40. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  41. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  42. // in the unit tests.
  43. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  44. }
  45. void TearDown() override
  46. {
  47. m_app.Stop();
  48. LeakDetectionFixture::TearDown();
  49. }
  50. AzFramework::Application m_app;
  51. };
  52. // Runs a series of tests on TransformComponent.
  53. class TransformComponentUberTest
  54. : public TransformComponentApplication
  55. , public TransformNotificationBus::Handler
  56. {
  57. public:
  58. //////////////////////////////////////////////////////////////////////////////
  59. // TransformNotificationBus
  60. void OnTransformChanged(const Transform& local, const Transform& world) override
  61. {
  62. AZ_TEST_ASSERT(m_checkWorldTM == world);
  63. AZ_TEST_ASSERT(m_checkLocalTM == local);
  64. }
  65. /// Called when the parent of an entity has changed. When the old/new parent are invalid the !EntityId.IsValid
  66. void OnParentChanged(EntityId oldParent, EntityId newParent) override
  67. {
  68. AZ_TEST_ASSERT(m_checkOldParentId == oldParent);
  69. AZ_TEST_ASSERT(m_checkNewParentId == newParent);
  70. }
  71. //////////////////////////////////////////////////////////////////////////////
  72. void run()
  73. {
  74. m_checkWorldTM = Transform::CreateIdentity();
  75. m_checkLocalTM = Transform::CreateIdentity();
  76. // Create test entity
  77. Entity childEntity, parentEntity;
  78. TransformComponent* childTransformComponent = childEntity.CreateComponent<TransformComponent>();
  79. TransformComponent* parentTranformComponent = parentEntity.CreateComponent<TransformComponent>();
  80. (void)parentTranformComponent;
  81. TransformNotificationBus::Handler::BusConnect(childEntity.GetId());
  82. childEntity.Init();
  83. parentEntity.Init();
  84. // We bind transform interface only when entity is activated
  85. AZ_TEST_ASSERT(childEntity.GetTransform() == nullptr);
  86. childEntity.Activate();
  87. TransformInterface* childTransform = childEntity.GetTransform();
  88. parentEntity.Activate();
  89. TransformInterface* parentTransform = parentEntity.GetTransform();
  90. parentTransform->SetWorldTM(Transform::CreateTranslation(Vector3(1.0f, 0.0f, 0.0f)));
  91. // Check validity of transform interface and initial transforms
  92. AZ_TEST_ASSERT(childTransform == static_cast<TransformInterface*>(childTransformComponent));
  93. AZ_TEST_ASSERT(childTransform->GetWorldTM() == m_checkWorldTM);
  94. AZ_TEST_ASSERT(childTransform->GetLocalTM() == m_checkLocalTM);
  95. AZ_TEST_ASSERT(childTransform->GetParentId() == m_checkNewParentId);
  96. // Modify the local (and world) matrix
  97. m_checkLocalTM = Transform::CreateTranslation(Vector3(5.0f, 0.0f, 0.0f));
  98. m_checkWorldTM = m_checkLocalTM;
  99. childTransform->SetWorldTM(m_checkWorldTM);
  100. // Parent the child object
  101. m_checkNewParentId = parentEntity.GetId();
  102. m_checkLocalTM *= parentTransform->GetWorldTM().GetInverse(); // the set parent will move the child object into parent space
  103. childTransform->SetParent(m_checkNewParentId);
  104. // Deactivate the parent (this essentially removes the parent)
  105. m_checkNewParentId.SetInvalid();
  106. m_checkOldParentId = parentEntity.GetId();
  107. m_checkLocalTM = m_checkWorldTM; // we will remove the parent
  108. parentEntity.Deactivate();
  109. TransformNotificationBus::Handler::BusDisconnect(childEntity.GetId());
  110. // now we should we without a parent
  111. childEntity.Deactivate();
  112. }
  113. Transform m_checkWorldTM;
  114. Transform m_checkLocalTM;
  115. EntityId m_checkOldParentId;
  116. EntityId m_checkNewParentId;
  117. };
  118. TEST_F(TransformComponentUberTest, Test)
  119. {
  120. run();
  121. }
  122. class TransformComponentChildNotificationTest
  123. : public TransformComponentApplication
  124. , public TransformNotificationBus::Handler
  125. {
  126. public:
  127. void OnChildAdded(EntityId child) override
  128. {
  129. AZ_TEST_ASSERT(child == m_checkChildId);
  130. m_onChildAddedCount++;
  131. }
  132. void OnChildRemoved(EntityId child) override
  133. {
  134. AZ_TEST_ASSERT(child == m_checkChildId);
  135. m_onChildRemovedCount++;
  136. }
  137. void run()
  138. {
  139. // Create ID for parent and begin listening for child add/remove notifications
  140. AZ::EntityId parentId = Entity::MakeId();
  141. TransformNotificationBus::Handler::BusConnect(parentId);
  142. Entity childEntity;
  143. AZ::TransformConfig transformConfig;
  144. transformConfig.m_isStatic = false;
  145. childEntity.CreateComponent<TransformComponent>()->SetConfiguration(transformConfig);
  146. m_checkChildId = childEntity.GetId();
  147. childEntity.Init();
  148. childEntity.Activate();
  149. TransformInterface* childTransform = childEntity.GetTransform();
  150. // Expected number of notifications to OnChildAdded and OnChildRemoved
  151. int checkAddCount = 0;
  152. int checkRemoveCount = 0;
  153. // Changing to target parentId should notify add
  154. AZ_TEST_ASSERT(m_onChildAddedCount == checkAddCount);
  155. childTransform->SetParent(parentId);
  156. checkAddCount++;
  157. AZ_TEST_ASSERT(m_onChildAddedCount == checkAddCount);
  158. // Deactivating child should notify removal
  159. AZ_TEST_ASSERT(m_onChildRemovedCount == checkRemoveCount);
  160. childEntity.Deactivate();
  161. checkRemoveCount++;
  162. AZ_TEST_ASSERT(m_onChildRemovedCount == checkRemoveCount);
  163. // Activating child (while parentId is set) should notify add
  164. AZ_TEST_ASSERT(m_onChildAddedCount == checkAddCount);
  165. childEntity.Activate();
  166. checkAddCount++;
  167. AZ_TEST_ASSERT(m_onChildAddedCount == checkAddCount);
  168. // Setting parent invalid should notify removal
  169. AZ_TEST_ASSERT(m_onChildRemovedCount == checkRemoveCount);
  170. childTransform->SetParent(EntityId());
  171. checkRemoveCount++;
  172. AZ_TEST_ASSERT(m_onChildRemovedCount == checkRemoveCount);
  173. TransformNotificationBus::Handler::BusDisconnect(parentId);
  174. childEntity.Deactivate();
  175. }
  176. EntityId m_checkChildId;
  177. int m_onChildAddedCount = 0;
  178. int m_onChildRemovedCount = 0;
  179. };
  180. TEST_F(TransformComponentChildNotificationTest, Test)
  181. {
  182. run();
  183. }
  184. class LookAtTransformTest
  185. : public ::testing::Test
  186. {
  187. public:
  188. void run()
  189. {
  190. // CreateLookAt
  191. AZ::Vector3 lookAtEye(1.0f, 2.0f, 3.0f);
  192. AZ::Vector3 lookAtTarget(10.0f, 5.0f, -5.0f);
  193. AZ::Transform t1 = AZ::Transform::CreateLookAt(lookAtEye, lookAtTarget);
  194. AZ_TEST_ASSERT(t1.GetBasisY().IsClose((lookAtTarget - lookAtEye).GetNormalized()));
  195. AZ_TEST_ASSERT(t1.GetTranslation() == lookAtEye);
  196. AZ_TEST_ASSERT(t1.IsOrthogonal());
  197. AZ_TEST_START_TRACE_SUPPRESSION;
  198. t1 = AZ::Transform::CreateLookAt(lookAtEye, lookAtEye); //degenerate direction
  199. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  200. AZ_TEST_ASSERT(t1.IsOrthogonal());
  201. AZ_TEST_ASSERT(t1 == AZ::Transform::CreateIdentity());
  202. t1 = AZ::Transform::CreateLookAt(lookAtEye, lookAtEye + AZ::Vector3::CreateAxisZ()); //degenerate with up direction
  203. AZ_TEST_ASSERT(t1.GetBasisY().IsClose(AZ::Vector3::CreateAxisZ()));
  204. AZ_TEST_ASSERT(t1.GetTranslation() == lookAtEye);
  205. AZ_TEST_ASSERT(t1.IsOrthogonal());
  206. }
  207. };
  208. TEST_F(LookAtTransformTest, Test)
  209. {
  210. run();
  211. }
  212. // Test TransformComponent's methods of modifying/retrieving underlying translation, rotation and scale transform component.
  213. class TransformComponentTransformMatrixSetGet
  214. : public TransformComponentApplication
  215. {
  216. protected:
  217. void SetUp() override
  218. {
  219. TransformComponentApplication::SetUp();
  220. m_parentEntity = aznew Entity("Parent");
  221. m_parentId = m_parentEntity->GetId();
  222. m_parentEntity->Init();
  223. m_parentEntity->CreateComponent<TransformComponent>();
  224. m_childEntity = aznew Entity("Child");
  225. m_childId = m_childEntity->GetId();
  226. m_childEntity->Init();
  227. m_childEntity->CreateComponent<TransformComponent>();
  228. m_parentEntity->Activate();
  229. m_childEntity->Activate();
  230. TransformBus::Event(m_childId, &TransformBus::Events::SetParent, m_parentId);
  231. }
  232. void TearDown() override
  233. {
  234. m_childEntity->Deactivate();
  235. m_parentEntity->Deactivate();
  236. delete m_childEntity;
  237. delete m_parentEntity;
  238. TransformComponentApplication::TearDown();
  239. }
  240. Entity* m_parentEntity = nullptr;
  241. EntityId m_parentId = EntityId();
  242. Entity* m_childEntity = nullptr;
  243. EntityId m_childId = EntityId();
  244. };
  245. TEST_F(TransformComponentTransformMatrixSetGet, SetLocalX_SimpleValues_Set)
  246. {
  247. float tx = 123.123f;
  248. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalX, tx);
  249. Transform tm;
  250. TransformBus::EventResult(tm, m_childId, &TransformBus::Events::GetLocalTM);
  251. EXPECT_NEAR(tx, tm.GetTranslation().GetX(), AZ::Constants::Tolerance);
  252. }
  253. TEST_F(TransformComponentTransformMatrixSetGet, GetLocalX_SimpleValues_Set)
  254. {
  255. Transform tm;
  256. tm.SetTranslation(AZ::Vector3::CreateAxisX(432.456f));
  257. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, tm);
  258. float tx = 0;
  259. TransformBus::EventResult(tx, m_childId, &TransformBus::Events::GetLocalX);
  260. EXPECT_NEAR(tx, tm.GetTranslation().GetX(), AZ::Constants::Tolerance);
  261. }
  262. TEST_F(TransformComponentTransformMatrixSetGet, SetLocalY_SimpleValues_Set)
  263. {
  264. float ty = 435.676f;
  265. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalY, ty);
  266. Transform tm;
  267. TransformBus::EventResult(tm, m_childId, &TransformBus::Events::GetLocalTM);
  268. EXPECT_NEAR(ty, tm.GetTranslation().GetY(), AZ::Constants::Tolerance);
  269. }
  270. TEST_F(TransformComponentTransformMatrixSetGet, GetLocalY_SimpleValues_Set)
  271. {
  272. Transform tm;
  273. tm.SetTranslation(AZ::Vector3::CreateAxisY(154.754f));
  274. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, tm);
  275. float ty = 0;
  276. TransformBus::EventResult(ty, m_childId, &TransformBus::Events::GetLocalY);
  277. EXPECT_NEAR(ty, tm.GetTranslation().GetY(), AZ::Constants::Tolerance);
  278. }
  279. TEST_F(TransformComponentTransformMatrixSetGet, SetLocalZ_SimpleValues_Set)
  280. {
  281. float tz = 987.456f;
  282. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalZ, tz);
  283. Transform tm;
  284. TransformBus::EventResult(tm, m_childId, &TransformBus::Events::GetLocalTM);
  285. EXPECT_NEAR(tz, tm.GetTranslation().GetZ(), AZ::Constants::Tolerance);
  286. }
  287. TEST_F(TransformComponentTransformMatrixSetGet, GetLocalZ_SimpleValues_Set)
  288. {
  289. Transform tm;
  290. tm.SetTranslation(AZ::Vector3::CreateAxisZ(453.894f));
  291. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, tm);
  292. float tz = 0;
  293. TransformBus::EventResult(tz, m_childId, &TransformBus::Events::GetLocalZ);
  294. EXPECT_NEAR(tz, tm.GetTranslation().GetZ(), AZ::Constants::Tolerance);
  295. }
  296. TEST_F(TransformComponentTransformMatrixSetGet, SetLocalRotation_SimpleValues_Set)
  297. {
  298. // add some scale first
  299. float scale = 1.23f;
  300. Transform tm = Transform::CreateUniformScale(scale);
  301. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, tm);
  302. float rx = 42.435f;
  303. float ry = 19.454f;
  304. float rz = 98.356f;
  305. Vector3 angles(rx, ry, rz);
  306. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalRotation, angles);
  307. TransformBus::EventResult(tm, m_childId, &TransformBus::Events::GetLocalTM);
  308. Matrix3x3 rotateZ = Matrix3x3::CreateRotationZ(rz);
  309. Matrix3x3 rotateY = Matrix3x3::CreateRotationY(ry);
  310. Matrix3x3 rotateX = Matrix3x3::CreateRotationX(rx);
  311. Matrix3x3 finalRotate = rotateX * rotateY * rotateZ;
  312. Vector3 basisX = tm.GetBasisX();
  313. Vector3 expectedBasisX = finalRotate.GetBasisX() * scale;
  314. EXPECT_TRUE(basisX.IsClose(expectedBasisX));
  315. Vector3 basisY = tm.GetBasisY();
  316. Vector3 expectedBasisY = finalRotate.GetBasisY() * scale;
  317. EXPECT_TRUE(basisY.IsClose(expectedBasisY));
  318. Vector3 basisZ = tm.GetBasisZ();
  319. Vector3 expectedBasisZ = finalRotate.GetBasisZ() * scale;
  320. EXPECT_TRUE(basisZ.IsClose(expectedBasisZ));
  321. }
  322. TEST_F(TransformComponentTransformMatrixSetGet, GetLocalRotation_SimpleValues_Return)
  323. {
  324. float rx = 0.66f;
  325. float ry = 1.23f;
  326. float rz = 0.23f;
  327. Matrix3x3 rotateZ = Matrix3x3::CreateRotationZ(rz);
  328. Matrix3x3 rotateY = Matrix3x3::CreateRotationY(ry);
  329. Matrix3x3 rotateX = Matrix3x3::CreateRotationX(rx);
  330. Matrix3x3 finalRotate = rotateX * rotateY * rotateZ;
  331. Transform tm = Transform::CreateFromMatrix3x3(finalRotate);
  332. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, tm);
  333. Vector3 angles;
  334. TransformBus::EventResult(angles, m_childId, &TransformBus::Events::GetLocalRotation);
  335. EXPECT_TRUE(angles.IsClose(Vector3(rx, ry, rz)));
  336. }
  337. TEST_F(TransformComponentTransformMatrixSetGet, SetLocalRotationQuaternion_SimpleValues_Set)
  338. {
  339. float rx = 42.435f;
  340. float ry = 19.454f;
  341. float rz = 98.356f;
  342. Quaternion quatX = Quaternion::CreateRotationX(rx);
  343. Quaternion quatY = Quaternion::CreateRotationY(ry);
  344. Quaternion quatZ = Quaternion::CreateRotationZ(rz);
  345. Quaternion finalQuat = quatX * quatY * quatZ;
  346. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalRotationQuaternion, finalQuat);
  347. Transform tm;
  348. TransformBus::EventResult(tm, m_childId, &TransformBus::Events::GetLocalTM);
  349. Matrix3x3 rotateZ = Matrix3x3::CreateRotationZ(rz);
  350. Matrix3x3 rotateY = Matrix3x3::CreateRotationY(ry);
  351. Matrix3x3 rotateX = Matrix3x3::CreateRotationX(rx);
  352. Matrix3x3 finalRotate = rotateX * rotateY * rotateZ;
  353. Vector3 basisX = tm.GetBasisX();
  354. Vector3 expectedBasisX = finalRotate.GetBasisX();
  355. EXPECT_TRUE(basisX.IsClose(expectedBasisX));
  356. Vector3 basisY = tm.GetBasisY();
  357. Vector3 expectedBasisY = finalRotate.GetBasisY();
  358. EXPECT_TRUE(basisY.IsClose(expectedBasisY));
  359. Vector3 basisZ = tm.GetBasisZ();
  360. Vector3 expectedBasisZ = finalRotate.GetBasisZ();
  361. EXPECT_TRUE(basisZ.IsClose(expectedBasisZ));
  362. }
  363. TEST_F(TransformComponentTransformMatrixSetGet, GetLocalRotationQuaternion_SimpleValues_Return)
  364. {
  365. float rx = 0.66f;
  366. float ry = 1.23f;
  367. float rz = 0.23f;
  368. Matrix3x3 rotateZ = Matrix3x3::CreateRotationZ(rz);
  369. Matrix3x3 rotateY = Matrix3x3::CreateRotationY(ry);
  370. Matrix3x3 rotateX = Matrix3x3::CreateRotationX(rx);
  371. Matrix3x3 finalRotate = rotateX * rotateY * rotateZ;
  372. Transform tm = Transform::CreateFromMatrix3x3(finalRotate);
  373. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, tm);
  374. Quaternion quatX = Quaternion::CreateRotationX(rx);
  375. Quaternion quatY = Quaternion::CreateRotationY(ry);
  376. Quaternion quatZ = Quaternion::CreateRotationZ(rz);
  377. Quaternion expectedQuat = quatX * quatY * quatZ;
  378. Quaternion resultQuat;
  379. TransformBus::EventResult(resultQuat, m_childId, &TransformBus::Events::GetLocalRotationQuaternion);
  380. EXPECT_TRUE(resultQuat.IsClose(expectedQuat));
  381. }
  382. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalX_SimpleValues_Set)
  383. {
  384. float rx = 1.43f;
  385. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalX, rx);
  386. Vector3 localRotation;
  387. TransformBus::EventResult(localRotation, m_childId, &TransformBus::Events::GetLocalRotation);
  388. EXPECT_TRUE(localRotation.IsClose(Vector3(rx, 0.0f, 0.0f)));
  389. }
  390. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalX_RepeatCallingThisFunctionDoesNotSkewScale)
  391. {
  392. // test numeric stability
  393. float rx = 1.43f;
  394. for (int i = 0; i < 100; ++i)
  395. {
  396. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalX, rx);
  397. }
  398. float localScale = FLT_MAX;
  399. TransformBus::EventResult(localScale, m_childId, &TransformBus::Events::GetLocalUniformScale);
  400. EXPECT_NEAR(localScale, 1.0f, AZ::Constants::Tolerance);
  401. }
  402. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalX_ScaleDoesNotSkewRotation)
  403. {
  404. float expectedScale = 42.564f;
  405. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalUniformScale, expectedScale);
  406. float rx = 1.43f;
  407. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalX, rx);
  408. Vector3 localRotation;
  409. TransformBus::EventResult(localRotation, m_childId, &TransformBus::Events::GetLocalRotation);
  410. EXPECT_TRUE(localRotation.IsClose(Vector3(rx, 0.0f, 0.0f)));
  411. }
  412. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalY_SimpleValue_Set)
  413. {
  414. float ry = 1.43f;
  415. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalY, ry);
  416. Vector3 localRotation;
  417. TransformBus::EventResult(localRotation, m_childId, &TransformBus::Events::GetLocalRotation);
  418. EXPECT_TRUE(localRotation.IsClose(Vector3(0.0f, ry, 0.0f)));
  419. }
  420. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalY_RepeatCallingThisFunctionDoesNotSkewScale)
  421. {
  422. // test numeric stability
  423. float ry = 1.43f;
  424. for (int i = 0; i < 100; ++i)
  425. {
  426. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalY, ry);
  427. }
  428. float localScale = FLT_MAX;
  429. TransformBus::EventResult(localScale, m_childId, &TransformBus::Events::GetLocalUniformScale);
  430. EXPECT_NEAR(localScale, 1.0f, AZ::Constants::Tolerance);
  431. }
  432. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalY_ScaleDoesNotSkewRotation)
  433. {
  434. float expectedScale = 42.564f;
  435. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalUniformScale, expectedScale);
  436. float ry = 1.43f;
  437. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalY, ry);
  438. Vector3 localRotation;
  439. TransformBus::EventResult(localRotation, m_childId, &TransformBus::Events::GetLocalRotation);
  440. EXPECT_TRUE(localRotation.IsClose(Vector3(0.0f, ry, 0.0f)));
  441. }
  442. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalZ_SimpleValues_Set)
  443. {
  444. float rz = 1.43f;
  445. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalZ, rz);
  446. Vector3 localRotation;
  447. TransformBus::EventResult(localRotation, m_childId, &TransformBus::Events::GetLocalRotation);
  448. EXPECT_TRUE(localRotation.IsClose(Vector3(0.0f, 0.0f, rz)));
  449. }
  450. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalZ_RepeatCallingThisFunctionDoesNotSkewScale)
  451. {
  452. // test numeric stability
  453. float rz = 1.43f;
  454. for (int i = 0; i < 100; ++i)
  455. {
  456. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalZ, rz);
  457. }
  458. float localScale = FLT_MAX;
  459. TransformBus::EventResult(localScale, m_childId, &TransformBus::Events::GetLocalUniformScale);
  460. EXPECT_NEAR(localScale, 1.0f, AZ::Constants::Tolerance);
  461. }
  462. TEST_F(TransformComponentTransformMatrixSetGet, RotateAroundLocalZ_ScaleDoesNotSkewRotation)
  463. {
  464. float expectedScale = 42.564f;
  465. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalUniformScale, expectedScale);
  466. float rz = 1.43f;
  467. TransformBus::Event(m_childId, &TransformBus::Events::RotateAroundLocalZ, rz);
  468. Vector3 localRotation;
  469. TransformBus::EventResult(localRotation, m_childId, &TransformBus::Events::GetLocalRotation);
  470. EXPECT_TRUE(localRotation.IsClose(Vector3(0.0f, 0.0f, rz)));
  471. }
  472. TEST_F(TransformComponentTransformMatrixSetGet, SetLocalScale_SimpleValues_Set)
  473. {
  474. float expectedScale = 42.564f;
  475. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalUniformScale, expectedScale);
  476. Transform tm;
  477. TransformBus::EventResult(tm, m_childId, &TransformBus::Events::GetLocalTM);
  478. float scale = tm.GetUniformScale();
  479. EXPECT_NEAR(scale, expectedScale, AZ::Constants::Tolerance);
  480. }
  481. TEST_F(TransformComponentTransformMatrixSetGet, GetLocalScale_SimpleValues_Return)
  482. {
  483. float expectedScale = 43.463f;
  484. Transform scaleTM = Transform::CreateUniformScale(expectedScale);
  485. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, scaleTM);
  486. float scale;
  487. TransformBus::EventResult(scale, m_childId, &TransformBus::Events::GetLocalUniformScale);
  488. EXPECT_NEAR(scale, expectedScale, AZ::Constants::Tolerance);
  489. }
  490. TEST_F(TransformComponentTransformMatrixSetGet, GetWorldScale_ChildHasNoScale_ReturnScaleSameAsParent)
  491. {
  492. float expectedScale = 43.463f;
  493. Transform scaleTM = Transform::CreateUniformScale(expectedScale);
  494. TransformBus::Event(m_parentId, &TransformBus::Events::SetLocalTM, scaleTM);
  495. float scale = FLT_MAX;
  496. TransformBus::EventResult(scale, m_childId, &TransformBus::Events::GetWorldUniformScale);
  497. EXPECT_NEAR(scale, expectedScale, AZ::Constants::Tolerance);
  498. }
  499. TEST_F(TransformComponentTransformMatrixSetGet, GetWorldScale_ChildHasScale_ReturnCompoundScale)
  500. {
  501. float parentScale = 4.463f;
  502. Transform parentScaleTM = Transform::CreateUniformScale(parentScale);
  503. TransformBus::Event(m_parentId, &TransformBus::Events::SetLocalTM, parentScaleTM);
  504. float childScale = 1.64f;
  505. Transform childScaleTM = Transform::CreateUniformScale(childScale);
  506. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTM, childScaleTM);
  507. float scale = FLT_MAX;
  508. TransformBus::EventResult(scale, m_childId, &TransformBus::Events::GetWorldUniformScale);
  509. EXPECT_NEAR(scale, parentScale * childScale, AZ::Constants::Tolerance);
  510. }
  511. class TransformComponentHierarchy
  512. : public TransformComponentApplication
  513. {
  514. protected:
  515. void SetUp() override
  516. {
  517. TransformComponentApplication::SetUp();
  518. m_parentEntity = aznew Entity("Parent");
  519. m_parentId = m_parentEntity->GetId();
  520. m_parentEntity->Init();
  521. m_parentEntity->CreateComponent<TransformComponent>();
  522. m_childEntity = aznew Entity("Child");
  523. m_childId = m_childEntity->GetId();
  524. m_childEntity->Init();
  525. m_childEntity->CreateComponent<TransformComponent>();
  526. m_parentEntity->Activate();
  527. m_childEntity->Activate();
  528. }
  529. void TearDown() override
  530. {
  531. m_childEntity->Deactivate();
  532. m_parentEntity->Deactivate();
  533. delete m_childEntity;
  534. delete m_parentEntity;
  535. TransformComponentApplication::TearDown();
  536. }
  537. Entity* m_parentEntity = nullptr;
  538. EntityId m_parentId = EntityId();
  539. Entity* m_childEntity = nullptr;
  540. EntityId m_childId = EntityId();
  541. };
  542. TEST_F(TransformComponentHierarchy, SetParent_NormalValue_SetKeepWorldTransform)
  543. {
  544. AZ::Vector3 childLocalPos(20.45f, 46.14f, 93.65f);
  545. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTranslation, childLocalPos);
  546. AZ::Vector3 expectedChildWorldPos = childLocalPos;
  547. AZ::Vector3 parentLocalPos(65.24f, 10.65f, 37.87f);
  548. TransformBus::Event(m_parentId, &TransformBus::Events::SetLocalTranslation, parentLocalPos);
  549. TransformBus::Event(m_childId, &TransformBus::Events::SetParent, m_parentId);
  550. AZ::Vector3 childWorldPos;
  551. TransformBus::EventResult(childWorldPos, m_childId, &TransformBus::Events::GetWorldTranslation);
  552. EXPECT_TRUE(childWorldPos == expectedChildWorldPos);
  553. }
  554. TEST_F(TransformComponentHierarchy, SetParentRelative_NormalValue_SetKeepLocalTransform)
  555. {
  556. AZ::Vector3 expectedChildLocalPos(22.45f, 42.14f, 97.45f);
  557. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTranslation, expectedChildLocalPos);
  558. AZ::Vector3 parentLocalPos(15.64f, 12.65f, 29.87f);
  559. TransformBus::Event(m_parentId, &TransformBus::Events::SetLocalTranslation, parentLocalPos);
  560. TransformBus::Event(m_childId, &TransformBus::Events::SetParentRelative, m_parentId);
  561. AZ::Vector3 childLocalPos;
  562. TransformBus::EventResult(childLocalPos, m_childId, &TransformBus::Events::GetLocalTranslation);
  563. EXPECT_TRUE(childLocalPos == expectedChildLocalPos);
  564. }
  565. TEST_F(TransformComponentHierarchy, SetParent_Null_SetKeepWorldTransform)
  566. {
  567. AZ::Vector3 childLocalPos(28.45f, 56.14f, 43.65f);
  568. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTranslation, childLocalPos);
  569. AZ::Vector3 parentLocalPos(85.24f, 12.65f, 33.87f);
  570. TransformBus::Event(m_parentId, &TransformBus::Events::SetLocalTranslation, parentLocalPos);
  571. TransformBus::Event(m_childId, &TransformBus::Events::SetParentRelative, m_parentId);
  572. AZ::Vector3 expectedChildWorldPos;
  573. TransformBus::EventResult(expectedChildWorldPos, m_childId, &TransformBus::Events::GetWorldTranslation);
  574. TransformBus::Event(m_childId, &TransformBus::Events::SetParent, AZ::EntityId());
  575. AZ::Vector3 childWorldPos;
  576. TransformBus::EventResult(childWorldPos, m_childId, &TransformBus::Events::GetWorldTranslation);
  577. EXPECT_TRUE(childWorldPos == expectedChildWorldPos);
  578. // child entity doesn't have a parent now, its world position should equal its local one
  579. AZ::Vector3 actualChildLocalPos;
  580. TransformBus::EventResult(actualChildLocalPos, m_childId, &TransformBus::Events::GetLocalTranslation);
  581. EXPECT_TRUE(actualChildLocalPos == expectedChildWorldPos);
  582. }
  583. TEST_F(TransformComponentHierarchy, SetParentRelative_Null_SetKeepLocalTransform)
  584. {
  585. AZ::Vector3 childLocalPos(28.45f, 49.14f, 94.65f);
  586. TransformBus::Event(m_childId, &TransformBus::Events::SetLocalTranslation, childLocalPos);
  587. AZ::Vector3 parentLocalPos(66.24f, 19.65f, 32.87f);
  588. TransformBus::Event(m_parentId, &TransformBus::Events::SetLocalTranslation, parentLocalPos);
  589. TransformBus::Event(m_childId, &TransformBus::Events::SetParent, m_parentId);
  590. AZ::Vector3 expectedChildLocalPos;
  591. TransformBus::EventResult(expectedChildLocalPos, m_childId, &TransformBus::Events::GetLocalTranslation);
  592. TransformBus::Event(m_childId, &TransformBus::Events::SetParentRelative, AZ::EntityId());
  593. TransformBus::EventResult(childLocalPos, m_childId, &TransformBus::Events::GetLocalTranslation);
  594. EXPECT_TRUE(childLocalPos == expectedChildLocalPos);
  595. // child entity doesn't have a parent now, its world position should equal its local one
  596. AZ::Vector3 actualChildWorldPos;
  597. TransformBus::EventResult(actualChildWorldPos, m_childId, &TransformBus::Events::GetWorldTranslation);
  598. EXPECT_TRUE(actualChildWorldPos == expectedChildLocalPos);
  599. }
  600. // Fixture provides TransformComponent that is static (or not static) on an entity that has been activated.
  601. template<bool IsStatic>
  602. class StaticOrMovableTransformComponent
  603. : public TransformComponentApplication
  604. {
  605. protected:
  606. void SetUp() override
  607. {
  608. TransformComponentApplication::SetUp();
  609. m_entity = aznew Entity(IsStatic ? "Static Entity" : "Movable Entity");
  610. AZ::TransformConfig transformConfig;
  611. transformConfig.m_isStatic = IsStatic;
  612. auto transformComponent = m_entity->CreateComponent<TransformComponent>();
  613. transformComponent->SetConfiguration(transformConfig);
  614. m_transformInterface = transformComponent;
  615. m_entity->Init();
  616. m_entity->Activate();
  617. }
  618. Entity* m_entity;
  619. TransformInterface* m_transformInterface = nullptr;
  620. };
  621. class MovableTransformComponent : public StaticOrMovableTransformComponent<false> {};
  622. class StaticTransformComponent : public StaticOrMovableTransformComponent<true> {};
  623. TEST_F(StaticTransformComponent, SanityCheck)
  624. {
  625. ASSERT_NE(m_entity, nullptr);
  626. ASSERT_NE(m_transformInterface, nullptr);
  627. EXPECT_EQ(m_entity->GetState(), Entity::State::Active);
  628. }
  629. TEST_F(MovableTransformComponent, IsStaticTransform_False)
  630. {
  631. EXPECT_FALSE(m_transformInterface->IsStaticTransform());
  632. }
  633. TEST_F(StaticTransformComponent, IsStaticTransform_True)
  634. {
  635. EXPECT_TRUE(m_transformInterface->IsStaticTransform());
  636. }
  637. TEST_F(MovableTransformComponent, SetWorldTM_MovesEntity)
  638. {
  639. [[maybe_unused]] Transform previousTM = m_transformInterface->GetWorldTM();
  640. Transform nextTM = Transform::CreateTranslation(Vector3(1.f, 2.f, 3.f));
  641. m_transformInterface->SetWorldTM(nextTM);
  642. EXPECT_TRUE(m_transformInterface->GetWorldTM().IsClose(nextTM));
  643. }
  644. TEST_F(StaticTransformComponent, SetWorldTM_DoesNothing)
  645. {
  646. Transform previousTM = m_transformInterface->GetWorldTM();
  647. Transform nextTM = Transform::CreateTranslation(Vector3(1.f, 2.f, 3.f));
  648. m_transformInterface->SetWorldTM(nextTM);
  649. EXPECT_TRUE(m_transformInterface->GetWorldTM().IsClose(previousTM));
  650. }
  651. TEST_F(MovableTransformComponent, SetLocalTM_MovesEntity)
  652. {
  653. [[maybe_unused]] Transform previousTM = m_transformInterface->GetLocalTM();
  654. Transform nextTM = Transform::CreateTranslation(Vector3(1.f, 2.f, 3.f));
  655. m_transformInterface->SetLocalTM(nextTM);
  656. EXPECT_TRUE(m_transformInterface->GetLocalTM().IsClose(nextTM));
  657. }
  658. TEST_F(StaticTransformComponent, SetLocalTM_DoesNothing)
  659. {
  660. Transform previousTM = m_transformInterface->GetLocalTM();
  661. Transform nextTM = Transform::CreateTranslation(Vector3(1.f, 2.f, 3.f));
  662. m_transformInterface->SetLocalTM(nextTM);
  663. EXPECT_TRUE(m_transformInterface->GetLocalTM().IsClose(previousTM));
  664. }
  665. TEST_F(StaticTransformComponent, SetLocalTmOnDeactivatedEntity_MovesEntity)
  666. {
  667. // when static transform component is deactivated, it should allow movement
  668. [[maybe_unused]] Transform previousTM = m_transformInterface->GetLocalTM();
  669. m_entity->Deactivate();
  670. Transform nextTM = Transform::CreateTranslation(Vector3(1.f, 2.f, 3.f));
  671. m_transformInterface->SetLocalTM(nextTM);
  672. EXPECT_TRUE(m_transformInterface->GetLocalTM().IsClose(nextTM));
  673. }
  674. // Sets up a parent/child relationship between two static transform components
  675. class ParentedStaticTransformComponent
  676. : public TransformComponentApplication
  677. {
  678. protected:
  679. void SetUp() override
  680. {
  681. TransformComponentApplication::SetUp();
  682. m_parentEntity = aznew Entity("Parent");
  683. m_parentEntity->Init();
  684. AZ::TransformConfig parentConfig{ AZ::Transform::CreateTranslation(AZ::Vector3(5.f, 5.f, 5.f)) };
  685. parentConfig.m_isStatic = true;
  686. m_parentEntity->CreateComponent<TransformComponent>()->SetConfiguration(parentConfig);
  687. m_childEntity = aznew Entity("Child");
  688. m_childEntity->Init();
  689. AZ::TransformConfig childConfig{ AZ::Transform::CreateTranslation(AZ::Vector3(5.f, 5.f, 5.f)) };
  690. childConfig.m_isStatic = true;
  691. childConfig.m_parentId = m_parentEntity->GetId();
  692. childConfig.m_parentActivationTransformMode = AZ::TransformConfig::ParentActivationTransformMode::MaintainOriginalRelativeTransform;
  693. m_childEntity->CreateComponent<TransformComponent>()->SetConfiguration(childConfig);
  694. }
  695. Entity* m_parentEntity = nullptr;
  696. Entity* m_childEntity = nullptr;
  697. };
  698. // we do expect a static entity to move if its parent is activated after itself
  699. TEST_F(ParentedStaticTransformComponent, ParentActivatesLast_OffsetObeyed)
  700. {
  701. m_childEntity->Activate();
  702. Transform previousWorldTM;
  703. TransformBus::EventResult(previousWorldTM, m_childEntity->GetId(), &TransformBus::Events::GetWorldTM);
  704. m_parentEntity->Activate();
  705. Transform nextWorldTM;
  706. TransformBus::EventResult(nextWorldTM, m_childEntity->GetId(), &TransformBus::Events::GetWorldTM);
  707. EXPECT_FALSE(previousWorldTM.IsClose(nextWorldTM));
  708. }
  709. // Fixture that loads a TransformComponent from a buffer.
  710. // Useful for testing version converters.
  711. class TransformComponentVersionConverter
  712. : public TransformComponentApplication
  713. {
  714. public:
  715. void SetUp() override
  716. {
  717. TransformComponentApplication::SetUp();
  718. m_transformComponent.reset(AZ::Utils::LoadObjectFromBuffer<TransformComponent>(m_objectStreamBuffer, strlen(m_objectStreamBuffer) + 1));
  719. m_transformInterface = m_transformComponent.get();
  720. }
  721. void TearDown() override
  722. {
  723. m_transformComponent.reset();
  724. TransformComponentApplication::TearDown();
  725. }
  726. AZStd::unique_ptr<TransformComponent> m_transformComponent;
  727. TransformInterface* m_transformInterface = nullptr;
  728. const char* m_objectStreamBuffer = nullptr;
  729. };
  730. ///////////////////////////////////////////////////////////////////////////
  731. // TransformConfig
  732. static bool operator==(const TransformConfig& lhs, const TransformConfig& rhs)
  733. {
  734. return lhs.m_parentId == rhs.m_parentId
  735. && lhs.m_parentActivationTransformMode == rhs.m_parentActivationTransformMode
  736. && lhs.m_isStatic == rhs.m_isStatic
  737. && lhs.m_localTransform == rhs.m_localTransform
  738. && lhs.m_worldTransform == rhs.m_worldTransform
  739. ;
  740. }
  741. class TransformConfigTest
  742. : public TransformComponentApplication
  743. {
  744. public:
  745. // creates a transform with a random rotation and a translation with each component uniformly sampled in the
  746. // range [0, 1)
  747. AZ::Transform CreateRandomTransform()
  748. {
  749. AZ::Vector3 translation(m_random.GetRandomFloat(), m_random.GetRandomFloat(), m_random.GetRandomFloat());
  750. return AZ::Transform::CreateFromQuaternionAndTranslation(CreateRandomQuaternion(m_random), translation);
  751. }
  752. TransformConfig GetRandomConfig()
  753. {
  754. TransformConfig config;
  755. config.m_worldTransform = CreateRandomTransform();
  756. config.m_localTransform = CreateRandomTransform();
  757. config.m_parentId = Entity::MakeId();
  758. config.m_parentActivationTransformMode = (TransformConfig::ParentActivationTransformMode)(m_random.GetRandom() % 2);
  759. config.m_netSyncEnabled = (m_random.GetRandom() % 2) == 1;
  760. config.m_interpolatePosition = (m_random.GetRandom() % 2) == 1 ? InterpolationMode::NoInterpolation : InterpolationMode::LinearInterpolation;
  761. config.m_interpolateRotation = (m_random.GetRandom() % 2) == 1 ? InterpolationMode::NoInterpolation : InterpolationMode::LinearInterpolation;
  762. config.m_isStatic = (m_random.GetRandom() % 2) == 1;
  763. return config;
  764. }
  765. void SetUp() override
  766. {
  767. TransformComponentApplication::SetUp();
  768. m_random.SetSeed(static_cast<AZ::u64>(m_app.GetTimeAtCurrentTick().GetMilliseconds()));
  769. }
  770. AZ::SimpleLcgRandom m_random;
  771. };
  772. TEST_F(TransformConfigTest, SetConfiguration_Succeeds)
  773. {
  774. TransformComponent component;
  775. TransformConfig config = GetRandomConfig();
  776. EXPECT_TRUE(component.SetConfiguration(config));
  777. }
  778. TEST_F(TransformConfigTest, GetConfiguration_Succeeds)
  779. {
  780. TransformComponent component;
  781. TransformConfig config;
  782. EXPECT_TRUE(component.GetConfiguration(config));
  783. }
  784. TEST_F(TransformConfigTest, SetThenGet_ConfigsMatches)
  785. {
  786. TransformComponent component;
  787. TransformConfig originalConfig = GetRandomConfig();
  788. EXPECT_TRUE(component.SetConfiguration(originalConfig));
  789. TransformConfig retrievedConfig;
  790. EXPECT_TRUE(component.GetConfiguration(retrievedConfig));
  791. EXPECT_TRUE(originalConfig == retrievedConfig);
  792. }
  793. TEST_F(TransformConfigTest, ConfigDefaultsComparedToComponentDefaults_Same)
  794. {
  795. // A default-constructed TransformConfig should be equivalent
  796. // to a configuration fetched from a default-constructed TransformComponent.
  797. TransformConfig defaultConfig;
  798. TransformConfig retrievedConfig;
  799. TransformComponent component;
  800. EXPECT_TRUE(component.GetConfiguration(retrievedConfig));
  801. EXPECT_TRUE(defaultConfig == retrievedConfig);
  802. }
  803. ///////////////////////////////////////////////////////////////////////////
  804. // AzToolsFramework::Components::TransformComponent
  805. // Fixture base class for AzToolsFramework::Components::TransformComponent tests
  806. class OldEditorTransformComponentTest
  807. : public UnitTest::LeakDetectionFixture
  808. {
  809. protected:
  810. void SetUp() override
  811. {
  812. AZ::ComponentApplication::StartupParameters startupParameters;
  813. startupParameters.m_loadSettingsRegistry = false;
  814. m_app.Start(AZ::ComponentApplication::Descriptor(), startupParameters);
  815. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  816. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  817. // in the unit tests.
  818. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  819. }
  820. void TearDown() override
  821. {
  822. m_app.Stop();
  823. }
  824. AzToolsFramework::ToolsApplication m_app;
  825. };
  826. // Old TransformComponents used to store "Slice Root" entity Id, which could be its own Id.
  827. // The version-converter could end up making an entity into its own transform parent.
  828. // The EditorEntityFixupComponent should fix this up during slice instantiation.
  829. TEST_F(OldEditorTransformComponentTest, OldSliceRoots_ShouldHaveNoParent)
  830. {
  831. const char kSliceData[] =
  832. R"DELIMITER(<ObjectStream version="1">
  833. <Class name="PrefabComponent" field="element" version="1" type="{AFD304E4-1773-47C8-855A-8B622398934F}">
  834. <Class name="AZ::Component" field="BaseClass1" type="{EDFCB2CF-F75D-43BE-B26B-F35821B29247}">
  835. <Class name="AZ::u64" field="Id" value="3561916384376604258" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  836. </Class>
  837. <Class name="AZStd::vector" field="Entities" type="{2BADE35A-6F1B-4698-B2BC-3373D010020C}">
  838. <Class name="AZ::Entity" field="element" version="2" type="{75651658-8663-478D-9090-2432DFCAFA44}">
  839. <Class name="EntityId" field="Id" version="1" type="{6383F1D3-BB27-4E6B-A49A-6409B2059EAA}">
  840. <Class name="AZ::u64" field="id" value="15464031792689993220" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  841. </Class>
  842. <Class name="AZStd::string" field="Name" value="MrRootEntity" type="{EF8FF807-DDEE-4EB0-B678-4CA3A2C490A4}"/>
  843. <Class name="bool" field="IsDependencyReady" value="true" type="{A0CA880C-AFE4-43CB-926C-59AC48496112}"/>
  844. <Class name="AZStd::vector" field="Components" type="{2BADE35A-6F1B-4698-B2BC-3373D010020C}">
  845. <Class name="TransformComponent" field="element" version="5" type="{27F1E1A1-8D9D-4C3B-BD3A-AFB9762449C0}">
  846. <Class name="EditorComponentBase" field="BaseClass1" version="1" type="{D5346BD4-7F20-444E-B370-327ACD03D4A0}">
  847. <Class name="AZ::Component" field="BaseClass1" type="{EDFCB2CF-F75D-43BE-B26B-F35821B29247}">
  848. <Class name="AZ::u64" field="Id" value="3107681419974783222" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  849. </Class>
  850. </Class>
  851. <Class name="EntityId" field="Parent Entity" version="1" type="{6383F1D3-BB27-4E6B-A49A-6409B2059EAA}">
  852. <Class name="AZ::u64" field="id" value="4294967295" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  853. </Class>
  854. <Class name="EditorTransform" field="Transform Data" version="1" type="{B02B7063-D238-4F40-A724-405F7A6D68CB}">
  855. <Class name="Vector3" field="Translate" value="0.0000000 0.0000000 0.0000000" type="{8379EB7D-01FA-4538-B64B-A6543B4BE73D}"/>
  856. <Class name="Vector3" field="Rotate" value="0.0000000 0.0000000 0.0000000" type="{8379EB7D-01FA-4538-B64B-A6543B4BE73D}"/>
  857. <Class name="Vector3" field="Scale" value="1.0000000 1.0000000 1.0000000" type="{8379EB7D-01FA-4538-B64B-A6543B4BE73D}"/>
  858. </Class>
  859. <Class name="Transform" field="Slice Transform" value="1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000" type="{5D9958E9-9F1E-4985-B532-FFFDE75FEDFD}"/>
  860. <Class name="EntityId" field="Slice Root" version="1" type="{6383F1D3-BB27-4E6B-A49A-6409B2059EAA}">
  861. <Class name="AZ::u64" field="id" value="15464031792689993220" type="{D6597933-47CD-4FC8-B911-63F3E2B0993A}"/>
  862. </Class>
  863. <Class name="Transform" field="Cached World Transform" value="1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000" type="{5D9958E9-9F1E-4985-B532-FFFDE75FEDFD}"/>
  864. </Class>
  865. </Class>
  866. </Class>
  867. </Class>
  868. <Class name="AZStd::list" field="Prefabs" type="{B845AD64-B5A0-4CCD-A86B-3477A36779BE}"/>
  869. <Class name="bool" field="IsDynamic" value="true" type="{A0CA880C-AFE4-43CB-926C-59AC48496112}"/>
  870. </Class>
  871. </ObjectStream>)DELIMITER";
  872. AZStd::unique_ptr<SliceComponent> slice{ AZ::Utils::LoadObjectFromBuffer<AZ::SliceComponent>(kSliceData, strlen(kSliceData) + 1) };
  873. EXPECT_NE(slice.get(), nullptr);
  874. if (slice)
  875. {
  876. AZStd::vector<AZ::Entity*> entities;
  877. slice->GetEntities(entities);
  878. EXPECT_FALSE(entities.empty());
  879. if (!entities.empty())
  880. {
  881. auto editorTransformComponent = entities[0]->FindComponent<AzToolsFramework::Components::TransformComponent>();
  882. EXPECT_NE(editorTransformComponent, nullptr);
  883. if (editorTransformComponent)
  884. {
  885. // EditorEntityFixupComponent should have fixed this
  886. EXPECT_EQ(editorTransformComponent->GetParentId(), AZ::EntityId());
  887. }
  888. }
  889. }
  890. }
  891. // Fixture provides a root prefab with Transform component and listens for TransformNotificationBus.
  892. class TransformComponentActivationTest
  893. : public PrefabTestFixture
  894. , public TransformNotificationBus::Handler
  895. {
  896. protected:
  897. void SetUpEditorFixtureImpl() override
  898. {
  899. PrefabTestFixture::SetUpEditorFixtureImpl();
  900. }
  901. void TearDownEditorFixtureImpl() override
  902. {
  903. AZ::TransformNotificationBus::Handler::BusDisconnect();
  904. PrefabTestFixture::TearDownEditorFixtureImpl();
  905. }
  906. void OnTransformChanged(const Transform& /*local*/, const Transform& /*world*/) override
  907. {
  908. m_transformUpdated = true;
  909. }
  910. void MoveEntity(AZ::EntityId entityId)
  911. {
  912. AzToolsFramework::ScopedUndoBatch undoBatch("Move Entity");
  913. TransformBus::Event(entityId, &TransformInterface::SetWorldTranslation, Vector3(1.f, 0.f, 0.f));
  914. }
  915. bool m_transformUpdated = false;
  916. };
  917. TEST_F(TransformComponentActivationTest, TransformChangedEventIsSentWhenEntityIsActivatedViaUndoRedo)
  918. {
  919. AZ::EntityId entityId = CreateEditorEntityUnderRoot("Entity");
  920. MoveEntity(entityId);
  921. ProcessDeferredUpdates();
  922. AZ::TransformNotificationBus::Handler::BusConnect(entityId);
  923. // verify that undoing/redoing move operations fires TransformChanged event
  924. Undo();
  925. EXPECT_TRUE(m_transformUpdated);
  926. m_transformUpdated = false;
  927. Redo();
  928. EXPECT_TRUE(m_transformUpdated);
  929. m_transformUpdated = false;
  930. }
  931. TEST_F(TransformComponentActivationTest, TransformChangedEventIsNotSentWhenEntityIsDeactivatedAndActivated)
  932. {
  933. AZ::EntityId entityId = CreateEditorEntityUnderRoot("Entity");
  934. AZ::TransformNotificationBus::Handler::BusConnect(entityId);
  935. // verify that simply activating/deactivating an entity does not fire TransformChanged event
  936. Entity* entity = nullptr;
  937. ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  938. entity->Deactivate();
  939. entity->Activate();
  940. EXPECT_FALSE(m_transformUpdated);
  941. }
  942. } // namespace UnitTest