LargeMeshScene.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. // Jolt includes
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  9. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. // Local includes
  13. #include "PerformanceTestScene.h"
  14. #include "Layers.h"
  15. // A scene that first finds the largest possible mesh and then simulates some objects on it
  16. class LargeMeshScene : public PerformanceTestScene
  17. {
  18. public:
  19. virtual const char * GetName() const override
  20. {
  21. return "LargeMeshScene";
  22. }
  23. virtual bool Load(const String &inAssetPath) override
  24. {
  25. // Create mesh shape creation settings
  26. mMeshCreationSettings.mMotionType = EMotionType::Static;
  27. mMeshCreationSettings.mObjectLayer = Layers::NON_MOVING;
  28. mMeshCreationSettings.mPosition = RVec3::sZero();
  29. mMeshCreationSettings.mFriction = 0.5f;
  30. mMeshCreationSettings.mRestitution = 0.6f;
  31. Trace("Finding the largest possible mesh, this will take some time!");
  32. Trace("N, Num Triangles, Mesh Size, Size / Triangle, SubShapeID Bits, Time");
  33. for (int i = 1; ; ++i)
  34. {
  35. const int n = 500 * i;
  36. const float cell_size = 1.0f;
  37. const float max_height = 50.0f;
  38. // Create heights
  39. MeshShapeSettings settings;
  40. float center = n * cell_size / 2;
  41. settings.mTriangleVertices.reserve((n + 1)*(n + 1));
  42. for (int x = 0; x <= n; ++x)
  43. for (int z = 0; z <= n; ++z)
  44. settings.mTriangleVertices.push_back(Float3(cell_size * x - center, max_height * Sin(float(x) * 50.0f / n) * Cos(float(z) * 50.0f / n), cell_size * z - center));
  45. // Create regular grid of triangles
  46. settings.mIndexedTriangles.reserve(2 * n * n);
  47. for (int x = 0; x < n; ++x)
  48. for (int z = 0; z < n; ++z)
  49. {
  50. settings.mIndexedTriangles.push_back(IndexedTriangle(x + z * (n + 1), x + 1 + z * (n + 1), x + (z + 1)*(n + 1)));
  51. settings.mIndexedTriangles.push_back(IndexedTriangle(x + 1 + z * (n + 1), x + 1 + (z + 1)*(n + 1), x + (z + 1)*(n + 1)));
  52. }
  53. // Start measuring
  54. chrono::high_resolution_clock::time_point clock_start = chrono::high_resolution_clock::now();
  55. // Create the mesh shape
  56. Shape::ShapeResult result = settings.Create();
  57. // Stop measuring
  58. chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now();
  59. chrono::nanoseconds duration = chrono::duration_cast<chrono::nanoseconds>(clock_end - clock_start);
  60. if (result.HasError())
  61. {
  62. // Break when we get an error
  63. Trace("Mesh creation failed with error: %s", result.GetError().c_str());
  64. break;
  65. }
  66. else
  67. {
  68. // Trace stats
  69. RefConst<Shape> shape = result.Get();
  70. Shape::Stats stats = shape->GetStats();
  71. Trace("%u, %u, %llu, %.1f, %d, %.3f", n, stats.mNumTriangles, (uint64)stats.mSizeBytes, double(stats.mSizeBytes) / double(stats.mNumTriangles), shape->GetSubShapeIDBitsRecursive(), 1.0e-9 * double(duration.count()));
  72. // Set this shape as the best shape so far
  73. mMeshCreationSettings.SetShape(shape);
  74. }
  75. }
  76. return true;
  77. }
  78. virtual void StartTest(PhysicsSystem &inPhysicsSystem, EMotionQuality inMotionQuality) override
  79. {
  80. // Create background
  81. BodyInterface &bi = inPhysicsSystem.GetBodyInterface();
  82. bi.CreateAndAddBody(mMeshCreationSettings, EActivation::DontActivate);
  83. // Construct bodies
  84. BodyCreationSettings creation_settings;
  85. creation_settings.mMotionType = EMotionType::Dynamic;
  86. creation_settings.mMotionQuality = inMotionQuality;
  87. creation_settings.mObjectLayer = Layers::MOVING;
  88. creation_settings.mFriction = 0.5f;
  89. creation_settings.mRestitution = 0.6f;
  90. creation_settings.SetShape(new BoxShape(Vec3(0.5f, 0.75f, 1.0f)));
  91. for (int x = -10; x <= 10; ++x)
  92. for (int y = 0; y < 10; ++y)
  93. for (int z = -10; z <= 10; ++z)
  94. {
  95. creation_settings.mPosition = RVec3(7.5_r * x, 55.0_r + 2.0_r * y, 7.5_r * z);
  96. bi.CreateAndAddBody(creation_settings, EActivation::Activate);
  97. }
  98. }
  99. private:
  100. BodyCreationSettings mMeshCreationSettings;
  101. };