DynamicNavigationMesh.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Navigation/NavigationMesh.h"
  24. class dtTileCache;
  25. struct dtTileCacheAlloc;
  26. struct dtTileCacheCompressor;
  27. struct dtTileCacheMeshProcess;
  28. struct dtTileCacheLayer;
  29. struct dtTileCacheContourSet;
  30. struct dtTileCachePolyMesh;
  31. namespace Urho3D
  32. {
  33. class OffMeshConnection;
  34. class Obstacle;
  35. class URHO3D_API DynamicNavigationMesh : public NavigationMesh
  36. {
  37. URHO3D_OBJECT(DynamicNavigationMesh, NavigationMesh)
  38. friend class Obstacle;
  39. friend struct MeshProcess;
  40. public:
  41. /// Constructor.
  42. DynamicNavigationMesh(Context*);
  43. /// Destructor.
  44. virtual ~DynamicNavigationMesh();
  45. /// Register with engine context.
  46. static void RegisterObject(Context*);
  47. /// Build/rebuild the entire navigation mesh.
  48. virtual bool Build();
  49. /// Build/rebuild a portion of the navigation mesh.
  50. virtual bool Build(const BoundingBox& boundingBox);
  51. /// Visualize the component as debug geometry.
  52. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  53. /// Add debug geometry to the debug renderer.
  54. void DrawDebugGeometry(bool depthTest);
  55. /// Set navigation data attribute.
  56. virtual void SetNavigationDataAttr(const PODVector<unsigned char>& value);
  57. /// Return navigation data attribute.
  58. virtual PODVector<unsigned char> GetNavigationDataAttr() const;
  59. /// Set the maximum number of obstacles allowed.
  60. void SetMaxObstacles(unsigned maxObstacles) { maxObstacles_ = maxObstacles; }
  61. /// Set the maximum number of layers that navigation construction can create.
  62. void SetMaxLayers(unsigned maxLayers);
  63. /// Return the maximum number of obstacles allowed.
  64. unsigned GetMaxObstacles() const { return maxObstacles_; }
  65. /// Return the maximum number of layers permitted to build.
  66. unsigned GetMaxLayers() const { return maxLayers_; }
  67. /// Draw debug geometry for Obstacles.
  68. void SetDrawObstacles(bool enable) { drawObstacles_ = enable; }
  69. /// Return whether to draw Obstacles.
  70. bool GetDrawObstacles() const { return drawObstacles_; }
  71. protected:
  72. struct TileCacheData;
  73. /// Subscribe to events when assigned to a scene.
  74. virtual void OnSceneSet(Scene* scene);
  75. /// Trigger the tile cache to make updates to the nav mesh if necessary.
  76. void HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData);
  77. /// Used by Obstacle class to add itself to the tile cache, if 'silent' an event will not be raised.
  78. void AddObstacle(Obstacle* obstacle, bool silent = false);
  79. /// Used by Obstacle class to update itself.
  80. void ObstacleChanged(Obstacle* obstacle);
  81. /// Used by Obstacle class to remove itself from the tile cache, if 'silent' an event will not be raised.
  82. void RemoveObstacle(Obstacle*, bool silent = false);
  83. /// Build one tile of the navigation mesh. Return true if successful.
  84. int BuildTile(Vector<NavigationGeometryInfo>& geometryList, int x, int z, TileCacheData*);
  85. /// Off-mesh connections to be rebuilt in the mesh processor.
  86. PODVector<OffMeshConnection*> CollectOffMeshConnections(const BoundingBox& bounds);
  87. /// Release the navigation mesh, query, and tile cache.
  88. virtual void ReleaseNavigationMesh();
  89. private:
  90. /// Free the tile cache.
  91. void ReleaseTileCache();
  92. /// Detour tile cache instance that works with the nav mesh.
  93. dtTileCache* tileCache_;
  94. /// Used by dtTileCache to allocate blocks of memory.
  95. UniquePtr<dtTileCacheAlloc> allocator_;
  96. /// Used by dtTileCache to compress the original tiles to use when reconstructing for changes.
  97. UniquePtr<dtTileCacheCompressor> compressor_;
  98. /// Mesh processor used by Detour, in this case a 'pass-through' processor.
  99. UniquePtr<dtTileCacheMeshProcess> meshProcessor_;
  100. /// Maximum number of obstacle objects allowed.
  101. unsigned maxObstacles_;
  102. /// Maximum number of layers that are allowed to be constructed.
  103. unsigned maxLayers_;
  104. /// Debug draw Obstacles.
  105. bool drawObstacles_;
  106. };
  107. }