DynamicNavigationMesh.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Core/Profiler.h"
  6. #include "../Graphics/DebugRenderer.h"
  7. #include "../IO/Log.h"
  8. #include "../IO/MemoryBuffer.h"
  9. #include "../Navigation/CrowdAgent.h"
  10. #include "../Navigation/DynamicNavigationMesh.h"
  11. #include "../Navigation/NavArea.h"
  12. #include "../Navigation/NavBuildData.h"
  13. #include "../Navigation/NavigationEvents.h"
  14. #include "../Navigation/Obstacle.h"
  15. #include "../Navigation/OffMeshConnection.h"
  16. #include "../Scene/Node.h"
  17. #include "../Scene/Scene.h"
  18. #include "../Scene/SceneEvents.h"
  19. #include <LZ4/lz4.h>
  20. #include <Detour/DetourNavMesh.h>
  21. #include <Detour/DetourNavMeshBuilder.h>
  22. #include <DetourTileCache/DetourTileCache.h>
  23. #include <DetourTileCache/DetourTileCacheBuilder.h>
  24. #include <Recast/Recast.h>
  25. using namespace std;
  26. // DebugNew is deliberately not used because the macro 'free' conflicts with DetourTileCache's LinearAllocator interface
  27. //#include "../DebugNew.h"
  28. static const unsigned TILECACHE_MAXLAYERS = 255;
  29. namespace Urho3D
  30. {
  31. extern const char* NAVIGATION_CATEGORY;
  32. static const int DEFAULT_MAX_OBSTACLES = 1024;
  33. static const int DEFAULT_MAX_LAYERS = 16;
  34. struct DynamicNavigationMesh::TileCacheData
  35. {
  36. unsigned char* data;
  37. int dataSize;
  38. };
  39. struct TileCompressor : public dtTileCacheCompressor
  40. {
  41. int maxCompressedSize(const int bufferSize) override
  42. {
  43. return (int)(bufferSize * 1.05f);
  44. }
  45. dtStatus compress(const unsigned char* buffer, const int bufferSize,
  46. unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize) override
  47. {
  48. *compressedSize = LZ4_compress_default((const char*)buffer, (char*)compressed, bufferSize, LZ4_compressBound(bufferSize));
  49. return DT_SUCCESS;
  50. }
  51. dtStatus decompress(const unsigned char* compressed, const int compressedSize,
  52. unsigned char* buffer, const int maxBufferSize, int* bufferSize) override
  53. {
  54. *bufferSize = LZ4_decompress_safe((const char*)compressed, (char*)buffer, compressedSize, maxBufferSize);
  55. return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
  56. }
  57. };
  58. struct MeshProcess : public dtTileCacheMeshProcess
  59. {
  60. DynamicNavigationMesh* owner_;
  61. Vector<Vector3> offMeshVertices_;
  62. Vector<float> offMeshRadii_;
  63. Vector<unsigned short> offMeshFlags_;
  64. Vector<unsigned char> offMeshAreas_;
  65. Vector<unsigned char> offMeshDir_;
  66. inline explicit MeshProcess(DynamicNavigationMesh* owner) :
  67. owner_(owner)
  68. {
  69. }
  70. void process(struct dtNavMeshCreateParams* params, unsigned char* polyAreas, unsigned short* polyFlags) override
  71. {
  72. // Update poly flags from areas.
  73. // \todo Assignment of flags from areas?
  74. for (int i = 0; i < params->polyCount; ++i)
  75. {
  76. if (polyAreas[i] != RC_NULL_AREA)
  77. polyFlags[i] = RC_WALKABLE_AREA;
  78. }
  79. BoundingBox bounds;
  80. rcVcopy(&bounds.min_.x_, params->bmin);
  81. rcVcopy(&bounds.max_.x_, params->bmin);
  82. // collect off-mesh connections
  83. Vector<OffMeshConnection*> offMeshConnections = owner_->CollectOffMeshConnections(bounds);
  84. if (offMeshConnections.Size() > 0)
  85. {
  86. if (offMeshConnections.Size() != offMeshRadii_.Size())
  87. {
  88. Matrix3x4 inverse = owner_->GetNode()->GetWorldTransform().Inverse();
  89. ClearConnectionData();
  90. for (unsigned i = 0; i < offMeshConnections.Size(); ++i)
  91. {
  92. OffMeshConnection* connection = offMeshConnections[i];
  93. Vector3 start = inverse * connection->GetNode()->GetWorldPosition();
  94. Vector3 end = inverse * connection->GetEndPoint()->GetWorldPosition();
  95. offMeshVertices_.Push(start);
  96. offMeshVertices_.Push(end);
  97. offMeshRadii_.Push(connection->GetRadius());
  98. offMeshFlags_.Push((unsigned short)connection->GetMask());
  99. offMeshAreas_.Push((unsigned char)connection->GetAreaID());
  100. offMeshDir_.Push((unsigned char)(connection->IsBidirectional() ? DT_OFFMESH_CON_BIDIR : 0));
  101. }
  102. }
  103. params->offMeshConCount = offMeshRadii_.Size();
  104. params->offMeshConVerts = &offMeshVertices_[0].x_;
  105. params->offMeshConRad = &offMeshRadii_[0];
  106. params->offMeshConFlags = &offMeshFlags_[0];
  107. params->offMeshConAreas = &offMeshAreas_[0];
  108. params->offMeshConDir = &offMeshDir_[0];
  109. }
  110. }
  111. void ClearConnectionData()
  112. {
  113. offMeshVertices_.Clear();
  114. offMeshRadii_.Clear();
  115. offMeshFlags_.Clear();
  116. offMeshAreas_.Clear();
  117. offMeshDir_.Clear();
  118. }
  119. };
  120. // From the Detour/Recast Sample_TempObstacles.cpp
  121. struct LinearAllocator : public dtTileCacheAlloc
  122. {
  123. unsigned char* buffer;
  124. int capacity;
  125. int top;
  126. int high;
  127. explicit LinearAllocator(const int cap) :
  128. buffer(nullptr), capacity(0), top(0), high(0)
  129. {
  130. resize(cap);
  131. }
  132. ~LinearAllocator() override
  133. {
  134. dtFree(buffer);
  135. }
  136. void resize(const int cap)
  137. {
  138. if (buffer)
  139. dtFree(buffer);
  140. buffer = (unsigned char*)dtAlloc(cap, DT_ALLOC_PERM);
  141. capacity = cap;
  142. }
  143. void reset() override
  144. {
  145. high = Max(high, top);
  146. top = 0;
  147. }
  148. void* alloc(const size_t size) override
  149. {
  150. if (!buffer)
  151. return nullptr;
  152. if (top + size > capacity)
  153. return nullptr;
  154. unsigned char* mem = &buffer[top];
  155. top += size;
  156. return mem;
  157. }
  158. void free(void*) override
  159. {
  160. }
  161. };
  162. DynamicNavigationMesh::DynamicNavigationMesh(Context* context) :
  163. NavigationMesh(context),
  164. maxLayers_(DEFAULT_MAX_LAYERS)
  165. {
  166. // 64 is the largest tile-size that DetourTileCache will tolerate without silently failing
  167. tileSize_ = 64;
  168. partitionType_ = NAVMESH_PARTITION_MONOTONE;
  169. allocator_ = make_unique<LinearAllocator>(32000); //32kb to start
  170. compressor_ = make_unique<TileCompressor>();
  171. meshProcessor_ = make_unique<MeshProcess>(this);
  172. }
  173. DynamicNavigationMesh::~DynamicNavigationMesh()
  174. {
  175. ReleaseNavigationMesh();
  176. }
  177. void DynamicNavigationMesh::RegisterObject(Context* context)
  178. {
  179. context->RegisterFactory<DynamicNavigationMesh>(NAVIGATION_CATEGORY);
  180. URHO3D_COPY_BASE_ATTRIBUTES(NavigationMesh);
  181. URHO3D_ACCESSOR_ATTRIBUTE("Max Obstacles", GetMaxObstacles, SetMaxObstacles, DEFAULT_MAX_OBSTACLES, AM_DEFAULT);
  182. URHO3D_ACCESSOR_ATTRIBUTE("Max Layers", GetMaxLayers, SetMaxLayers, DEFAULT_MAX_LAYERS, AM_DEFAULT);
  183. URHO3D_ACCESSOR_ATTRIBUTE("Draw Obstacles", GetDrawObstacles, SetDrawObstacles, false, AM_DEFAULT);
  184. }
  185. bool DynamicNavigationMesh::Allocate(const BoundingBox& boundingBox, unsigned maxTiles)
  186. {
  187. // Release existing navigation data and zero the bounding box
  188. ReleaseNavigationMesh();
  189. if (!node_)
  190. return false;
  191. if (!node_->GetWorldScale().Equals(Vector3::ONE))
  192. URHO3D_LOGWARNING("Navigation mesh root node has scaling. Agent parameters may not work as intended");
  193. boundingBox_ = boundingBox.Transformed(node_->GetWorldTransform().Inverse());
  194. maxTiles = NextPowerOfTwo(maxTiles);
  195. // Calculate number of tiles
  196. int gridW = 0, gridH = 0;
  197. float tileEdgeLength = (float)tileSize_ * cellSize_;
  198. rcCalcGridSize(&boundingBox_.min_.x_, &boundingBox_.max_.x_, cellSize_, &gridW, &gridH);
  199. numTilesX_ = (gridW + tileSize_ - 1) / tileSize_;
  200. numTilesZ_ = (gridH + tileSize_ - 1) / tileSize_;
  201. // Calculate max number of polygons, 22 bits available to identify both tile & polygon within tile
  202. unsigned tileBits = LogBaseTwo(maxTiles);
  203. unsigned maxPolys = 1u << (22 - tileBits);
  204. dtNavMeshParams params; // NOLINT(hicpp-member-init)
  205. rcVcopy(params.orig, &boundingBox_.min_.x_);
  206. params.tileWidth = tileEdgeLength;
  207. params.tileHeight = tileEdgeLength;
  208. params.maxTiles = maxTiles;
  209. params.maxPolys = maxPolys;
  210. navMesh_ = dtAllocNavMesh();
  211. if (!navMesh_)
  212. {
  213. URHO3D_LOGERROR("Could not allocate navigation mesh");
  214. return false;
  215. }
  216. if (dtStatusFailed(navMesh_->init(&params)))
  217. {
  218. URHO3D_LOGERROR("Could not initialize navigation mesh");
  219. ReleaseNavigationMesh();
  220. return false;
  221. }
  222. dtTileCacheParams tileCacheParams; // NOLINT(hicpp-member-init)
  223. memset(&tileCacheParams, 0, sizeof(tileCacheParams));
  224. rcVcopy(tileCacheParams.orig, &boundingBox_.min_.x_);
  225. tileCacheParams.ch = cellHeight_;
  226. tileCacheParams.cs = cellSize_;
  227. tileCacheParams.width = tileSize_;
  228. tileCacheParams.height = tileSize_;
  229. tileCacheParams.maxSimplificationError = edgeMaxError_;
  230. tileCacheParams.maxTiles = maxTiles * maxLayers_;
  231. tileCacheParams.maxObstacles = maxObstacles_;
  232. // Settings from NavigationMesh
  233. tileCacheParams.walkableClimb = agentMaxClimb_;
  234. tileCacheParams.walkableHeight = agentHeight_;
  235. tileCacheParams.walkableRadius = agentRadius_;
  236. tileCache_ = dtAllocTileCache();
  237. if (!tileCache_)
  238. {
  239. URHO3D_LOGERROR("Could not allocate tile cache");
  240. ReleaseNavigationMesh();
  241. return false;
  242. }
  243. if (dtStatusFailed(tileCache_->init(&tileCacheParams, allocator_.get(), compressor_.get(), meshProcessor_.get())))
  244. {
  245. URHO3D_LOGERROR("Could not initialize tile cache");
  246. ReleaseNavigationMesh();
  247. return false;
  248. }
  249. URHO3D_LOGDEBUG("Allocated empty navigation mesh with max " + String(maxTiles) + " tiles");
  250. // Scan for obstacles to insert into us
  251. Vector<Node*> obstacles;
  252. GetScene()->GetChildrenWithComponent<Obstacle>(obstacles, true);
  253. for (unsigned i = 0; i < obstacles.Size(); ++i)
  254. {
  255. auto* obs = obstacles[i]->GetComponent<Obstacle>();
  256. if (obs && obs->IsEnabledEffective())
  257. AddObstacle(obs);
  258. }
  259. // Send a notification event to concerned parties that we've been fully rebuilt
  260. {
  261. using namespace NavigationMeshRebuilt;
  262. VariantMap& buildEventParams = GetContext()->GetEventDataMap();
  263. buildEventParams[P_NODE] = node_;
  264. buildEventParams[P_MESH] = this;
  265. SendEvent(E_NAVIGATION_MESH_REBUILT, buildEventParams);
  266. }
  267. return true;
  268. }
  269. bool DynamicNavigationMesh::Build()
  270. {
  271. URHO3D_PROFILE(BuildNavigationMesh);
  272. // Release existing navigation data and zero the bounding box
  273. ReleaseNavigationMesh();
  274. if (!node_)
  275. return false;
  276. if (!node_->GetWorldScale().Equals(Vector3::ONE))
  277. URHO3D_LOGWARNING("Navigation mesh root node has scaling. Agent parameters may not work as intended");
  278. Vector<NavigationGeometryInfo> geometryList;
  279. CollectGeometries(geometryList);
  280. if (geometryList.Empty())
  281. return true; // Nothing to do
  282. // Build the combined bounding box
  283. for (unsigned i = 0; i < geometryList.Size(); ++i)
  284. boundingBox_.Merge(geometryList[i].boundingBox_);
  285. // Expand bounding box by padding
  286. boundingBox_.min_ -= padding_;
  287. boundingBox_.max_ += padding_;
  288. {
  289. URHO3D_PROFILE(BuildNavigationMesh);
  290. // Calculate number of tiles
  291. int gridW = 0, gridH = 0;
  292. float tileEdgeLength = (float)tileSize_ * cellSize_;
  293. rcCalcGridSize(&boundingBox_.min_.x_, &boundingBox_.max_.x_, cellSize_, &gridW, &gridH);
  294. numTilesX_ = (gridW + tileSize_ - 1) / tileSize_;
  295. numTilesZ_ = (gridH + tileSize_ - 1) / tileSize_;
  296. // Calculate max. number of tiles and polygons, 22 bits available to identify both tile & polygon within tile
  297. unsigned maxTiles = NextPowerOfTwo((unsigned)(numTilesX_ * numTilesZ_)) * maxLayers_;
  298. unsigned tileBits = LogBaseTwo(maxTiles);
  299. unsigned maxPolys = 1u << (22 - tileBits);
  300. dtNavMeshParams params; // NOLINT(hicpp-member-init)
  301. rcVcopy(params.orig, &boundingBox_.min_.x_);
  302. params.tileWidth = tileEdgeLength;
  303. params.tileHeight = tileEdgeLength;
  304. params.maxTiles = maxTiles;
  305. params.maxPolys = maxPolys;
  306. navMesh_ = dtAllocNavMesh();
  307. if (!navMesh_)
  308. {
  309. URHO3D_LOGERROR("Could not allocate navigation mesh");
  310. return false;
  311. }
  312. if (dtStatusFailed(navMesh_->init(&params)))
  313. {
  314. URHO3D_LOGERROR("Could not initialize navigation mesh");
  315. ReleaseNavigationMesh();
  316. return false;
  317. }
  318. dtTileCacheParams tileCacheParams; // NOLINT(hicpp-member-init)
  319. memset(&tileCacheParams, 0, sizeof(tileCacheParams));
  320. rcVcopy(tileCacheParams.orig, &boundingBox_.min_.x_);
  321. tileCacheParams.ch = cellHeight_;
  322. tileCacheParams.cs = cellSize_;
  323. tileCacheParams.width = tileSize_;
  324. tileCacheParams.height = tileSize_;
  325. tileCacheParams.maxSimplificationError = edgeMaxError_;
  326. tileCacheParams.maxTiles = numTilesX_ * numTilesZ_ * maxLayers_;
  327. tileCacheParams.maxObstacles = maxObstacles_;
  328. // Settings from NavigationMesh
  329. tileCacheParams.walkableClimb = agentMaxClimb_;
  330. tileCacheParams.walkableHeight = agentHeight_;
  331. tileCacheParams.walkableRadius = agentRadius_;
  332. tileCache_ = dtAllocTileCache();
  333. if (!tileCache_)
  334. {
  335. URHO3D_LOGERROR("Could not allocate tile cache");
  336. ReleaseNavigationMesh();
  337. return false;
  338. }
  339. if (dtStatusFailed(tileCache_->init(&tileCacheParams, allocator_.get(), compressor_.get(), meshProcessor_.get())))
  340. {
  341. URHO3D_LOGERROR("Could not initialize tile cache");
  342. ReleaseNavigationMesh();
  343. return false;
  344. }
  345. // Build each tile
  346. unsigned numTiles = 0;
  347. for (int z = 0; z < numTilesZ_; ++z)
  348. {
  349. for (int x = 0; x < numTilesX_; ++x)
  350. {
  351. TileCacheData tiles[TILECACHE_MAXLAYERS];
  352. int layerCt = BuildTile(geometryList, x, z, tiles);
  353. for (int i = 0; i < layerCt; ++i)
  354. {
  355. dtCompressedTileRef tileRef;
  356. int status = tileCache_->addTile(tiles[i].data, tiles[i].dataSize, DT_COMPRESSEDTILE_FREE_DATA, &tileRef);
  357. if (dtStatusFailed((dtStatus)status))
  358. {
  359. dtFree(tiles[i].data);
  360. tiles[i].data = nullptr;
  361. }
  362. }
  363. tileCache_->buildNavMeshTilesAt(x, z, navMesh_);
  364. ++numTiles;
  365. }
  366. }
  367. // For a full build it's necessary to update the nav mesh
  368. // not doing so will cause dependent components to crash, like CrowdManager
  369. tileCache_->update(0, navMesh_);
  370. URHO3D_LOGDEBUG("Built navigation mesh with " + String(numTiles) + " tiles");
  371. // Send a notification event to concerned parties that we've been fully rebuilt
  372. {
  373. using namespace NavigationMeshRebuilt;
  374. VariantMap& buildEventParams = GetContext()->GetEventDataMap();
  375. buildEventParams[P_NODE] = node_;
  376. buildEventParams[P_MESH] = this;
  377. SendEvent(E_NAVIGATION_MESH_REBUILT, buildEventParams);
  378. }
  379. // Scan for obstacles to insert into us
  380. Vector<Node*> obstacles;
  381. GetScene()->GetChildrenWithComponent<Obstacle>(obstacles, true);
  382. for (unsigned i = 0; i < obstacles.Size(); ++i)
  383. {
  384. auto* obs = obstacles[i]->GetComponent<Obstacle>();
  385. if (obs && obs->IsEnabledEffective())
  386. AddObstacle(obs);
  387. }
  388. return true;
  389. }
  390. }
  391. bool DynamicNavigationMesh::Build(const BoundingBox& boundingBox)
  392. {
  393. URHO3D_PROFILE(BuildPartialNavigationMesh);
  394. if (!node_)
  395. return false;
  396. if (!navMesh_)
  397. {
  398. URHO3D_LOGERROR("Navigation mesh must first be built fully before it can be partially rebuilt");
  399. return false;
  400. }
  401. if (!node_->GetWorldScale().Equals(Vector3::ONE))
  402. URHO3D_LOGWARNING("Navigation mesh root node has scaling. Agent parameters may not work as intended");
  403. BoundingBox localSpaceBox = boundingBox.Transformed(node_->GetWorldTransform().Inverse());
  404. float tileEdgeLength = (float)tileSize_ * cellSize_;
  405. Vector<NavigationGeometryInfo> geometryList;
  406. CollectGeometries(geometryList);
  407. int sx = Clamp((int)((localSpaceBox.min_.x_ - boundingBox_.min_.x_) / tileEdgeLength), 0, numTilesX_ - 1);
  408. int sz = Clamp((int)((localSpaceBox.min_.z_ - boundingBox_.min_.z_) / tileEdgeLength), 0, numTilesZ_ - 1);
  409. int ex = Clamp((int)((localSpaceBox.max_.x_ - boundingBox_.min_.x_) / tileEdgeLength), 0, numTilesX_ - 1);
  410. int ez = Clamp((int)((localSpaceBox.max_.z_ - boundingBox_.min_.z_) / tileEdgeLength), 0, numTilesZ_ - 1);
  411. unsigned numTiles = BuildTiles(geometryList, IntVector2(sx, sz), IntVector2(ex, ez));
  412. URHO3D_LOGDEBUG("Rebuilt " + String(numTiles) + " tiles of the navigation mesh");
  413. return true;
  414. }
  415. bool DynamicNavigationMesh::Build(const IntVector2& from, const IntVector2& to)
  416. {
  417. URHO3D_PROFILE(BuildPartialNavigationMesh);
  418. if (!node_)
  419. return false;
  420. if (!navMesh_)
  421. {
  422. URHO3D_LOGERROR("Navigation mesh must first be built fully before it can be partially rebuilt");
  423. return false;
  424. }
  425. if (!node_->GetWorldScale().Equals(Vector3::ONE))
  426. URHO3D_LOGWARNING("Navigation mesh root node has scaling. Agent parameters may not work as intended");
  427. Vector<NavigationGeometryInfo> geometryList;
  428. CollectGeometries(geometryList);
  429. unsigned numTiles = BuildTiles(geometryList, from, to);
  430. URHO3D_LOGDEBUG("Rebuilt " + String(numTiles) + " tiles of the navigation mesh");
  431. return true;
  432. }
  433. Vector<byte> DynamicNavigationMesh::GetTileData(const IntVector2& tile) const
  434. {
  435. VectorBuffer ret;
  436. WriteTiles(ret, tile.x_, tile.y_);
  437. return ret.GetBuffer();
  438. }
  439. bool DynamicNavigationMesh::IsObstacleInTile(Obstacle* obstacle, const IntVector2& tile) const
  440. {
  441. const BoundingBox tileBoundingBox = GetTileBoundingBox(tile);
  442. const Vector3 obstaclePosition = obstacle->GetNode()->GetWorldPosition();
  443. return tileBoundingBox.DistanceToPoint(obstaclePosition) < obstacle->GetRadius();
  444. }
  445. bool DynamicNavigationMesh::AddTile(const Vector<byte>& tileData)
  446. {
  447. MemoryBuffer buffer(tileData);
  448. return ReadTiles(buffer, false);
  449. }
  450. void DynamicNavigationMesh::RemoveTile(const IntVector2& tile)
  451. {
  452. if (!navMesh_)
  453. return;
  454. dtCompressedTileRef existing[TILECACHE_MAXLAYERS];
  455. const int existingCt = tileCache_->getTilesAt(tile.x_, tile.y_, existing, maxLayers_);
  456. for (int i = 0; i < existingCt; ++i)
  457. {
  458. unsigned char* data = nullptr;
  459. if (!dtStatusFailed(tileCache_->removeTile(existing[i], &data, nullptr)) && data != nullptr)
  460. dtFree(data);
  461. }
  462. NavigationMesh::RemoveTile(tile);
  463. }
  464. void DynamicNavigationMesh::RemoveAllTiles()
  465. {
  466. int numTiles = tileCache_->getTileCount();
  467. for (int i = 0; i < numTiles; ++i)
  468. {
  469. const dtCompressedTile* tile = tileCache_->getTile(i);
  470. assert(tile);
  471. if (tile->header)
  472. tileCache_->removeTile(tileCache_->getTileRef(tile), nullptr, nullptr);
  473. }
  474. NavigationMesh::RemoveAllTiles();
  475. }
  476. void DynamicNavigationMesh::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  477. {
  478. if (!debug || !navMesh_ || !node_)
  479. return;
  480. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  481. const dtNavMesh* navMesh = navMesh_;
  482. for (int j = 0; j < navMesh->getMaxTiles(); ++j)
  483. {
  484. const dtMeshTile* tile = navMesh->getTile(j);
  485. assert(tile);
  486. if (!tile->header)
  487. continue;
  488. for (int i = 0; i < tile->header->polyCount; ++i)
  489. {
  490. dtPoly* poly = tile->polys + i;
  491. for (unsigned j = 0; j < poly->vertCount; ++j)
  492. {
  493. debug->AddLine(worldTransform * *reinterpret_cast<const Vector3*>(&tile->verts[poly->verts[j] * 3]),
  494. worldTransform * *reinterpret_cast<const Vector3*>(&tile->verts[poly->verts[(j + 1) % poly->vertCount] * 3]),
  495. Color::YELLOW, depthTest);
  496. }
  497. }
  498. }
  499. Scene* scene = GetScene();
  500. if (scene)
  501. {
  502. // Draw Obstacle components
  503. if (drawObstacles_)
  504. {
  505. Vector<Node*> obstacles;
  506. scene->GetChildrenWithComponent<Obstacle>(obstacles, true);
  507. for (unsigned i = 0; i < obstacles.Size(); ++i)
  508. {
  509. auto* obstacle = obstacles[i]->GetComponent<Obstacle>();
  510. if (obstacle && obstacle->IsEnabledEffective())
  511. obstacle->DrawDebugGeometry(debug, depthTest);
  512. }
  513. }
  514. // Draw OffMeshConnection components
  515. if (drawOffMeshConnections_)
  516. {
  517. Vector<Node*> connections;
  518. scene->GetChildrenWithComponent<OffMeshConnection>(connections, true);
  519. for (unsigned i = 0; i < connections.Size(); ++i)
  520. {
  521. auto* connection = connections[i]->GetComponent<OffMeshConnection>();
  522. if (connection && connection->IsEnabledEffective())
  523. connection->DrawDebugGeometry(debug, depthTest);
  524. }
  525. }
  526. // Draw NavArea components
  527. if (drawNavAreas_)
  528. {
  529. Vector<Node*> areas;
  530. scene->GetChildrenWithComponent<NavArea>(areas, true);
  531. for (unsigned i = 0; i < areas.Size(); ++i)
  532. {
  533. auto* area = areas[i]->GetComponent<NavArea>();
  534. if (area && area->IsEnabledEffective())
  535. area->DrawDebugGeometry(debug, depthTest);
  536. }
  537. }
  538. }
  539. }
  540. void DynamicNavigationMesh::DrawDebugGeometry(bool depthTest)
  541. {
  542. Scene* scene = GetScene();
  543. if (scene)
  544. {
  545. auto* debug = scene->GetComponent<DebugRenderer>();
  546. if (debug)
  547. DrawDebugGeometry(debug, depthTest);
  548. }
  549. }
  550. void DynamicNavigationMesh::SetNavigationDataAttr(const Vector<byte>& value)
  551. {
  552. ReleaseNavigationMesh();
  553. if (value.Empty())
  554. return;
  555. MemoryBuffer buffer(value);
  556. boundingBox_ = buffer.ReadBoundingBox();
  557. numTilesX_ = buffer.ReadI32();
  558. numTilesZ_ = buffer.ReadI32();
  559. dtNavMeshParams params; // NOLINT(hicpp-member-init)
  560. buffer.Read(&params, sizeof(dtNavMeshParams));
  561. navMesh_ = dtAllocNavMesh();
  562. if (!navMesh_)
  563. {
  564. URHO3D_LOGERROR("Could not allocate navigation mesh");
  565. return;
  566. }
  567. if (dtStatusFailed(navMesh_->init(&params)))
  568. {
  569. URHO3D_LOGERROR("Could not initialize navigation mesh");
  570. ReleaseNavigationMesh();
  571. return;
  572. }
  573. dtTileCacheParams tcParams; // NOLINT(hicpp-member-init)
  574. buffer.Read(&tcParams, sizeof(tcParams));
  575. tileCache_ = dtAllocTileCache();
  576. if (!tileCache_)
  577. {
  578. URHO3D_LOGERROR("Could not allocate tile cache");
  579. ReleaseNavigationMesh();
  580. return;
  581. }
  582. if (dtStatusFailed(tileCache_->init(&tcParams, allocator_.get(), compressor_.get(), meshProcessor_.get())))
  583. {
  584. URHO3D_LOGERROR("Could not initialize tile cache");
  585. ReleaseNavigationMesh();
  586. return;
  587. }
  588. ReadTiles(buffer, true);
  589. // \todo Shall we send E_NAVIGATION_MESH_REBUILT here?
  590. }
  591. Vector<byte> DynamicNavigationMesh::GetNavigationDataAttr() const
  592. {
  593. VectorBuffer ret;
  594. if (navMesh_ && tileCache_)
  595. {
  596. ret.WriteBoundingBox(boundingBox_);
  597. ret.WriteI32(numTilesX_);
  598. ret.WriteI32(numTilesZ_);
  599. const dtNavMeshParams* params = navMesh_->getParams();
  600. ret.Write(params, sizeof(dtNavMeshParams));
  601. const dtTileCacheParams* tcParams = tileCache_->getParams();
  602. ret.Write(tcParams, sizeof(dtTileCacheParams));
  603. for (int z = 0; z < numTilesZ_; ++z)
  604. for (int x = 0; x < numTilesX_; ++x)
  605. WriteTiles(ret, x, z);
  606. }
  607. return ret.GetBuffer();
  608. }
  609. void DynamicNavigationMesh::SetMaxLayers(unsigned maxLayers)
  610. {
  611. // Set 3 as a minimum due to the tendency of layers to be constructed inside the hollow space of stacked objects
  612. // That behavior is unlikely to be expected by the end user
  613. maxLayers_ = Max(3U, Min(maxLayers, TILECACHE_MAXLAYERS));
  614. }
  615. void DynamicNavigationMesh::WriteTiles(Serializer& dest, int x, int z) const
  616. {
  617. dtCompressedTileRef tiles[TILECACHE_MAXLAYERS];
  618. const int ct = tileCache_->getTilesAt(x, z, tiles, maxLayers_);
  619. for (int i = 0; i < ct; ++i)
  620. {
  621. const dtCompressedTile* tile = tileCache_->getTileByRef(tiles[i]);
  622. if (!tile || !tile->header || !tile->dataSize)
  623. continue; // Don't write "void-space" tiles
  624. // The header conveniently has the majority of the information required
  625. dest.Write(tile->header, sizeof(dtTileCacheLayerHeader));
  626. dest.WriteI32(tile->dataSize);
  627. dest.Write(tile->data, (unsigned)tile->dataSize);
  628. }
  629. }
  630. bool DynamicNavigationMesh::ReadTiles(Deserializer& source, bool silent)
  631. {
  632. tileQueue_.Clear();
  633. while (!source.IsEof())
  634. {
  635. dtTileCacheLayerHeader header; // NOLINT(hicpp-member-init)
  636. source.Read(&header, sizeof(dtTileCacheLayerHeader));
  637. const int dataSize = source.ReadI32();
  638. auto* data = (unsigned char*)dtAlloc(dataSize, DT_ALLOC_PERM);
  639. if (!data)
  640. {
  641. URHO3D_LOGERROR("Could not allocate data for navigation mesh tile");
  642. return false;
  643. }
  644. source.Read(data, (unsigned)dataSize);
  645. if (dtStatusFailed(tileCache_->addTile(data, dataSize, DT_TILE_FREE_DATA, nullptr)))
  646. {
  647. URHO3D_LOGERROR("Failed to add tile");
  648. dtFree(data);
  649. return false;
  650. }
  651. const IntVector2 tileIdx = IntVector2(header.tx, header.ty);
  652. if (tileQueue_.Empty() || tileQueue_.Back() != tileIdx)
  653. tileQueue_.Push(tileIdx);
  654. }
  655. for (unsigned i = 0; i < tileQueue_.Size(); ++i)
  656. tileCache_->buildNavMeshTilesAt(tileQueue_[i].x_, tileQueue_[i].y_, navMesh_);
  657. tileCache_->update(0, navMesh_);
  658. // Send event
  659. if (!silent)
  660. {
  661. for (unsigned i = 0; i < tileQueue_.Size(); ++i)
  662. {
  663. using namespace NavigationTileAdded;
  664. VariantMap& eventData = GetContext()->GetEventDataMap();
  665. eventData[P_NODE] = GetNode();
  666. eventData[P_MESH] = this;
  667. eventData[P_TILE] = tileQueue_[i];
  668. SendEvent(E_NAVIGATION_TILE_ADDED, eventData);
  669. }
  670. }
  671. return true;
  672. }
  673. int DynamicNavigationMesh::BuildTile(Vector<NavigationGeometryInfo>& geometryList, int x, int z, TileCacheData* tiles)
  674. {
  675. URHO3D_PROFILE(BuildNavigationMeshTile);
  676. tileCache_->removeTile(navMesh_->getTileRefAt(x, z, 0), nullptr, nullptr);
  677. const BoundingBox tileBoundingBox = GetTileBoundingBox(IntVector2(x, z));
  678. DynamicNavBuildData build(allocator_.get());
  679. rcConfig cfg; // NOLINT(hicpp-member-init)
  680. memset(&cfg, 0, sizeof cfg);
  681. cfg.cs = cellSize_;
  682. cfg.ch = cellHeight_;
  683. cfg.walkableSlopeAngle = agentMaxSlope_;
  684. cfg.walkableHeight = (int)ceilf(agentHeight_ / cfg.ch);
  685. cfg.walkableClimb = (int)floorf(agentMaxClimb_ / cfg.ch);
  686. cfg.walkableRadius = (int)ceilf(agentRadius_ / cfg.cs);
  687. cfg.maxEdgeLen = (int)(edgeMaxLength_ / cellSize_);
  688. cfg.maxSimplificationError = edgeMaxError_;
  689. cfg.minRegionArea = (int)sqrtf(regionMinSize_);
  690. cfg.mergeRegionArea = (int)sqrtf(regionMergeSize_);
  691. cfg.maxVertsPerPoly = 6;
  692. cfg.tileSize = tileSize_;
  693. cfg.borderSize = cfg.walkableRadius + 3; // Add padding
  694. cfg.width = cfg.tileSize + cfg.borderSize * 2;
  695. cfg.height = cfg.tileSize + cfg.borderSize * 2;
  696. cfg.detailSampleDist = detailSampleDistance_ < 0.9f ? 0.0f : cellSize_ * detailSampleDistance_;
  697. cfg.detailSampleMaxError = cellHeight_ * detailSampleMaxError_;
  698. rcVcopy(cfg.bmin, &tileBoundingBox.min_.x_);
  699. rcVcopy(cfg.bmax, &tileBoundingBox.max_.x_);
  700. cfg.bmin[0] -= cfg.borderSize * cfg.cs;
  701. cfg.bmin[2] -= cfg.borderSize * cfg.cs;
  702. cfg.bmax[0] += cfg.borderSize * cfg.cs;
  703. cfg.bmax[2] += cfg.borderSize * cfg.cs;
  704. BoundingBox expandedBox(*reinterpret_cast<Vector3*>(cfg.bmin), *reinterpret_cast<Vector3*>(cfg.bmax));
  705. GetTileGeometry(&build, geometryList, expandedBox);
  706. if (build.vertices_.Empty() || build.indices_.Empty())
  707. return 0; // Nothing to do
  708. build.heightField_ = rcAllocHeightfield();
  709. if (!build.heightField_)
  710. {
  711. URHO3D_LOGERROR("Could not allocate heightfield");
  712. return 0;
  713. }
  714. if (!rcCreateHeightfield(build.ctx_, *build.heightField_, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs,
  715. cfg.ch))
  716. {
  717. URHO3D_LOGERROR("Could not create heightfield");
  718. return 0;
  719. }
  720. unsigned numTriangles = build.indices_.Size() / 3;
  721. SharedArrayPtr<unsigned char> triAreas(new unsigned char[numTriangles]);
  722. memset(triAreas.Get(), 0, numTriangles);
  723. rcMarkWalkableTriangles(build.ctx_, cfg.walkableSlopeAngle, &build.vertices_[0].x_, build.vertices_.Size(),
  724. &build.indices_[0], numTriangles, triAreas.Get());
  725. rcRasterizeTriangles(build.ctx_, &build.vertices_[0].x_, build.vertices_.Size(), &build.indices_[0],
  726. triAreas.Get(), numTriangles, *build.heightField_, cfg.walkableClimb);
  727. rcFilterLowHangingWalkableObstacles(build.ctx_, cfg.walkableClimb, *build.heightField_);
  728. rcFilterLedgeSpans(build.ctx_, cfg.walkableHeight, cfg.walkableClimb, *build.heightField_);
  729. rcFilterWalkableLowHeightSpans(build.ctx_, cfg.walkableHeight, *build.heightField_);
  730. build.compactHeightField_ = rcAllocCompactHeightfield();
  731. if (!build.compactHeightField_)
  732. {
  733. URHO3D_LOGERROR("Could not allocate create compact heightfield");
  734. return 0;
  735. }
  736. if (!rcBuildCompactHeightfield(build.ctx_, cfg.walkableHeight, cfg.walkableClimb, *build.heightField_,
  737. *build.compactHeightField_))
  738. {
  739. URHO3D_LOGERROR("Could not build compact heightfield");
  740. return 0;
  741. }
  742. if (!rcErodeWalkableArea(build.ctx_, cfg.walkableRadius, *build.compactHeightField_))
  743. {
  744. URHO3D_LOGERROR("Could not erode compact heightfield");
  745. return 0;
  746. }
  747. // area volumes
  748. for (unsigned i = 0; i < build.navAreas_.Size(); ++i)
  749. rcMarkBoxArea(build.ctx_, &build.navAreas_[i].bounds_.min_.x_, &build.navAreas_[i].bounds_.max_.x_,
  750. build.navAreas_[i].areaID_, *build.compactHeightField_);
  751. if (this->partitionType_ == NAVMESH_PARTITION_WATERSHED)
  752. {
  753. if (!rcBuildDistanceField(build.ctx_, *build.compactHeightField_))
  754. {
  755. URHO3D_LOGERROR("Could not build distance field");
  756. return 0;
  757. }
  758. if (!rcBuildRegions(build.ctx_, *build.compactHeightField_, cfg.borderSize, cfg.minRegionArea,
  759. cfg.mergeRegionArea))
  760. {
  761. URHO3D_LOGERROR("Could not build regions");
  762. return 0;
  763. }
  764. }
  765. else
  766. {
  767. if (!rcBuildRegionsMonotone(build.ctx_, *build.compactHeightField_, cfg.borderSize, cfg.minRegionArea, cfg.mergeRegionArea))
  768. {
  769. URHO3D_LOGERROR("Could not build monotone regions");
  770. return 0;
  771. }
  772. }
  773. build.heightFieldLayers_ = rcAllocHeightfieldLayerSet();
  774. if (!build.heightFieldLayers_)
  775. {
  776. URHO3D_LOGERROR("Could not allocate height field layer set");
  777. return 0;
  778. }
  779. if (!rcBuildHeightfieldLayers(build.ctx_, *build.compactHeightField_, cfg.borderSize, cfg.walkableHeight,
  780. *build.heightFieldLayers_))
  781. {
  782. URHO3D_LOGERROR("Could not build height field layers");
  783. return 0;
  784. }
  785. int retCt = 0;
  786. for (int i = 0; i < build.heightFieldLayers_->nlayers; ++i)
  787. {
  788. dtTileCacheLayerHeader header; // NOLINT(hicpp-member-init)
  789. header.magic = DT_TILECACHE_MAGIC;
  790. header.version = DT_TILECACHE_VERSION;
  791. header.tx = x;
  792. header.ty = z;
  793. header.tlayer = i;
  794. rcHeightfieldLayer* layer = &build.heightFieldLayers_->layers[i];
  795. // Tile info.
  796. rcVcopy(header.bmin, layer->bmin);
  797. rcVcopy(header.bmax, layer->bmax);
  798. header.width = (unsigned char)layer->width;
  799. header.height = (unsigned char)layer->height;
  800. header.minx = (unsigned char)layer->minx;
  801. header.maxx = (unsigned char)layer->maxx;
  802. header.miny = (unsigned char)layer->miny;
  803. header.maxy = (unsigned char)layer->maxy;
  804. header.hmin = (unsigned short)layer->hmin;
  805. header.hmax = (unsigned short)layer->hmax;
  806. if (dtStatusFailed(
  807. dtBuildTileCacheLayer(compressor_.get(), &header, layer->heights, layer->areas, layer->cons,
  808. &(tiles[retCt].data), &tiles[retCt].dataSize)))
  809. {
  810. URHO3D_LOGERROR("Failed to build tile cache layers");
  811. return 0;
  812. }
  813. else
  814. ++retCt;
  815. }
  816. // Send a notification of the rebuild of this tile to anyone interested
  817. {
  818. using namespace NavigationAreaRebuilt;
  819. VariantMap& eventData = GetContext()->GetEventDataMap();
  820. eventData[P_NODE] = GetNode();
  821. eventData[P_MESH] = this;
  822. eventData[P_BOUNDSMIN] = Variant(tileBoundingBox.min_);
  823. eventData[P_BOUNDSMAX] = Variant(tileBoundingBox.max_);
  824. SendEvent(E_NAVIGATION_AREA_REBUILT, eventData);
  825. }
  826. return retCt;
  827. }
  828. unsigned DynamicNavigationMesh::BuildTiles(Vector<NavigationGeometryInfo>& geometryList, const IntVector2& from, const IntVector2& to)
  829. {
  830. unsigned numTiles = 0;
  831. for (int z = from.y_; z <= to.y_; ++z)
  832. {
  833. for (int x = from.x_; x <= to.x_; ++x)
  834. {
  835. dtCompressedTileRef existing[TILECACHE_MAXLAYERS];
  836. const int existingCt = tileCache_->getTilesAt(x, z, existing, maxLayers_);
  837. for (int i = 0; i < existingCt; ++i)
  838. {
  839. unsigned char* data = nullptr;
  840. if (!dtStatusFailed(tileCache_->removeTile(existing[i], &data, nullptr)) && data != nullptr)
  841. dtFree(data);
  842. }
  843. TileCacheData tiles[TILECACHE_MAXLAYERS];
  844. int layerCt = BuildTile(geometryList, x, z, tiles);
  845. for (int i = 0; i < layerCt; ++i)
  846. {
  847. dtCompressedTileRef tileRef;
  848. int status = tileCache_->addTile(tiles[i].data, tiles[i].dataSize, DT_COMPRESSEDTILE_FREE_DATA, &tileRef);
  849. if (dtStatusFailed((dtStatus)status))
  850. {
  851. dtFree(tiles[i].data);
  852. tiles[i].data = nullptr;
  853. }
  854. else
  855. {
  856. tileCache_->buildNavMeshTile(tileRef, navMesh_);
  857. ++numTiles;
  858. }
  859. }
  860. }
  861. }
  862. return numTiles;
  863. }
  864. Vector<OffMeshConnection*> DynamicNavigationMesh::CollectOffMeshConnections(const BoundingBox& bounds)
  865. {
  866. Vector<OffMeshConnection*> connections;
  867. node_->GetComponents<OffMeshConnection>(connections, true);
  868. for (unsigned i = 0; i < connections.Size(); ++i)
  869. {
  870. OffMeshConnection* connection = connections[i];
  871. if (!(connection->IsEnabledEffective() && connection->GetEndPoint()))
  872. {
  873. // discard this connection
  874. connections.Erase(i);
  875. --i;
  876. }
  877. }
  878. return connections;
  879. }
  880. void DynamicNavigationMesh::ReleaseNavigationMesh()
  881. {
  882. NavigationMesh::ReleaseNavigationMesh();
  883. ReleaseTileCache();
  884. }
  885. void DynamicNavigationMesh::ReleaseTileCache()
  886. {
  887. dtFreeTileCache(tileCache_);
  888. tileCache_ = nullptr;
  889. }
  890. void DynamicNavigationMesh::OnSceneSet(Scene* scene)
  891. {
  892. // Subscribe to the scene subsystem update, which will trigger the tile cache to update the nav mesh
  893. if (scene)
  894. SubscribeToEvent(scene, E_SCENESUBSYSTEMUPDATE, URHO3D_HANDLER(DynamicNavigationMesh, HandleSceneSubsystemUpdate));
  895. else
  896. UnsubscribeFromEvent(E_SCENESUBSYSTEMUPDATE);
  897. }
  898. void DynamicNavigationMesh::AddObstacle(Obstacle* obstacle, bool silent)
  899. {
  900. if (tileCache_)
  901. {
  902. float pos[3];
  903. Vector3 obsPos = obstacle->GetNode()->GetWorldPosition();
  904. rcVcopy(pos, &obsPos.x_);
  905. dtObstacleRef refHolder;
  906. // Because dtTileCache doesn't process obstacle requests while updating tiles
  907. // it's necessary update until sufficient request space is available
  908. while (tileCache_->isObstacleQueueFull())
  909. tileCache_->update(1, navMesh_);
  910. if (dtStatusFailed(tileCache_->addObstacle(pos, obstacle->GetRadius(), obstacle->GetHeight(), &refHolder)))
  911. {
  912. URHO3D_LOGERROR("Failed to add obstacle");
  913. return;
  914. }
  915. obstacle->obstacleId_ = refHolder;
  916. assert(refHolder > 0);
  917. if (!silent)
  918. {
  919. using namespace NavigationObstacleAdded;
  920. VariantMap& eventData = GetContext()->GetEventDataMap();
  921. eventData[P_NODE] = obstacle->GetNode();
  922. eventData[P_OBSTACLE] = obstacle;
  923. eventData[P_POSITION] = obstacle->GetNode()->GetWorldPosition();
  924. eventData[P_RADIUS] = obstacle->GetRadius();
  925. eventData[P_HEIGHT] = obstacle->GetHeight();
  926. SendEvent(E_NAVIGATION_OBSTACLE_ADDED, eventData);
  927. }
  928. }
  929. }
  930. void DynamicNavigationMesh::ObstacleChanged(Obstacle* obstacle)
  931. {
  932. if (tileCache_)
  933. {
  934. RemoveObstacle(obstacle, true);
  935. AddObstacle(obstacle, true);
  936. }
  937. }
  938. void DynamicNavigationMesh::RemoveObstacle(Obstacle* obstacle, bool silent)
  939. {
  940. if (tileCache_ && obstacle->obstacleId_ > 0)
  941. {
  942. // Because dtTileCache doesn't process obstacle requests while updating tiles
  943. // it's necessary update until sufficient request space is available
  944. while (tileCache_->isObstacleQueueFull())
  945. tileCache_->update(1, navMesh_);
  946. if (dtStatusFailed(tileCache_->removeObstacle(obstacle->obstacleId_)))
  947. {
  948. URHO3D_LOGERROR("Failed to remove obstacle");
  949. return;
  950. }
  951. obstacle->obstacleId_ = 0;
  952. // Require a node in order to send an event
  953. if (!silent && obstacle->GetNode())
  954. {
  955. using namespace NavigationObstacleRemoved;
  956. VariantMap& eventData = GetContext()->GetEventDataMap();
  957. eventData[P_NODE] = obstacle->GetNode();
  958. eventData[P_OBSTACLE] = obstacle;
  959. eventData[P_POSITION] = obstacle->GetNode()->GetWorldPosition();
  960. eventData[P_RADIUS] = obstacle->GetRadius();
  961. eventData[P_HEIGHT] = obstacle->GetHeight();
  962. SendEvent(E_NAVIGATION_OBSTACLE_REMOVED, eventData);
  963. }
  964. }
  965. }
  966. void DynamicNavigationMesh::HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData)
  967. {
  968. using namespace SceneSubsystemUpdate;
  969. if (tileCache_ && navMesh_ && IsEnabledEffective())
  970. tileCache_->update(eventData[P_TIMESTEP].GetFloat(), navMesh_);
  971. }
  972. }