RecastNavigationMeshComponentController.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "RecastProcessing.h"
  9. #include <DetourDebugDraw.h>
  10. #include <DetourNavMeshBuilder.h>
  11. #include <AzCore/Console/Console.h>
  12. #include <AzCore/Debug/Profiler.h>
  13. #include <AzCore/std/smart_ptr/make_shared.h>
  14. #include <AzFramework/Components/CameraBus.h>
  15. #include <AzFramework/Physics/PhysicsScene.h>
  16. #include <Misc/RecastNavigationMeshComponentController.h>
  17. #include <RecastNavigation/RecastNavigationProviderBus.h>
  18. AZ_DEFINE_BUDGET(Navigation);
  19. AZ_CVAR(
  20. bool, cl_navmesh_debug, false, nullptr, AZ::ConsoleFunctorFlags::Null,
  21. "If enabled, draw debug visual information about a navigation mesh");
  22. AZ_CVAR(
  23. float, cl_navmesh_debugRadius, 25.f, nullptr, AZ::ConsoleFunctorFlags::Null,
  24. "Limit debug draw to within a specified distance from the active camera");
  25. AZ_CVAR(
  26. AZ::u32, bg_navmesh_threads, 2, nullptr, AZ::ConsoleFunctorFlags::Null,
  27. "Number of threads to use to process tiles for each RecastNavigationMeshComponentController");
  28. namespace RecastNavigation
  29. {
  30. void RecastNavigationMeshComponentController::Reflect(AZ::ReflectContext* context)
  31. {
  32. RecastNavigationMeshConfig::Reflect(context);
  33. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  34. {
  35. serializeContext->Class<RecastNavigationMeshComponentController>()
  36. ->Field("Configuration", &RecastNavigationMeshComponentController::m_configuration)
  37. ->Version(1)
  38. ;
  39. }
  40. }
  41. void RecastNavigationMeshComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  42. {
  43. provided.push_back(AZ_CRC_CE("RecastNavigationMeshComponent"));
  44. }
  45. void RecastNavigationMeshComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  46. {
  47. incompatible.push_back(AZ_CRC_CE("RecastNavigationMeshComponent"));
  48. }
  49. void RecastNavigationMeshComponentController::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  50. {
  51. // This can be satisfied by @RecastNavigationPhysXProviderComponent or a user-defined component.
  52. required.push_back(AZ_CRC_CE("RecastNavigationProviderService"));
  53. }
  54. bool RecastNavigationMeshComponentController::UpdateNavigationMeshBlockUntilCompleted()
  55. {
  56. bool notInProgress = false;
  57. if (!m_updateInProgress.compare_exchange_strong(notInProgress, true))
  58. {
  59. return false;
  60. }
  61. AZStd::vector<AZStd::shared_ptr<TileGeometry>> tiles;
  62. // Blocking call.
  63. RecastNavigationProviderRequestBus::EventResult(tiles, m_entityComponentIdPair.GetEntityId(),
  64. &RecastNavigationProviderRequests::CollectGeometry,
  65. m_configuration.m_tileSize, aznumeric_cast<float>(m_configuration.m_borderSize) * m_configuration.m_cellSize);
  66. RecastNavigationMeshNotificationBus::Event(m_entityComponentIdPair.GetEntityId(),
  67. &RecastNavigationMeshNotificationBus::Events::OnNavigationMeshBeganRecalculating, m_entityComponentIdPair.GetEntityId());
  68. {
  69. for (AZStd::shared_ptr<TileGeometry>& tile : tiles)
  70. {
  71. {
  72. NavMeshQuery::LockGuard lock(*m_navObject);
  73. // If a tile at the location already exists, remove it before updating the data.
  74. if (const dtTileRef tileRef = lock.GetNavMesh()->getTileRefAt(tile->m_tileX, tile->m_tileY, 0))
  75. {
  76. lock.GetNavMesh()->removeTile(tileRef, nullptr, nullptr);
  77. }
  78. }
  79. if (tile->IsEmpty())
  80. {
  81. continue;
  82. }
  83. // Given geometry create Recast tile structure.
  84. NavigationTileData navigationTileData = CreateNavigationTile(tile.get(),
  85. m_configuration, m_context.get());
  86. // A tile might have no geometry at all if no objects were found there.
  87. if (navigationTileData.IsValid())
  88. {
  89. AttachNavigationTileToMesh(navigationTileData);
  90. }
  91. }
  92. }
  93. RecastNavigationMeshNotificationBus::Event(m_entityComponentIdPair.GetEntityId(),
  94. &RecastNavigationMeshNotifications::OnNavigationMeshUpdated, m_entityComponentIdPair.GetEntityId());
  95. m_updateInProgress = false;
  96. return true;
  97. }
  98. bool RecastNavigationMeshComponentController::UpdateNavigationMeshAsync()
  99. {
  100. bool notInProgress = false;
  101. if (m_updateInProgress.compare_exchange_strong(notInProgress, true))
  102. {
  103. AZ_PROFILE_SCOPE(Navigation, "Navigation: UpdateNavigationMeshAsync");
  104. bool operationScheduled = false;
  105. RecastNavigationProviderRequestBus::EventResult(operationScheduled, m_entityComponentIdPair.GetEntityId(),
  106. &RecastNavigationProviderRequests::CollectGeometryAsync,
  107. m_configuration.m_tileSize, aznumeric_cast<float>(m_configuration.m_borderSize) * m_configuration.m_cellSize,
  108. [this](AZStd::shared_ptr<TileGeometry> tile)
  109. {
  110. OnTileProcessedEvent(tile);
  111. });
  112. if (!operationScheduled)
  113. {
  114. m_updateInProgress = false;
  115. return false;
  116. }
  117. return true;
  118. }
  119. return false;
  120. }
  121. AZStd::shared_ptr<NavMeshQuery> RecastNavigationMeshComponentController::GetNavigationObject()
  122. {
  123. return m_navObject;
  124. }
  125. void RecastNavigationMeshComponentController::Activate(const AZ::EntityComponentIdPair& entityComponentIdPair)
  126. {
  127. m_entityComponentIdPair = entityComponentIdPair;
  128. m_context = AZStd::make_unique<rcContext>();
  129. // It is safe to create the navigation mesh object now.
  130. // The actual navigation data will be passed at a later time.
  131. CreateNavigationMesh(m_entityComponentIdPair.GetEntityId());
  132. if (IsDebugDrawEnabled())
  133. {
  134. m_tickEvent.Enqueue(AZ::TimeMs{ 0 }, true);
  135. }
  136. RecastNavigationMeshRequestBus::Handler::BusConnect(m_entityComponentIdPair.GetEntityId());
  137. m_shouldProcessTiles = true;
  138. }
  139. void RecastNavigationMeshComponentController::Deactivate()
  140. {
  141. m_tickEvent.RemoveFromQueue();
  142. if (m_updateInProgress)
  143. {
  144. m_shouldProcessTiles = false;
  145. if (m_taskGraphEvent && m_taskGraphEvent->IsSignaled() == false)
  146. {
  147. // If the tasks are still in progress, wait until the task graph is finished.
  148. m_taskGraphEvent->Wait();
  149. }
  150. }
  151. m_context.reset();
  152. m_navObject.reset();
  153. m_taskGraphEvent.reset();
  154. m_updateInProgress = false;
  155. RecastNavigationMeshRequestBus::Handler::BusDisconnect();
  156. }
  157. void RecastNavigationMeshComponentController::SetConfiguration(const RecastNavigationMeshConfig& config)
  158. {
  159. m_configuration = config;
  160. }
  161. const RecastNavigationMeshConfig& RecastNavigationMeshComponentController::GetConfiguration() const
  162. {
  163. return m_configuration;
  164. }
  165. void RecastNavigationMeshComponentController::OnSendNotificationTick()
  166. {
  167. if (m_updateInProgress)
  168. {
  169. RecastNavigationMeshNotificationBus::Event(m_entityComponentIdPair.GetEntityId(),
  170. &RecastNavigationMeshNotifications::OnNavigationMeshUpdated, m_entityComponentIdPair.GetEntityId());
  171. m_updateInProgress = false;
  172. }
  173. }
  174. bool RecastNavigationMeshComponentController::IsDebugDrawEnabled() const
  175. {
  176. return cl_navmesh_debug || m_configuration.m_enableDebugDraw || m_configuration.m_enableEditorPreview;
  177. }
  178. void RecastNavigationMeshComponentController::OnDebugDrawTick()
  179. {
  180. if (IsDebugDrawEnabled())
  181. {
  182. NavMeshQuery::LockGuard lock(*m_navObject);
  183. if (lock.GetNavMesh())
  184. {
  185. AZ::Transform cameraTransform = AZ::Transform::CreateIdentity();
  186. Camera::ActiveCameraRequestBus::BroadcastResult(cameraTransform, &Camera::ActiveCameraRequestBus::Events::GetActiveCameraTransform);
  187. m_customDebugDraw.SetViewableAabb(AZ::Aabb::CreateCenterRadius(cameraTransform.GetTranslation(), cl_navmesh_debugRadius));
  188. duDebugDrawNavMesh(&m_customDebugDraw, *lock.GetNavMesh(), DU_DRAWNAVMESH_COLOR_TILES);
  189. }
  190. }
  191. }
  192. void RecastNavigationMeshComponentController::OnReceivedAllNewTiles()
  193. {
  194. ReceivedAllNewTilesImpl(m_configuration, m_sendNotificationEvent);
  195. }
  196. void RecastNavigationMeshComponentController::OnTileProcessedEvent(AZStd::shared_ptr<TileGeometry> tile)
  197. {
  198. if (tile)
  199. {
  200. if (m_shouldProcessTiles)
  201. {
  202. // Store tile data until we received all of them.
  203. AZStd::lock_guard lock(m_tileProcessingMutex);
  204. m_tilesToBeProcessed.push_back(tile);
  205. }
  206. }
  207. else
  208. {
  209. // The async operation to receive all tiled has finished. Kick off processing of received tiles on the main thread.
  210. m_receivedAllNewTilesEvent.Enqueue(AZ::TimeMs{ 0 });
  211. }
  212. }
  213. NavigationTileData RecastNavigationMeshComponentController::CreateNavigationTile(TileGeometry* geom,
  214. const RecastNavigationMeshConfig& meshConfig, rcContext* context)
  215. {
  216. AZ_PROFILE_SCOPE(Navigation, "Navigation: create tile");
  217. RecastProcessing recast;
  218. recast.m_vertices = geom->m_vertices.empty() ? nullptr : geom->m_vertices.front().m_xyz;
  219. recast.m_vertexCount = static_cast<int>(geom->m_vertices.size());
  220. recast.m_triangleData = geom->m_indices.empty() ? nullptr : &geom->m_indices[0];
  221. recast.m_triangleCount = static_cast<int>(geom->m_indices.size()) / 3;
  222. recast.m_context = context;
  223. // Step 1. Initialize build config.
  224. recast.InitializeMeshConfig(geom, meshConfig);
  225. // Step 2. Rasterize input polygon soup.
  226. if (!recast.RasterizeInputPolygonSoup())
  227. {
  228. return {};
  229. }
  230. // Step 3. Filter walkable surfaces.
  231. recast.FilterWalkableSurfaces(meshConfig);
  232. // Step 4. Partition walkable surface to simple regions.
  233. if (!recast.PartitionWalkableSurfaceToSimpleRegions())
  234. {
  235. return {};
  236. }
  237. // Step 5. Trace and simplify region contours.
  238. if (!recast.TraceAndSimplifyRegionContours())
  239. {
  240. return {};
  241. }
  242. // Step 6. Build polygons mesh from contours.
  243. if (!recast.BuildPolygonsMeshFromContours())
  244. {
  245. return {};
  246. }
  247. // Step 7. Create detail mesh which allows to access approximate height on each polygon.
  248. if (!recast.CreateDetailMesh())
  249. {
  250. return {};
  251. }
  252. // Step 8. Create Detour data from Recast poly mesh.
  253. return recast.CreateDetourData(geom, meshConfig);
  254. }
  255. RecastNavigationMeshComponentController::RecastNavigationMeshComponentController()
  256. : m_taskExecutor(bg_navmesh_threads)
  257. {
  258. }
  259. RecastNavigationMeshComponentController::RecastNavigationMeshComponentController(const RecastNavigationMeshConfig& config)
  260. : m_configuration(config)
  261. , m_taskExecutor(bg_navmesh_threads)
  262. {
  263. }
  264. bool RecastNavigationMeshComponentController::CreateNavigationMesh(AZ::EntityId meshEntityId)
  265. {
  266. AZ_PROFILE_SCOPE(Navigation, "Navigation: create mesh");
  267. RecastPointer<dtNavMesh> navMesh(dtAllocNavMesh());
  268. if (!navMesh)
  269. {
  270. AZ_Error("Navigation", false, "Could not create Detour navmesh");
  271. return false;
  272. }
  273. AZ::Aabb worldVolume = AZ::Aabb::CreateNull();
  274. dtNavMeshParams params = {};
  275. RecastNavigationProviderRequestBus::EventResult(worldVolume, meshEntityId, &RecastNavigationProviderRequests::GetWorldBounds);
  276. const RecastVector3 worldCenter = RecastVector3::CreateFromVector3SwapYZ(worldVolume.GetMin());
  277. rcVcopy(params.orig, worldCenter.m_xyz);
  278. RecastNavigationProviderRequestBus::EventResult(params.maxTiles, meshEntityId, &RecastNavigationProviderRequests::GetNumberOfTiles, m_configuration.m_tileSize);
  279. // in world units
  280. params.tileWidth = m_configuration.m_tileSize;
  281. params.tileHeight = m_configuration.m_tileSize;
  282. dtStatus status = navMesh->init(&params);
  283. if (dtStatusFailed(status))
  284. {
  285. AZ_Error("Navigation", false, "Could not init Detour navmesh");
  286. return false;
  287. }
  288. RecastPointer<dtNavMeshQuery> navQuery(dtAllocNavMeshQuery());
  289. status = navQuery->init(navMesh.get(), 2048);
  290. if (dtStatusFailed(status))
  291. {
  292. AZ_Error("Navigation", false, "Could not init Detour navmesh query");
  293. return false;
  294. }
  295. const AZStd::shared_ptr<NavMeshQuery> object = GetNavigationObject();
  296. if (object)
  297. {
  298. NavMeshQuery::LockGuard lock(*object);
  299. m_navObject = AZStd::make_shared<NavMeshQuery>(navMesh.release(), navQuery.release());
  300. }
  301. else
  302. {
  303. m_navObject = AZStd::make_shared<NavMeshQuery>(navMesh.release(), navQuery.release());
  304. }
  305. m_shouldProcessTiles = false;
  306. m_updateInProgress = false;
  307. return true;
  308. }
  309. bool RecastNavigationMeshComponentController::AttachNavigationTileToMesh(NavigationTileData& navigationTileData)
  310. {
  311. AZ_PROFILE_SCOPE(Navigation, "Navigation: addTile");
  312. NavMeshQuery::LockGuard lock(*m_navObject);
  313. dtTileRef tileRef = 0;
  314. const dtStatus status = lock.GetNavMesh()->addTile(
  315. navigationTileData.m_data, navigationTileData.m_size,
  316. DT_TILE_FREE_DATA, 0, &tileRef);
  317. if (dtStatusFailed(status))
  318. {
  319. dtFree(navigationTileData.m_data);
  320. return false;
  321. }
  322. return true;
  323. }
  324. void RecastNavigationMeshComponentController::ReceivedAllNewTilesImpl(const RecastNavigationMeshConfig& config, AZ::ScheduledEvent& sendNotificationEvent)
  325. {
  326. if (m_shouldProcessTiles && (!m_taskGraphEvent || m_taskGraphEvent->IsSignaled()))
  327. {
  328. AZ_PROFILE_SCOPE(Navigation, "Navigation: OnReceivedAllNewTiles");
  329. m_taskGraphEvent = AZStd::make_unique<AZ::TaskGraphEvent>("RecastNavigation Tile Processing Wait");
  330. m_taskGraph.Reset();
  331. AZStd::vector<AZ::TaskToken> tileTaskTokens;
  332. AZStd::vector<AZStd::shared_ptr<TileGeometry>> tilesToBeProcessed;
  333. {
  334. AZStd::lock_guard lock(m_tileProcessingMutex);
  335. m_tilesToBeProcessed.swap(tilesToBeProcessed);
  336. }
  337. // Create tasks for each tile and a finish task.
  338. for (AZStd::shared_ptr<TileGeometry> tile : tilesToBeProcessed)
  339. {
  340. AZ::TaskToken token = m_taskGraph.AddTask(
  341. m_taskDescriptor, [this, tile, &config]()
  342. {
  343. if (!m_shouldProcessTiles)
  344. {
  345. return;
  346. }
  347. AZ_PROFILE_SCOPE(Navigation, "Navigation: task - computing tile");
  348. NavigationTileData navigationTileData = CreateNavigationTile(tile.get(),
  349. config, m_context.get());
  350. {
  351. NavMeshQuery::LockGuard lock(*m_navObject);
  352. if (const dtTileRef tileRef = lock.GetNavMesh()->getTileRefAt(tile->m_tileX, tile->m_tileY, 0))
  353. {
  354. lock.GetNavMesh()->removeTile(tileRef, nullptr, nullptr);
  355. }
  356. }
  357. if (navigationTileData.IsValid())
  358. {
  359. AZ_PROFILE_SCOPE(Navigation, "Navigation: UpdateNavigationMeshAsync - tile callback");
  360. if (navigationTileData.IsValid())
  361. {
  362. AttachNavigationTileToMesh(navigationTileData);
  363. }
  364. }
  365. });
  366. tileTaskTokens.push_back(AZStd::move(token));
  367. }
  368. AZ::TaskToken finishToken = m_taskGraph.AddTask(
  369. m_taskDescriptor, [&sendNotificationEvent]()
  370. {
  371. sendNotificationEvent.Enqueue(AZ::TimeMs{ 0 });
  372. });
  373. for (AZ::TaskToken& task : tileTaskTokens)
  374. {
  375. task.Precedes(finishToken);
  376. }
  377. m_taskGraph.SubmitOnExecutor(m_taskExecutor, m_taskGraphEvent.get());
  378. RecastNavigationMeshNotificationBus::Event(m_entityComponentIdPair.GetEntityId(),
  379. &RecastNavigationMeshNotificationBus::Events::OnNavigationMeshBeganRecalculating, m_entityComponentIdPair.GetEntityId());
  380. }
  381. }
  382. } // namespace RecastNavigation