LoadSaveSceneTest.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/LoadSaveSceneTest.h>
  5. #include <Math/Perlin.h>
  6. #include <ObjectStream/ObjectStreamOut.h>
  7. #include <ObjectStream/ObjectStreamIn.h>
  8. #include <Physics/Collision/Shape/MeshShape.h>
  9. #include <Physics/Collision/Shape/ScaledShape.h>
  10. #include <Physics/Collision/Shape/BoxShape.h>
  11. #include <Physics/Collision/Shape/SphereShape.h>
  12. #include <Physics/Collision/Shape/CapsuleShape.h>
  13. #include <Physics/Collision/Shape/CylinderShape.h>
  14. #include <Physics/Collision/Shape/TaperedCapsuleShape.h>
  15. #include <Physics/Collision/Shape/StaticCompoundShape.h>
  16. #include <Physics/Collision/Shape/MutableCompoundShape.h>
  17. #include <Physics/Collision/Shape/ConvexHullShape.h>
  18. #include <Physics/Collision/Shape/HeightFieldShape.h>
  19. #include <Physics/Collision/Shape/TriangleShape.h>
  20. #include <Physics/Collision/Shape/RotatedTranslatedShape.h>
  21. #include <Physics/Collision/PhysicsMaterialSimple.h>
  22. #include <Physics/Body/BodyCreationSettings.h>
  23. #include <Layers.h>
  24. #include <Utils/Log.h>
  25. JPH_IMPLEMENT_RTTI_VIRTUAL(LoadSaveSceneTest)
  26. {
  27. JPH_ADD_BASE_CLASS(LoadSaveSceneTest, Test)
  28. }
  29. static const float cMaxHeight = 4.0f;
  30. static MeshShapeSettings *sCreateMesh()
  31. {
  32. const int n = 10;
  33. const float cell_size = 2.0f;
  34. // Create heights
  35. float heights[n + 1][n + 1];
  36. for (int x = 0; x <= n; ++x)
  37. for (int z = 0; z <= n; ++z)
  38. heights[x][z] = cMaxHeight * PerlinNoise3(float(x) / n, 0, float(z) / n, 256, 256, 256);
  39. // Create 'wall' around grid
  40. for (int x = 0; x <= n; ++x)
  41. {
  42. heights[x][0] += 2.0f;
  43. heights[x][n] += 2.0f;
  44. }
  45. for (int y = 1; y < n; ++y)
  46. {
  47. heights[0][y] += 2.0f;
  48. heights[n][y] += 2.0f;
  49. }
  50. // Create regular grid of triangles
  51. uint32 max_material_index = 0;
  52. TriangleList triangles;
  53. for (int x = 0; x < n; ++x)
  54. for (int z = 0; z < n; ++z)
  55. {
  56. float center = n * cell_size / 2;
  57. float x1 = cell_size * x - center;
  58. float z1 = cell_size * z - center;
  59. float x2 = x1 + cell_size;
  60. float z2 = z1 + cell_size;
  61. Float3 v1 = Float3(x1, heights[x][z], z1);
  62. Float3 v2 = Float3(x2, heights[x + 1][z], z1);
  63. Float3 v3 = Float3(x1, heights[x][z + 1], z2);
  64. Float3 v4 = Float3(x2, heights[x + 1][z + 1], z2);
  65. uint32 material_index = uint32((Vec3(v1) + Vec3(v2) + Vec3(v3) + Vec3(v4)).Length() / 4.0f / cell_size);
  66. max_material_index = max(max_material_index, material_index);
  67. triangles.push_back(Triangle(v1, v3, v4, material_index));
  68. triangles.push_back(Triangle(v1, v4, v2, material_index));
  69. }
  70. // Create materials
  71. PhysicsMaterialList materials;
  72. for (uint i = 0; i <= max_material_index; ++i)
  73. materials.push_back(new PhysicsMaterialSimple("Mesh Material " + ConvertToString(i), Color::sGetDistinctColor(i)));
  74. return new MeshShapeSettings(triangles, materials);
  75. }
  76. static HeightFieldShapeSettings *sCreateHeightField()
  77. {
  78. const int n = 32;
  79. const float cell_size = 1.0f;
  80. // Create height samples
  81. float heights[n * n];
  82. for (int y = 0; y < n; ++y)
  83. for (int x = 0; x < n; ++x)
  84. heights[y * n + x] = cMaxHeight * PerlinNoise3(float(x) / n, 0, float(y) / n, 256, 256, 256);
  85. // Make a hole
  86. heights[2 * n + 2] = HeightFieldShapeConstants::cNoCollisionValue;
  87. // Make material indices
  88. uint8 max_material_index = 0;
  89. uint8 material_indices[Square(n - 1)];
  90. for (int y = 0; y < n - 1; ++y)
  91. for (int x = 0; x < n - 1; ++x)
  92. {
  93. uint8 material_index = uint8(round((Vec3(x * cell_size, 0, y * cell_size) - Vec3(n * cell_size / 2, 0, n * cell_size / 2)).Length() / 10.0f));
  94. max_material_index = max(max_material_index, material_index);
  95. material_indices[y * (n - 1) + x] = material_index;
  96. }
  97. // Create materials
  98. PhysicsMaterialList materials;
  99. for (uint8 i = 0; i <= max_material_index; ++i)
  100. materials.push_back(new PhysicsMaterialSimple("HeightField Material " + ConvertToString(uint(i)), Color::sGetDistinctColor(i)));
  101. // Create height field
  102. return new HeightFieldShapeSettings(heights, Vec3(-0.5f * cell_size * n, 0.0f, -0.5f * cell_size * n), Vec3(cell_size, 1.0f, cell_size), n, material_indices, materials);
  103. }
  104. Ref<PhysicsScene> LoadSaveSceneTest::sCreateScene()
  105. {
  106. // Create scene
  107. Ref<PhysicsScene> scene = new PhysicsScene();
  108. // A scaled mesh floor
  109. scene->AddBody(BodyCreationSettings(new ScaledShapeSettings(sCreateMesh(), Vec3(2.5f, 1.0f, 1.5f)), Vec3(0, 0, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  110. // A heightfield floor
  111. scene->AddBody(BodyCreationSettings(sCreateHeightField(), Vec3(50, 0, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  112. // Some simple primitives
  113. scene->AddBody(BodyCreationSettings(new TriangleShapeSettings(Vec3(-2, 0, 0), Vec3(0, 1, 0), Vec3(2, 0, 0), 0.0f, new PhysicsMaterialSimple("Triangle Material", Color::sGetDistinctColor(0))), Vec3(0, cMaxHeight, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Static, Layers::NON_MOVING));
  114. scene->AddBody(BodyCreationSettings(new SphereShapeSettings(0.2f, new PhysicsMaterialSimple("Sphere Material", Color::sGetDistinctColor(1))), Vec3(0, cMaxHeight + 1.0f, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  115. scene->AddBody(BodyCreationSettings(new BoxShapeSettings(Vec3(0.2f, 0.2f, 0.4f), 0.01f, new PhysicsMaterialSimple("Box Material", Color::sGetDistinctColor(2))), Vec3(0, cMaxHeight + 2.0f, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  116. scene->AddBody(BodyCreationSettings(new CapsuleShapeSettings(1.5f, 0.2f, new PhysicsMaterialSimple("Capsule Material", Color::sGetDistinctColor(3))), Vec3(0, cMaxHeight + 3.0f, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  117. scene->AddBody(BodyCreationSettings(new TaperedCapsuleShapeSettings(0.5f, 0.1f, 0.2f, new PhysicsMaterialSimple("Tapered Capsule Material", Color::sGetDistinctColor(4))), Vec3(0, cMaxHeight + 4.0f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  118. scene->AddBody(BodyCreationSettings(new CylinderShapeSettings(0.5f, 0.2f, cDefaultConvexRadius, new PhysicsMaterialSimple("Cylinder Material", Color::sGetDistinctColor(5))), Vec3(0, cMaxHeight + 5.0f, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  119. // Compound with sub compound and rotation
  120. StaticCompoundShapeSettings *sub_compound = new StaticCompoundShapeSettings();
  121. sub_compound->AddShape(Vec3(0, 0.5f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShapeSettings(Vec3(0.5f, 0.1f, 0.2f), cDefaultConvexRadius, new PhysicsMaterialSimple("Compound Box Material", Color::sGetDistinctColor(6))));
  122. sub_compound->AddShape(Vec3(0.5f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CylinderShapeSettings(0.5f, 0.2f, cDefaultConvexRadius, new PhysicsMaterialSimple("Compound Cylinder Material", Color::sGetDistinctColor(7))));
  123. sub_compound->AddShape(Vec3(0, 0, 0.5f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(0.5f, 0.1f, 0.2f, new PhysicsMaterialSimple("Compound Tapered Capsule Material", Color::sGetDistinctColor(8))));
  124. StaticCompoundShapeSettings *compound_shape = new StaticCompoundShapeSettings();
  125. compound_shape->AddShape(Vec3(0, 0, 0), Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), sub_compound);
  126. compound_shape->AddShape(Vec3(0, -0.1f, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), -0.75f * JPH_PI), sub_compound);
  127. scene->AddBody(BodyCreationSettings(compound_shape, Vec3(0, cMaxHeight + 6.0f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  128. // Convex hull shape
  129. vector<Vec3> tetrahedron;
  130. tetrahedron.push_back(Vec3(-0.5f, 0, -0.5f));
  131. tetrahedron.push_back(Vec3(0, 0, 0.5f));
  132. tetrahedron.push_back(Vec3(0.5f, 0, -0.5f));
  133. tetrahedron.push_back(Vec3(0, -0.5f, 0));
  134. Ref<ConvexHullShapeSettings> convex_hull = new ConvexHullShapeSettings(tetrahedron, cDefaultConvexRadius, new PhysicsMaterialSimple("Convex Hull Material", Color::sGetDistinctColor(9)));
  135. scene->AddBody(BodyCreationSettings(convex_hull, Vec3(0, cMaxHeight + 7.0f, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  136. // Rotated convex hull
  137. scene->AddBody(BodyCreationSettings(new RotatedTranslatedShapeSettings(Vec3::sReplicate(0.5f), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), convex_hull), Vec3(0, cMaxHeight + 8.0f, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  138. // Mutable compound
  139. MutableCompoundShapeSettings *mutable_compound = new MutableCompoundShapeSettings();
  140. mutable_compound->AddShape(Vec3(0, 0.5f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShapeSettings(Vec3(0.5f, 0.1f, 0.2f), cDefaultConvexRadius, new PhysicsMaterialSimple("MutableCompound Box Material", Color::sGetDistinctColor(10))));
  141. mutable_compound->AddShape(Vec3(0.5f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CapsuleShapeSettings(0.5f, 0.1f, new PhysicsMaterialSimple("MutableCompound Capsule Material", Color::sGetDistinctColor(11))));
  142. mutable_compound->AddShape(Vec3(0, 0, 0.5f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(0.5f, 0.2f, 0.1f, new PhysicsMaterialSimple("MutableCompound Tapered Capsule Material", Color::sGetDistinctColor(12))));
  143. scene->AddBody(BodyCreationSettings(mutable_compound, Vec3(0, cMaxHeight + 9.0f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  144. return scene;
  145. }
  146. void LoadSaveSceneTest::Initialize()
  147. {
  148. Ref<PhysicsScene> scene = sCreateScene();
  149. stringstream data;
  150. // Write scene
  151. if (!ObjectStreamOut::sWriteObject(data, ObjectStream::EStreamType::Text, *scene))
  152. FatalError("Failed to save scene");
  153. // Clear scene
  154. scene = nullptr;
  155. // Read scene back in
  156. if (!ObjectStreamIn::sReadObject(data, scene))
  157. FatalError("Failed to load scene");
  158. // Instantiate scene
  159. scene->CreateBodies(mPhysicsSystem);
  160. }