SimulatedObjectSerializeTests.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "ActorFixture.h"
  9. #include <MCore/Source/CommandGroup.h>
  10. #include <EMotionFX/Source/Actor.h>
  11. #include <EMotionFX/Source/SimulatedObjectSetup.h>
  12. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  13. #include <AzFramework/StringFunc/StringFunc.h>
  14. #include <AzCore/std/string/string.h>
  15. namespace EMotionFX
  16. {
  17. using SimulatedObjectSerializeTests = ActorFixture;
  18. TEST_F(SimulatedObjectSerializeTests, SerializeTest)
  19. {
  20. SimulatedObjectSetup* setup = GetActor()->GetSimulatedObjectSetup().get();
  21. // Build some setup.
  22. SimulatedObject* object = setup->AddSimulatedObject();
  23. object->SetName("Left Arm");
  24. object->SetDampingFactor(2.0f);
  25. object->SetGravityFactor(3.0f);
  26. object->SetStiffnessFactor(4.0f);
  27. const AZStd::vector<AZStd::string> jointNames = { "l_upArm", "l_loArm", "l_hand" };
  28. Skeleton* skeleton = GetActor()->GetSkeleton();
  29. for (const AZStd::string& name : jointNames)
  30. {
  31. size_t skeletonJointIndex;
  32. const Node* skeletonJoint = skeleton->FindNodeAndIndexByName(name, skeletonJointIndex);
  33. ASSERT_NE(skeletonJoint, nullptr);
  34. ASSERT_NE(skeletonJointIndex, InvalidIndex);
  35. SimulatedJoint* simulatedJoint = object->AddSimulatedJoint(skeletonJointIndex);
  36. simulatedJoint->SetDamping(0.1f);
  37. simulatedJoint->SetMass(2.0f);
  38. simulatedJoint->SetStiffness(200.0f);
  39. simulatedJoint->SetGravityFactor(1.5f);
  40. simulatedJoint->SetCollisionRadius(3.0f);
  41. simulatedJoint->SetGeometricAutoExclusion(false);
  42. simulatedJoint->SetColliderExclusionTags({"TagA", "TagB"});
  43. simulatedJoint->SetAutoExcludeMode(SimulatedJoint::AutoExcludeMode::Self);
  44. }
  45. object->GetSimulatedJoint(0)->SetPinned(true);
  46. // Serialize it and deserialize it.
  47. const AZStd::string serialized = SerializeSimulatedObjectSetup(GetActor());
  48. AZStd::unique_ptr<SimulatedObjectSetup> loadedSetup(DeserializeSimulatedObjectSetup(serialized));
  49. // Verify some of the contents of the deserialized version.
  50. ASSERT_EQ(loadedSetup->GetNumSimulatedObjects(), 1);
  51. SimulatedObject* loadedObject = loadedSetup->GetSimulatedObject(0);
  52. ASSERT_STREQ(loadedObject->GetName().c_str(), "Left Arm");
  53. ASSERT_EQ(loadedObject->GetNumSimulatedJoints(), jointNames.size());
  54. ASSERT_FLOAT_EQ(loadedObject->GetDampingFactor(), 2.0f);
  55. ASSERT_FLOAT_EQ(loadedObject->GetGravityFactor(), 3.0f);
  56. ASSERT_FLOAT_EQ(loadedObject->GetStiffnessFactor(), 4.0f);
  57. for (size_t i = 0; i < jointNames.size(); ++i)
  58. {
  59. const SimulatedJoint* loadedJoint = loadedObject->GetSimulatedJoint(i);
  60. ASSERT_STREQ(skeleton->GetNode(loadedJoint->GetSkeletonJointIndex())->GetName(), jointNames[i].c_str());
  61. ASSERT_FLOAT_EQ(loadedJoint->GetDamping(), 0.1f);
  62. ASSERT_FLOAT_EQ(loadedJoint->GetMass(), 2.0f);
  63. ASSERT_FLOAT_EQ(loadedJoint->GetStiffness(), 200.0f);
  64. ASSERT_FLOAT_EQ(loadedJoint->GetGravityFactor(), 1.5f);
  65. ASSERT_FLOAT_EQ(loadedJoint->GetCollisionRadius(), 3.0f);
  66. ASSERT_EQ(loadedJoint->IsPinned(), (i==0) ? true : false);
  67. ASSERT_EQ(loadedJoint->IsGeometricAutoExclusion(), false);
  68. ASSERT_EQ(loadedJoint->GetColliderExclusionTags().size(), 2);
  69. ASSERT_STREQ(loadedJoint->GetColliderExclusionTags()[0].c_str(), "TagA");
  70. ASSERT_STREQ(loadedJoint->GetColliderExclusionTags()[1].c_str(), "TagB");
  71. ASSERT_EQ(loadedJoint->GetAutoExcludeMode(), SimulatedJoint::AutoExcludeMode::Self);
  72. }
  73. }
  74. } // namespace EMotionFX