NavigationMesh.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. //
  2. // Copyright (c) 2008-2013 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. #include "Precompiled.h"
  23. #include "CollisionShape.h"
  24. #include "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Drawable.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "MemoryBuffer.h"
  30. #include "Model.h"
  31. #include "Navigable.h"
  32. #include "NavigationMesh.h"
  33. #include "Profiler.h"
  34. #include "Scene.h"
  35. #include "StaticModel.h"
  36. #include "TerrainPatch.h"
  37. #include "VectorBuffer.h"
  38. #include <cfloat>
  39. #include <DetourNavMesh.h>
  40. #include <DetourNavMeshBuilder.h>
  41. #include <DetourNavMeshQuery.h>
  42. #include <Recast.h>
  43. #include "DebugNew.h"
  44. namespace Urho3D
  45. {
  46. static const int DEFAULT_TILE_SIZE = 128;
  47. static const float DEFAULT_CELL_SIZE = 0.3f;
  48. static const float DEFAULT_CELL_HEIGHT = 0.2f;
  49. static const float DEFAULT_AGENT_HEIGHT = 2.0f;
  50. static const float DEFAULT_AGENT_RADIUS = 0.6f;
  51. static const float DEFAULT_AGENT_MAX_CLIMB = 0.9f;
  52. static const float DEFAULT_AGENT_MAX_SLOPE = 45.0f;
  53. static const float DEFAULT_REGION_MIN_SIZE = 8.0f;
  54. static const float DEFAULT_REGION_MERGE_SIZE = 20.0f;
  55. static const float DEFAULT_EDGE_MAX_LENGTH = 12.0f;
  56. static const float DEFAULT_EDGE_MAX_ERROR = 1.3f;
  57. static const float DEFAULT_DETAIL_SAMPLE_DISTANCE = 6.0f;
  58. static const float DEFAULT_DETAIL_SAMPLE_MAX_ERROR = 1.0f;
  59. static const int MAX_POLYS = 2048;
  60. /// Temporary data for building one tile of the navigation mesh.
  61. struct NavigationBuildData
  62. {
  63. /// Construct.
  64. NavigationBuildData() :
  65. ctx_(new rcContext(false)),
  66. heightField_(0),
  67. compactHeightField_(0),
  68. contourSet_(0),
  69. polyMesh_(0),
  70. polyMeshDetail_(0)
  71. {
  72. }
  73. /// Destruct.
  74. ~NavigationBuildData()
  75. {
  76. delete(ctx_);
  77. rcFreeHeightField(heightField_);
  78. rcFreeCompactHeightfield(compactHeightField_);
  79. rcFreeContourSet(contourSet_);
  80. rcFreePolyMesh(polyMesh_);
  81. rcFreePolyMeshDetail(polyMeshDetail_);
  82. ctx_ = 0;
  83. heightField_ = 0;
  84. compactHeightField_ = 0;
  85. contourSet_ = 0;
  86. polyMesh_ = 0;
  87. polyMeshDetail_ = 0;
  88. }
  89. /// World-space bounding box of the navigation mesh tile.
  90. BoundingBox worldBoundingBox_;
  91. /// Vertices from geometries.
  92. PODVector<Vector3> vertices_;
  93. /// Triangle indices from geometries.
  94. PODVector<int> indices_;
  95. /// Recast context.
  96. rcContext* ctx_;
  97. /// Recast heightfield.
  98. rcHeightfield* heightField_;
  99. /// Recast compact heightfield.
  100. rcCompactHeightfield* compactHeightField_;
  101. /// Recast contour set.
  102. rcContourSet* contourSet_;
  103. /// Recast poly mesh.
  104. rcPolyMesh* polyMesh_;
  105. /// Recast detail poly mesh.
  106. rcPolyMeshDetail* polyMeshDetail_;
  107. };
  108. /// Temporary data for finding a path.
  109. struct FindPathData
  110. {
  111. // Polygons.
  112. dtPolyRef polys_[MAX_POLYS];
  113. // Polygons on the path.
  114. dtPolyRef pathPolys_[MAX_POLYS];
  115. // Points on the path.
  116. Vector3 pathPoints_[MAX_POLYS];
  117. // Flags on the path.
  118. unsigned char pathFlags_[MAX_POLYS];
  119. };
  120. OBJECTTYPESTATIC(NavigationMesh);
  121. NavigationMesh::NavigationMesh(Context* context) :
  122. Component(context),
  123. navMesh_(0),
  124. navMeshQuery_(0),
  125. queryFilter_(new dtQueryFilter()),
  126. pathData_(new FindPathData()),
  127. tileSize_(DEFAULT_TILE_SIZE),
  128. cellSize_(DEFAULT_CELL_SIZE),
  129. cellHeight_(DEFAULT_CELL_HEIGHT),
  130. agentHeight_(DEFAULT_AGENT_HEIGHT),
  131. agentRadius_(DEFAULT_AGENT_RADIUS),
  132. agentMaxClimb_(DEFAULT_AGENT_MAX_CLIMB),
  133. agentMaxSlope_(DEFAULT_AGENT_MAX_SLOPE),
  134. regionMinSize_(DEFAULT_REGION_MIN_SIZE),
  135. regionMergeSize_(DEFAULT_REGION_MERGE_SIZE),
  136. edgeMaxLength_(DEFAULT_EDGE_MAX_LENGTH),
  137. edgeMaxError_(DEFAULT_EDGE_MAX_ERROR),
  138. detailSampleDistance_(DEFAULT_DETAIL_SAMPLE_DISTANCE),
  139. detailSampleMaxError_(DEFAULT_DETAIL_SAMPLE_MAX_ERROR),
  140. numTilesX_(0),
  141. numTilesZ_(0)
  142. {
  143. }
  144. NavigationMesh::~NavigationMesh()
  145. {
  146. ReleaseNavigationMesh();
  147. delete queryFilter_;
  148. queryFilter_ = 0;
  149. delete pathData_;
  150. pathData_ = 0;
  151. }
  152. void NavigationMesh::RegisterObject(Context* context)
  153. {
  154. context->RegisterFactory<NavigationMesh>();
  155. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_INT, "Tile Size", GetTileSize, SetTileSize, int, DEFAULT_TILE_SIZE, AM_DEFAULT);
  156. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Cell Size", GetCellSize, SetCellSize, float, DEFAULT_CELL_SIZE, AM_DEFAULT);
  157. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Cell Height", GetCellHeight, SetCellHeight, float, DEFAULT_CELL_HEIGHT, AM_DEFAULT);
  158. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Agent Height", GetAgentHeight, SetAgentHeight, float, DEFAULT_AGENT_HEIGHT, AM_DEFAULT);
  159. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Agent Radius", GetAgentRadius, SetAgentRadius, float, DEFAULT_AGENT_RADIUS, AM_DEFAULT);
  160. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Agent Max Climb", GetAgentMaxClimb, SetAgentMaxClimb, float, DEFAULT_AGENT_MAX_CLIMB, AM_DEFAULT);
  161. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Agent Max Slope", GetAgentMaxSlope, SetAgentMaxSlope, float, DEFAULT_AGENT_MAX_SLOPE, AM_DEFAULT);
  162. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Region Min Size", GetRegionMinSize, SetRegionMinSize, float, DEFAULT_REGION_MIN_SIZE, AM_DEFAULT);
  163. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Region Merge Size", GetRegionMergeSize, SetRegionMergeSize, float, DEFAULT_REGION_MERGE_SIZE, AM_DEFAULT);
  164. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Edge Max Length", GetEdgeMaxLength, SetEdgeMaxLength, float, DEFAULT_EDGE_MAX_LENGTH, AM_DEFAULT);
  165. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Edge Max Error", GetEdgeMaxError, SetEdgeMaxError, float, DEFAULT_EDGE_MAX_ERROR, AM_DEFAULT);
  166. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Detail Sample Distance", GetDetailSampleDistance, SetDetailSampleDistance, float, DEFAULT_DETAIL_SAMPLE_DISTANCE, AM_DEFAULT);
  167. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_FLOAT, "Detail Sample Max Error", GetDetailSampleMaxError, SetDetailSampleMaxError, float, DEFAULT_DETAIL_SAMPLE_MAX_ERROR, AM_DEFAULT);
  168. ACCESSOR_ATTRIBUTE(NavigationMesh, VAR_BUFFER, "Navigation Data", GetNavigationDataAttr, SetNavigationDataAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_FILE | AM_NOEDIT);
  169. }
  170. void NavigationMesh::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  171. {
  172. if (!navMesh_ || !node_)
  173. return;
  174. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  175. const dtNavMesh* navMesh = navMesh_;
  176. for (int z = 0; z < numTilesZ_; ++z)
  177. {
  178. for (int x = 0; x < numTilesX_; ++x)
  179. {
  180. const dtMeshTile* tile = navMesh->getTileAt(x, z, 0);
  181. if (!tile)
  182. continue;
  183. for (int i = 0; i < tile->header->polyCount; ++i)
  184. {
  185. dtPoly* poly = tile->polys + i;
  186. for (unsigned j = 0; j < poly->vertCount; ++j)
  187. {
  188. debug->AddLine(
  189. worldTransform * *reinterpret_cast<const Vector3*>(&tile->verts[poly->verts[j] * 3]),
  190. worldTransform * *reinterpret_cast<const Vector3*>(&tile->verts[poly->verts[(j + 1) % poly->vertCount] * 3]),
  191. Color::YELLOW,
  192. depthTest
  193. );
  194. }
  195. }
  196. }
  197. }
  198. }
  199. void NavigationMesh::SetTileSize(int size)
  200. {
  201. tileSize_ = Max(size, 16);
  202. }
  203. void NavigationMesh::SetCellSize(float size)
  204. {
  205. cellSize_ = Max(size, M_EPSILON);
  206. }
  207. void NavigationMesh::SetCellHeight(float height)
  208. {
  209. cellHeight_ = Max(height, M_EPSILON);
  210. }
  211. void NavigationMesh::SetAgentHeight(float height)
  212. {
  213. agentHeight_ = Max(height, M_EPSILON);
  214. }
  215. void NavigationMesh::SetAgentRadius(float radius)
  216. {
  217. agentRadius_ = Max(radius, M_EPSILON);
  218. }
  219. void NavigationMesh::SetAgentMaxClimb(float maxClimb)
  220. {
  221. agentMaxClimb_ = Max(maxClimb, M_EPSILON);
  222. }
  223. void NavigationMesh::SetAgentMaxSlope(float maxSlope)
  224. {
  225. agentMaxSlope_ = Max(maxSlope, 0.0f);
  226. }
  227. void NavigationMesh::SetRegionMinSize(float size)
  228. {
  229. regionMinSize_ = Max(size, M_EPSILON);
  230. }
  231. void NavigationMesh::SetRegionMergeSize(float size)
  232. {
  233. regionMergeSize_ = Max(size, M_EPSILON);
  234. }
  235. void NavigationMesh::SetEdgeMaxLength(float length)
  236. {
  237. edgeMaxLength_ = Max(length, M_EPSILON);
  238. }
  239. void NavigationMesh::SetEdgeMaxError(float error)
  240. {
  241. edgeMaxError_ = Max(error, M_EPSILON);
  242. }
  243. void NavigationMesh::SetDetailSampleDistance(float distance)
  244. {
  245. detailSampleDistance_ = Max(distance, M_EPSILON);
  246. }
  247. void NavigationMesh::SetDetailSampleMaxError(float error)
  248. {
  249. detailSampleMaxError_ = Max(error, M_EPSILON);
  250. }
  251. bool NavigationMesh::Build()
  252. {
  253. PROFILE(BuildNavigationMesh);
  254. ReleaseNavigationMesh();
  255. if (!node_)
  256. return false;
  257. if (!node_->GetWorldScale().Equals(Vector3::ONE))
  258. LOGWARNING("Navigation mesh root node has scaling. Agent parameters may not work as intended");
  259. Vector<NavigationGeometryInfo> geometryList;
  260. CollectGeometries(geometryList);
  261. if (geometryList.Empty())
  262. return true; // Nothing to do
  263. // Build the combined bounding box
  264. for (unsigned i = 0; i < geometryList.Size(); ++i)
  265. boundingBox_.Merge(geometryList[i].boundingBox_);
  266. {
  267. PROFILE(BuildNavigationMesh);
  268. // Calculate number of tiles
  269. int gridW = 0, gridH = 0;
  270. float tileEdgeLength = (float)tileSize_ * cellSize_;
  271. rcCalcGridSize(&boundingBox_.min_.x_, &boundingBox_.max_.x_, cellSize_, &gridW, &gridH);
  272. numTilesX_ = (gridW + tileSize_ - 1) / tileSize_;
  273. numTilesZ_ = (gridH + tileSize_ - 1) / tileSize_;
  274. // Calculate max. number of tiles and polygons, 22 bits available to identify both tile & polygon within tile
  275. unsigned maxTiles = NextPowerOfTwo(numTilesX_ * numTilesZ_);
  276. unsigned tileBits = 0;
  277. unsigned temp = maxTiles;
  278. while (temp > 1)
  279. {
  280. temp >>= 1;
  281. ++tileBits;
  282. }
  283. unsigned maxPolys = 1 << (22 - tileBits);
  284. dtNavMeshParams params;
  285. rcVcopy(params.orig, &boundingBox_.min_.x_);
  286. params.tileWidth = tileEdgeLength;
  287. params.tileHeight = tileEdgeLength;
  288. params.maxTiles = maxTiles;
  289. params.maxPolys = maxPolys;
  290. navMesh_ = dtAllocNavMesh();
  291. if (!navMesh_)
  292. {
  293. LOGERROR("Could not allocate navigation mesh");
  294. return false;
  295. }
  296. if (dtStatusFailed(navMesh_->init(&params)))
  297. {
  298. LOGERROR("Could not initialize navigation mesh");
  299. ReleaseNavigationMesh();
  300. return false;
  301. }
  302. // Build each tile
  303. unsigned numTiles = 0;
  304. for (int z = 0; z < numTilesZ_; ++z)
  305. {
  306. for (int x = 0; x < numTilesX_; ++x)
  307. {
  308. if (BuildTile(geometryList, x, z))
  309. ++numTiles;
  310. }
  311. }
  312. LOGDEBUG("Built navigation mesh with " + String(numTiles) + " tiles");
  313. return true;
  314. }
  315. }
  316. bool NavigationMesh::Build(const BoundingBox& boundingBox)
  317. {
  318. PROFILE(BuildPartialNavigationMesh);
  319. /// \todo Partial rebuild does not modify the bounding box. Modifying in Y-direction could be supported
  320. if (!node_)
  321. return false;
  322. if (!navMesh_)
  323. {
  324. LOGERROR("Navigation mesh must first be built fully before it can be partially rebuilt");
  325. return false;
  326. }
  327. if (!node_->GetWorldScale().Equals(Vector3::ONE))
  328. LOGWARNING("Navigation mesh root node has scaling. Agent parameters may not work as intended");
  329. BoundingBox localSpaceBox = boundingBox.Transformed(node_->GetWorldTransform().Inverse());
  330. float tileEdgeLength = (float)tileSize_ * cellSize_;
  331. Vector<NavigationGeometryInfo> geometryList;
  332. CollectGeometries(geometryList);
  333. int sx = Clamp((int)((localSpaceBox.min_.x_ - boundingBox_.min_.x_) / tileEdgeLength), 0, numTilesX_ - 1);
  334. int sz = Clamp((int)((localSpaceBox.min_.z_ - boundingBox_.min_.z_) / tileEdgeLength), 0, numTilesZ_ - 1);
  335. int ex = Clamp((int)((localSpaceBox.max_.x_ - boundingBox_.min_.x_) / tileEdgeLength), 0, numTilesX_ - 1);
  336. int ez = Clamp((int)((localSpaceBox.max_.z_ - boundingBox_.min_.z_) / tileEdgeLength), 0, numTilesZ_ - 1);
  337. unsigned numTiles = 0;
  338. for (int z = sz; z <= ez; ++z)
  339. {
  340. for (int x = sx; x <= ex; ++x)
  341. {
  342. if (BuildTile(geometryList, x, z))
  343. ++numTiles;
  344. }
  345. }
  346. LOGDEBUG("Rebuilt " + String(numTiles) + " tiles of the navigation mesh");
  347. return true;
  348. }
  349. void NavigationMesh::FindPath(PODVector<Vector3>& dest, const Vector3& start, const Vector3& end, const Vector3& extents)
  350. {
  351. PROFILE(FindPath);
  352. dest.Clear();
  353. if (!InitializeQuery())
  354. return;
  355. // Navigation data is in local space. Transform path points from world to local
  356. const Matrix3x4& transform = node_->GetWorldTransform();
  357. Matrix3x4 inverse = transform.Inverse();
  358. Vector3 localStart = inverse * start;
  359. Vector3 localEnd = inverse * end;
  360. dtPolyRef startRef;
  361. dtPolyRef endRef;
  362. navMeshQuery_->findNearestPoly(&localStart.x_, &extents.x_, queryFilter_, &startRef, 0);
  363. navMeshQuery_->findNearestPoly(&localEnd.x_, &extents.x_, queryFilter_, &endRef, 0);
  364. if (!startRef || !endRef)
  365. return;
  366. int numPolys = 0;
  367. int numPathPoints = 0;
  368. navMeshQuery_->findPath(startRef, endRef, &localStart.x_, &localEnd.x_, queryFilter_, pathData_->polys_, &numPolys,
  369. MAX_POLYS);
  370. if (!numPolys)
  371. return;
  372. Vector3 actualLocalEnd = localEnd;
  373. // If full path was not found, clamp end point to the end polygon
  374. if (pathData_->polys_[numPolys - 1] != endRef)
  375. navMeshQuery_->closestPointOnPoly(pathData_->polys_[numPolys - 1], &localEnd.x_, &actualLocalEnd.x_);
  376. navMeshQuery_->findStraightPath(&localStart.x_, &actualLocalEnd.x_, pathData_->polys_, numPolys,
  377. &pathData_->pathPoints_[0].x_, pathData_->pathFlags_, pathData_->pathPolys_, &numPathPoints, MAX_POLYS);
  378. // Transform path result back to world space
  379. for (int i = 0; i < numPathPoints; ++i)
  380. dest.Push(transform * pathData_->pathPoints_[i]);
  381. }
  382. Vector3 NavigationMesh::GetRandomPoint()
  383. {
  384. if (!InitializeQuery())
  385. return Vector3::ZERO;
  386. dtPolyRef polyRef;
  387. Vector3 point(Vector3::ZERO);
  388. navMeshQuery_->findRandomPoint(queryFilter_, Random, &polyRef, &point.x_);
  389. return node_->GetWorldTransform() * point;
  390. }
  391. Vector3 NavigationMesh::GetRandomPointInCircle(const Vector3& center, float radius, const Vector3& extents)
  392. {
  393. if (!InitializeQuery())
  394. return center;
  395. const Matrix3x4& transform = node_->GetWorldTransform();
  396. Matrix3x4 inverse = transform.Inverse();
  397. Vector3 localCenter = inverse * center;
  398. dtPolyRef startRef;
  399. navMeshQuery_->findNearestPoly(&localCenter.x_, &extents.x_, queryFilter_, &startRef, 0);
  400. if (!startRef)
  401. return center;
  402. dtPolyRef polyRef;
  403. Vector3 point(localCenter);
  404. navMeshQuery_->findRandomPointAroundCircle(startRef, &localCenter.x_, radius, queryFilter_, Random, &polyRef, &point.x_);
  405. return transform * point;
  406. }
  407. float NavigationMesh::GetDistanceToWall(const Vector3& point, float radius, const Vector3& extents)
  408. {
  409. if (!InitializeQuery())
  410. return radius;
  411. const Matrix3x4& transform = node_->GetWorldTransform();
  412. Matrix3x4 inverse = transform.Inverse();
  413. Vector3 localPoint = inverse * point;
  414. dtPolyRef startRef;
  415. navMeshQuery_->findNearestPoly(&localPoint.x_, &extents.x_, queryFilter_, &startRef, 0);
  416. if (!startRef)
  417. return radius;
  418. float hitDist = radius;
  419. Vector3 hitPos;
  420. Vector3 hitNormal;
  421. navMeshQuery_->findDistanceToWall(startRef, &localPoint.x_, radius, queryFilter_, &hitDist, &hitPos.x_, &hitNormal.x_);
  422. return hitDist;
  423. }
  424. Vector3 NavigationMesh::Raycast(const Vector3& start, const Vector3& end, const Vector3& extents)
  425. {
  426. if (!InitializeQuery())
  427. return end;
  428. const Matrix3x4& transform = node_->GetWorldTransform();
  429. Matrix3x4 inverse = transform.Inverse();
  430. Vector3 localStart = inverse * start;
  431. Vector3 localEnd = inverse * end;
  432. dtPolyRef startRef;
  433. navMeshQuery_->findNearestPoly(&localStart.x_, &extents.x_, queryFilter_, &startRef, 0);
  434. if (!startRef)
  435. return end;
  436. Vector3 localHitNormal;
  437. float t;
  438. int numPolys;
  439. navMeshQuery_->raycast(startRef, &localStart.x_, &localEnd.x_, queryFilter_, &t, &localHitNormal.x_, pathData_->polys_, &numPolys, MAX_POLYS);
  440. if (t == FLT_MAX)
  441. t = 1.0f;
  442. return start.Lerp(end, t);
  443. }
  444. BoundingBox NavigationMesh::GetWorldBoundingBox() const
  445. {
  446. return node_ ? boundingBox_.Transformed(node_->GetWorldTransform()) : boundingBox_;
  447. }
  448. void NavigationMesh::SetNavigationDataAttr(PODVector<unsigned char> data)
  449. {
  450. ReleaseNavigationMesh();
  451. if (!data.Size())
  452. return;
  453. MemoryBuffer buffer(data);
  454. boundingBox_ = buffer.ReadBoundingBox();
  455. numTilesX_ = buffer.ReadInt();
  456. numTilesZ_ = buffer.ReadInt();
  457. dtNavMeshParams params;
  458. rcVcopy(params.orig, &boundingBox_.min_.x_);
  459. params.tileWidth = buffer.ReadFloat();
  460. params.tileHeight = buffer.ReadFloat();
  461. params.maxTiles = buffer.ReadInt();
  462. params.maxPolys = buffer.ReadInt();
  463. navMesh_ = dtAllocNavMesh();
  464. if (!navMesh_)
  465. {
  466. LOGERROR("Could not allocate navigation mesh");
  467. return;
  468. }
  469. if (dtStatusFailed(navMesh_->init(&params)))
  470. {
  471. LOGERROR("Could not initialize navigation mesh");
  472. ReleaseNavigationMesh();
  473. return;
  474. }
  475. unsigned numTiles = 0;
  476. while (!buffer.IsEof())
  477. {
  478. int x = buffer.ReadInt();
  479. int z = buffer.ReadInt();
  480. dtTileRef tileRef = buffer.ReadUInt();
  481. unsigned navDataSize = buffer.ReadUInt();
  482. unsigned char* navData = (unsigned char*)dtAlloc(navDataSize, DT_ALLOC_PERM);
  483. if (!navData)
  484. {
  485. LOGERROR("Could not allocate data for navigation mesh tile");
  486. return;
  487. }
  488. buffer.Read(navData, navDataSize);
  489. if (dtStatusFailed(navMesh_->addTile(navData, navDataSize, DT_TILE_FREE_DATA, 0, 0)))
  490. {
  491. LOGERROR("Failed to add navigation mesh tile");
  492. dtFree(navData);
  493. return;
  494. }
  495. else
  496. ++numTiles;
  497. }
  498. LOGDEBUG("Created navigation mesh with " + String(numTiles) + " tiles from serialized data");
  499. }
  500. PODVector<unsigned char> NavigationMesh::GetNavigationDataAttr() const
  501. {
  502. VectorBuffer ret;
  503. if (navMesh_)
  504. {
  505. ret.WriteBoundingBox(boundingBox_);
  506. ret.WriteInt(numTilesX_);
  507. ret.WriteInt(numTilesZ_);
  508. const dtNavMeshParams* params = navMesh_->getParams();
  509. ret.WriteFloat(params->tileWidth);
  510. ret.WriteFloat(params->tileHeight);
  511. ret.WriteInt(params->maxTiles);
  512. ret.WriteInt(params->maxPolys);
  513. const dtNavMesh* navMesh = navMesh_;
  514. for (int z = 0; z < numTilesZ_; ++z)
  515. {
  516. for (int x = 0; x < numTilesX_; ++x)
  517. {
  518. const dtMeshTile* tile = navMesh->getTileAt(x, z, 0);
  519. if (!tile)
  520. continue;
  521. ret.WriteInt(x);
  522. ret.WriteInt(z);
  523. ret.WriteUInt(navMesh->getTileRef(tile));
  524. ret.WriteUInt(tile->dataSize);
  525. ret.Write(tile->data, tile->dataSize);
  526. }
  527. }
  528. }
  529. return ret.GetBuffer();
  530. }
  531. void NavigationMesh::CollectGeometries(Vector<NavigationGeometryInfo>& geometryList)
  532. {
  533. PROFILE(CollectNavigationGeometry);
  534. // Get Navigable components from child nodes, not from whole scene. This makes it possible to partition
  535. // the scene into several navigation meshes
  536. PODVector<Navigable*> navigables;
  537. node_->GetComponents<Navigable>(navigables, true);
  538. HashSet<Node*> processedNodes;
  539. for (unsigned i = 0; i < navigables.Size(); ++i)
  540. {
  541. if (navigables[i]->IsEnabledEffective())
  542. CollectGeometries(geometryList, navigables[i]->GetNode(), processedNodes, navigables[i]->IsRecursive());
  543. }
  544. }
  545. void NavigationMesh::CollectGeometries(Vector<NavigationGeometryInfo>& geometryList, Node* node, HashSet<Node*>& processedNodes, bool recursive)
  546. {
  547. // Make sure nodes are not included twice
  548. if (processedNodes.Contains(node))
  549. return;
  550. processedNodes.Insert(node);
  551. Matrix3x4 inverse = node_->GetWorldTransform().Inverse();
  552. // Prefer compatible physics collision shapes (triangle mesh, convex hull, box) if found.
  553. // Then fallback to visible geometry
  554. PODVector<CollisionShape*> collisionShapes;
  555. node->GetComponents<CollisionShape>(collisionShapes);
  556. bool collisionShapeFound = false;
  557. for (unsigned i = 0; i < collisionShapes.Size(); ++i)
  558. {
  559. CollisionShape* shape = collisionShapes[i];
  560. if (!shape->IsEnabledEffective())
  561. continue;
  562. ShapeType type = shape->GetShapeType();
  563. if ((type == SHAPE_BOX || type == SHAPE_TRIANGLEMESH || type == SHAPE_CONVEXHULL) && shape->GetCollisionShape())
  564. {
  565. NavigationGeometryInfo info;
  566. Matrix3x4 scaleMatrix(Matrix3x4::IDENTITY);
  567. scaleMatrix.SetScale(shape->GetSize());
  568. info.component_ = shape;
  569. info.transform_ = inverse * node->GetWorldTransform() * scaleMatrix;
  570. info.boundingBox_ = shape->GetWorldBoundingBox().Transformed(inverse);
  571. geometryList.Push(info);
  572. collisionShapeFound = true;
  573. }
  574. }
  575. if (!collisionShapeFound)
  576. {
  577. PODVector<Drawable*> drawables;
  578. node->GetDerivedComponents<Drawable>(drawables);
  579. for (unsigned i = 0; i < drawables.Size(); ++i)
  580. {
  581. /// \todo Evaluate whether should handle other types. Now StaticModel & TerrainPatch are supported, others skipped
  582. Drawable* drawable = drawables[i];
  583. if (!drawable->IsEnabledEffective())
  584. continue;
  585. NavigationGeometryInfo info;
  586. if (drawable->GetType() == StaticModel::GetTypeStatic())
  587. info.lodLevel_ = static_cast<StaticModel*>(drawable)->GetOcclusionLodLevel();
  588. else if (drawable->GetType() == TerrainPatch::GetTypeStatic())
  589. info.lodLevel_ = 0;
  590. else
  591. continue;
  592. info.component_ = drawable;
  593. info.transform_ = inverse * node->GetWorldTransform();
  594. info.boundingBox_ = drawable->GetWorldBoundingBox().Transformed(inverse);
  595. geometryList.Push(info);
  596. }
  597. }
  598. if (recursive)
  599. {
  600. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  601. for(unsigned i = 0; i < children.Size(); ++i)
  602. CollectGeometries(geometryList, children[i], processedNodes, recursive);
  603. }
  604. }
  605. void NavigationMesh::GetTileGeometry(NavigationBuildData& build, Vector<NavigationGeometryInfo>& geometryList, BoundingBox& box)
  606. {
  607. for (unsigned i = 0; i < geometryList.Size(); ++i)
  608. {
  609. if (box.IsInsideFast(geometryList[i].boundingBox_) != OUTSIDE)
  610. {
  611. const Matrix3x4& transform = geometryList[i].transform_;
  612. CollisionShape* shape = dynamic_cast<CollisionShape*>(geometryList[i].component_);
  613. if (shape)
  614. {
  615. switch (shape->GetShapeType())
  616. {
  617. case SHAPE_TRIANGLEMESH:
  618. {
  619. Model* model = shape->GetModel();
  620. if (!model)
  621. continue;
  622. unsigned lodLevel = shape->GetLodLevel();
  623. for (unsigned j = 0; j < model->GetNumGeometries(); ++j)
  624. AddTriMeshGeometry(build, model->GetGeometry(j, lodLevel), transform);
  625. }
  626. break;
  627. case SHAPE_CONVEXHULL:
  628. {
  629. ConvexData* data = static_cast<ConvexData*>(shape->GetGeometryData());
  630. if (!data)
  631. continue;
  632. unsigned numVertices = data->vertexCount_;
  633. unsigned numIndices = data->indexCount_;
  634. unsigned destVertexStart = build.vertices_.Size();
  635. for (unsigned j = 0; j < numVertices; ++j)
  636. build.vertices_.Push(transform * data->vertexData_[j]);
  637. for (unsigned j = 0; j < numIndices; ++j)
  638. build.indices_.Push(data->indexData_[j] + destVertexStart);
  639. }
  640. break;
  641. case SHAPE_BOX:
  642. {
  643. unsigned destVertexStart = build.vertices_.Size();
  644. build.vertices_.Push(transform * Vector3(-0.5f, 0.5f, -0.5f));
  645. build.vertices_.Push(transform * Vector3(0.5f, 0.5f, -0.5f));
  646. build.vertices_.Push(transform * Vector3(0.5f, -0.5f, -0.5f));
  647. build.vertices_.Push(transform * Vector3(-0.5f, -0.5f, -0.5f));
  648. build.vertices_.Push(transform * Vector3(-0.5f, 0.5f, 0.5f));
  649. build.vertices_.Push(transform * Vector3(0.5f, 0.5f, 0.5f));
  650. build.vertices_.Push(transform * Vector3(0.5f, -0.5f, 0.5f));
  651. build.vertices_.Push(transform * Vector3(-0.5f, -0.5f, 0.5f));
  652. const unsigned indices[] = {
  653. 0, 1, 2, 0, 2, 3, 1, 5, 6, 1, 6, 2, 4, 5, 1, 4, 1, 0, 5, 4, 7, 5, 7, 6,
  654. 4, 0, 3, 4, 3, 7, 1, 0, 4, 1, 4, 5
  655. };
  656. for (unsigned j = 0; j < 36; ++j)
  657. build.indices_.Push(indices[j] + destVertexStart);
  658. }
  659. break;
  660. }
  661. }
  662. else
  663. {
  664. Drawable* drawable = dynamic_cast<Drawable*>(geometryList[i].component_);
  665. if (drawable)
  666. {
  667. const Vector<SourceBatch>& batches = drawable->GetBatches();
  668. for (unsigned j = 0; j < batches.Size(); ++j)
  669. AddTriMeshGeometry(build, drawable->GetLodGeometry(j, geometryList[i].lodLevel_), transform);
  670. }
  671. }
  672. }
  673. }
  674. }
  675. void NavigationMesh::AddTriMeshGeometry(NavigationBuildData& build, Geometry* geometry, const Matrix3x4& transform)
  676. {
  677. if (!geometry)
  678. return;
  679. const unsigned char* vertexData;
  680. const unsigned char* indexData;
  681. unsigned vertexSize;
  682. unsigned indexSize;
  683. unsigned elementMask;
  684. geometry->GetRawData(vertexData, vertexSize, indexData, indexSize, elementMask);
  685. if (!vertexData || !indexData || (elementMask & MASK_POSITION) == 0)
  686. return;
  687. unsigned srcIndexStart = geometry->GetIndexStart();
  688. unsigned srcIndexCount = geometry->GetIndexCount();
  689. unsigned srcVertexStart = geometry->GetVertexStart();
  690. unsigned srcVertexCount = geometry->GetVertexCount();
  691. if (!srcIndexCount)
  692. return;
  693. unsigned destVertexStart = build.vertices_.Size();
  694. for (unsigned k = srcVertexStart; k < srcVertexStart + srcVertexCount; ++k)
  695. {
  696. Vector3 vertex = transform * *((const Vector3*)(&vertexData[k * vertexSize]));
  697. build.vertices_.Push(vertex);
  698. }
  699. // Copy remapped indices
  700. if (indexSize == sizeof(unsigned short))
  701. {
  702. const unsigned short* indices = ((const unsigned short*)indexData) + srcIndexStart;
  703. const unsigned short* indicesEnd = indices + srcIndexCount;
  704. while (indices < indicesEnd)
  705. {
  706. build.indices_.Push(*indices - srcVertexStart + destVertexStart);
  707. ++indices;
  708. }
  709. }
  710. else
  711. {
  712. const unsigned* indices = ((const unsigned*)indexData) + srcIndexStart;
  713. const unsigned* indicesEnd = indices + srcIndexCount;
  714. while (indices < indicesEnd)
  715. {
  716. build.indices_.Push(*indices - srcVertexStart + destVertexStart);
  717. ++indices;
  718. }
  719. }
  720. }
  721. bool NavigationMesh::BuildTile(Vector<NavigationGeometryInfo>& geometryList, int x, int z)
  722. {
  723. PROFILE(BuildNavigationMeshTile);
  724. // Remove previous tile (if any)
  725. navMesh_->removeTile(navMesh_->getTileRefAt(x, z, 0), 0, 0);
  726. float tileEdgeLength = (float)tileSize_ * cellSize_;
  727. BoundingBox tileBoundingBox(Vector3(
  728. boundingBox_.min_.x_ + tileEdgeLength * (float)x,
  729. boundingBox_.min_.y_,
  730. boundingBox_.min_.z_ + tileEdgeLength * (float)z
  731. ),
  732. Vector3(
  733. boundingBox_.min_.x_ + tileEdgeLength * (float)(x + 1),
  734. boundingBox_.max_.y_,
  735. boundingBox_.min_.z_ + tileEdgeLength * (float)(z + 1)
  736. ));
  737. NavigationBuildData build;
  738. rcConfig cfg;
  739. memset(&cfg, 0, sizeof cfg);
  740. cfg.cs = cellSize_;
  741. cfg.ch = cellHeight_;
  742. cfg.walkableSlopeAngle = agentMaxSlope_;
  743. cfg.walkableHeight = (int)ceilf(agentHeight_ / cfg.ch);
  744. cfg.walkableClimb = (int)floorf(agentMaxClimb_ / cfg.ch);
  745. cfg.walkableRadius = (int)ceilf(agentRadius_ / cfg.cs);
  746. cfg.maxEdgeLen = (int)(edgeMaxLength_ / cellSize_);
  747. cfg.maxSimplificationError = edgeMaxError_;
  748. cfg.minRegionArea = (int)sqrtf(regionMinSize_);
  749. cfg.mergeRegionArea = (int)sqrtf(regionMergeSize_);
  750. cfg.maxVertsPerPoly = 6;
  751. cfg.tileSize = tileSize_;
  752. cfg.borderSize = cfg.walkableRadius + 3; // Add padding
  753. cfg.width = cfg.tileSize + cfg.borderSize * 2;
  754. cfg.height = cfg.tileSize + cfg.borderSize * 2;
  755. cfg.detailSampleDist = detailSampleDistance_ < 0.9f ? 0.0f : cellSize_ * detailSampleDistance_;
  756. cfg.detailSampleMaxError = cellHeight_ * detailSampleMaxError_;
  757. rcVcopy(cfg.bmin, &tileBoundingBox.min_.x_);
  758. rcVcopy(cfg.bmax, &tileBoundingBox.max_.x_);
  759. cfg.bmin[0] -= cfg.borderSize * cfg.cs;
  760. cfg.bmin[2] -= cfg.borderSize * cfg.cs;
  761. cfg.bmax[0] += cfg.borderSize * cfg.cs;
  762. cfg.bmax[2] += cfg.borderSize * cfg.cs;
  763. BoundingBox expandedBox(*reinterpret_cast<Vector3*>(cfg.bmin), *reinterpret_cast<Vector3*>(cfg.bmax));
  764. GetTileGeometry(build, geometryList, expandedBox);
  765. if (build.vertices_.Empty() || build.indices_.Empty())
  766. return true; // Nothing to do
  767. build.heightField_ = rcAllocHeightfield();
  768. if (!build.heightField_)
  769. {
  770. LOGERROR("Could not allocate heightfield");
  771. return false;
  772. }
  773. if (!rcCreateHeightfield(build.ctx_, *build.heightField_, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs,
  774. cfg.ch))
  775. {
  776. LOGERROR("Could not create heightfield");
  777. return false;
  778. }
  779. unsigned numTriangles = build.indices_.Size() / 3;
  780. SharedArrayPtr<unsigned char> triAreas(new unsigned char[numTriangles]);
  781. memset(triAreas.Get(), 0, numTriangles);
  782. rcMarkWalkableTriangles(build.ctx_, cfg.walkableSlopeAngle, &build.vertices_[0].x_, build.vertices_.Size(),
  783. &build.indices_[0], numTriangles, triAreas.Get());
  784. rcRasterizeTriangles(build.ctx_, &build.vertices_[0].x_, build.vertices_.Size(), &build.indices_[0],
  785. triAreas.Get(), numTriangles, *build.heightField_, cfg.walkableClimb);
  786. rcFilterLowHangingWalkableObstacles(build.ctx_, cfg.walkableClimb, *build.heightField_);
  787. rcFilterLedgeSpans(build.ctx_, cfg.walkableHeight, cfg.walkableClimb, *build.heightField_);
  788. rcFilterWalkableLowHeightSpans(build.ctx_, cfg.walkableHeight, *build.heightField_);
  789. build.compactHeightField_ = rcAllocCompactHeightfield();
  790. if (!build.compactHeightField_)
  791. {
  792. LOGERROR("Could not allocate create compact heightfield");
  793. return false;
  794. }
  795. if (!rcBuildCompactHeightfield(build.ctx_, cfg.walkableHeight, cfg.walkableClimb, *build.heightField_,
  796. *build.compactHeightField_))
  797. {
  798. LOGERROR("Could not build compact heightfield");
  799. return false;
  800. }
  801. if (!rcErodeWalkableArea(build.ctx_, cfg.walkableRadius, *build.compactHeightField_))
  802. {
  803. LOGERROR("Could not erode compact heightfield");
  804. return false;
  805. }
  806. if (!rcBuildDistanceField(build.ctx_, *build.compactHeightField_))
  807. {
  808. LOGERROR("Could not build distance field");
  809. return false;
  810. }
  811. if (!rcBuildRegions(build.ctx_, *build.compactHeightField_, cfg.borderSize, cfg.minRegionArea,
  812. cfg.mergeRegionArea))
  813. {
  814. LOGERROR("Could not build regions");
  815. return false;
  816. }
  817. build.contourSet_ = rcAllocContourSet();
  818. if (!build.contourSet_)
  819. {
  820. LOGERROR("Could not allocate contour set");
  821. return false;
  822. }
  823. if (!rcBuildContours(build.ctx_, *build.compactHeightField_, cfg.maxSimplificationError, cfg.maxEdgeLen,
  824. *build.contourSet_))
  825. {
  826. LOGERROR("Could not create contours");
  827. return false;
  828. }
  829. build.polyMesh_ = rcAllocPolyMesh();
  830. if (!build.polyMesh_)
  831. {
  832. LOGERROR("Could not allocate poly mesh");
  833. return false;
  834. }
  835. if (!rcBuildPolyMesh(build.ctx_, *build.contourSet_, cfg.maxVertsPerPoly, *build.polyMesh_))
  836. {
  837. LOGERROR("Could not triangulate contours");
  838. return false;
  839. }
  840. build.polyMeshDetail_ = rcAllocPolyMeshDetail();
  841. if (!build.polyMeshDetail_)
  842. {
  843. LOGERROR("Could not allocate detail mesh");
  844. return false;
  845. }
  846. if (!rcBuildPolyMeshDetail(build.ctx_, *build.polyMesh_, *build.compactHeightField_, cfg.detailSampleDist,
  847. cfg.detailSampleMaxError, *build.polyMeshDetail_))
  848. {
  849. LOGERROR("Could not build detail mesh");
  850. return false;
  851. }
  852. // Set polygon flags
  853. /// \todo Allow to define custom flags
  854. for (int i = 0; i < build.polyMesh_->npolys; ++i)
  855. {
  856. if (build.polyMesh_->areas[i] == RC_WALKABLE_AREA)
  857. build.polyMesh_->flags[i] = 0x1;
  858. }
  859. unsigned char* navData = 0;
  860. int navDataSize = 0;
  861. dtNavMeshCreateParams params;
  862. memset(&params, 0, sizeof params);
  863. params.verts = build.polyMesh_->verts;
  864. params.vertCount = build.polyMesh_->nverts;
  865. params.polys = build.polyMesh_->polys;
  866. params.polyAreas = build.polyMesh_->areas;
  867. params.polyFlags = build.polyMesh_->flags;
  868. params.polyCount = build.polyMesh_->npolys;
  869. params.nvp = build.polyMesh_->nvp;
  870. params.detailMeshes = build.polyMeshDetail_->meshes;
  871. params.detailVerts = build.polyMeshDetail_->verts;
  872. params.detailVertsCount = build.polyMeshDetail_->nverts;
  873. params.detailTris = build.polyMeshDetail_->tris;
  874. params.detailTriCount = build.polyMeshDetail_->ntris;
  875. params.walkableHeight = agentHeight_;
  876. params.walkableRadius = agentRadius_;
  877. params.walkableClimb = agentMaxClimb_;
  878. params.tileX = x;
  879. params.tileY = z;
  880. rcVcopy(params.bmin, build.polyMesh_->bmin);
  881. rcVcopy(params.bmax, build.polyMesh_->bmax);
  882. params.cs = cfg.cs;
  883. params.ch = cfg.ch;
  884. params.buildBvTree = true;
  885. if (!dtCreateNavMeshData(&params, &navData, &navDataSize))
  886. {
  887. LOGERROR("Could not build navigation mesh tile data");
  888. return false;
  889. }
  890. if (dtStatusFailed(navMesh_->addTile(navData, navDataSize, DT_TILE_FREE_DATA, 0, 0)))
  891. {
  892. LOGERROR("Failed to add navigation mesh tile");
  893. dtFree(navData);
  894. return false;
  895. }
  896. return true;
  897. }
  898. bool NavigationMesh::InitializeQuery()
  899. {
  900. if (!navMesh_ || !node_)
  901. return false;
  902. if (navMeshQuery_)
  903. return true;
  904. navMeshQuery_ = dtAllocNavMeshQuery();
  905. if (!navMeshQuery_)
  906. {
  907. LOGERROR("Could not create navigation mesh query");
  908. return false;
  909. }
  910. if (dtStatusFailed(navMeshQuery_->init(navMesh_, MAX_POLYS)))
  911. {
  912. LOGERROR("Could not init navigation mesh query");
  913. return false;
  914. }
  915. return true;
  916. }
  917. void NavigationMesh::ReleaseNavigationMesh()
  918. {
  919. dtFreeNavMesh(navMesh_);
  920. navMesh_ = 0;
  921. dtFreeNavMeshQuery(navMeshQuery_);
  922. navMeshQuery_ = 0;
  923. numTilesX_ = 0;
  924. numTilesZ_ = 0;
  925. boundingBox_.min_ = boundingBox_.max_ = Vector3::ZERO;
  926. boundingBox_.defined_ = false;
  927. }
  928. }