DynamicNavigationMesh.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Navigation/NavigationMesh.h"
  5. #include <memory>
  6. class dtTileCache;
  7. struct dtTileCacheAlloc;
  8. struct dtTileCacheCompressor;
  9. struct dtTileCacheMeshProcess;
  10. struct dtTileCacheLayer;
  11. struct dtTileCacheContourSet;
  12. struct dtTileCachePolyMesh;
  13. namespace Urho3D
  14. {
  15. class OffMeshConnection;
  16. class Obstacle;
  17. class URHO3D_API DynamicNavigationMesh : public NavigationMesh
  18. {
  19. URHO3D_OBJECT(DynamicNavigationMesh, NavigationMesh);
  20. friend class Obstacle;
  21. friend struct MeshProcess;
  22. public:
  23. /// Constructor.
  24. explicit DynamicNavigationMesh(Context* context);
  25. /// Destructor.
  26. ~DynamicNavigationMesh() override;
  27. /// Register with engine context.
  28. /// @nobind
  29. static void RegisterObject(Context* context);
  30. /// Allocate the navigation mesh without building any tiles. Bounding box is not padded. Return true if successful.
  31. bool Allocate(const BoundingBox& boundingBox, unsigned maxTiles) override;
  32. /// Build/rebuild the entire navigation mesh.
  33. bool Build() override;
  34. /// Build/rebuild a portion of the navigation mesh.
  35. bool Build(const BoundingBox& boundingBox) override;
  36. /// Rebuild part of the navigation mesh in the rectangular area. Return true if successful.
  37. bool Build(const IntVector2& from, const IntVector2& to) override;
  38. /// Return tile data.
  39. Vector<byte> GetTileData(const IntVector2& tile) const override;
  40. /// Return whether the Obstacle is touching the given tile.
  41. bool IsObstacleInTile(Obstacle* obstacle, const IntVector2& tile) const;
  42. /// Add tile to navigation mesh.
  43. bool AddTile(const Vector<byte>& tileData) override;
  44. /// Remove tile from navigation mesh.
  45. void RemoveTile(const IntVector2& tile) override;
  46. /// Remove all tiles from navigation mesh.
  47. void RemoveAllTiles() override;
  48. /// Visualize the component as debug geometry.
  49. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  50. /// Add debug geometry to the debug renderer.
  51. void DrawDebugGeometry(bool depthTest);
  52. /// Set navigation data attribute.
  53. void SetNavigationDataAttr(const Vector<byte>& value) override;
  54. /// Return navigation data attribute.
  55. Vector<byte> GetNavigationDataAttr() const override;
  56. /// Set the maximum number of obstacles allowed.
  57. /// @property
  58. void SetMaxObstacles(unsigned maxObstacles) { maxObstacles_ = maxObstacles; }
  59. /// Set the maximum number of layers that navigation construction can create.
  60. /// @property
  61. void SetMaxLayers(unsigned maxLayers);
  62. /// Return the maximum number of obstacles allowed.
  63. /// @property
  64. unsigned GetMaxObstacles() const { return maxObstacles_; }
  65. /// Return the maximum number of layers permitted to build.
  66. /// @property
  67. unsigned GetMaxLayers() const { return maxLayers_; }
  68. /// Draw debug geometry for Obstacles.
  69. /// @property
  70. void SetDrawObstacles(bool enable) { drawObstacles_ = enable; }
  71. /// Return whether to draw Obstacles.
  72. /// @property
  73. bool GetDrawObstacles() const { return drawObstacles_; }
  74. protected:
  75. struct TileCacheData;
  76. /// Subscribe to events when assigned to a scene.
  77. void OnSceneSet(Scene* scene) override;
  78. /// Trigger the tile cache to make updates to the nav mesh if necessary.
  79. void HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData);
  80. /// Used by Obstacle class to add itself to the tile cache, if 'silent' an event will not be raised.
  81. void AddObstacle(Obstacle* obstacle, bool silent = false);
  82. /// Used by Obstacle class to update itself.
  83. void ObstacleChanged(Obstacle* obstacle);
  84. /// Used by Obstacle class to remove itself from the tile cache, if 'silent' an event will not be raised.
  85. void RemoveObstacle(Obstacle*, bool silent = false);
  86. /// Build one tile of the navigation mesh. Return true if successful.
  87. int BuildTile(Vector<NavigationGeometryInfo>& geometryList, int x, int z, TileCacheData* tiles);
  88. /// Build tiles in the rectangular area. Return number of built tiles.
  89. unsigned BuildTiles(Vector<NavigationGeometryInfo>& geometryList, const IntVector2& from, const IntVector2& to);
  90. /// Off-mesh connections to be rebuilt in the mesh processor.
  91. Vector<OffMeshConnection*> CollectOffMeshConnections(const BoundingBox& bounds);
  92. /// Release the navigation mesh, query, and tile cache.
  93. void ReleaseNavigationMesh() override;
  94. private:
  95. /// Write tiles data.
  96. void WriteTiles(Serializer& dest, int x, int z) const;
  97. /// Read tiles data to the navigation mesh.
  98. bool ReadTiles(Deserializer& source, bool silent);
  99. /// Free the tile cache.
  100. void ReleaseTileCache();
  101. /// Detour tile cache instance that works with the nav mesh.
  102. dtTileCache* tileCache_{};
  103. /// Used by dtTileCache to allocate blocks of memory.
  104. std::unique_ptr<dtTileCacheAlloc> allocator_;
  105. /// Used by dtTileCache to compress the original tiles to use when reconstructing for changes.
  106. std::unique_ptr<dtTileCacheCompressor> compressor_;
  107. /// Mesh processor used by Detour, in this case a 'pass-through' processor.
  108. std::unique_ptr<dtTileCacheMeshProcess> meshProcessor_;
  109. /// Maximum number of obstacle objects allowed.
  110. unsigned maxObstacles_{1024};
  111. /// Maximum number of layers that are allowed to be constructed.
  112. unsigned maxLayers_{};
  113. /// Debug draw Obstacles.
  114. bool drawObstacles_{};
  115. /// Queue of tiles to be built.
  116. Vector<IntVector2> tileQueue_;
  117. };
  118. }