From 86e6d3dac8bd542b04148521d81bde9d5d3a0695 Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 7 Aug 2021 12:03:21 +0200 Subject: [PATCH] Do not cache navmesh when only object transformation is changed This saves cache capacity when a scene contains objects contantly transforming by scripts and causing changes in navmesh. The probability to get cache hit for such states is almost zero because even a constant change in a single float value may give up to 2^32 different states. --- components/detournavigator/asyncnavmeshupdater.cpp | 11 ++++++++++- components/detournavigator/makenavmesh.cpp | 6 +++++- components/detournavigator/makenavmesh.hpp | 8 +++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/components/detournavigator/asyncnavmeshupdater.cpp b/components/detournavigator/asyncnavmeshupdater.cpp index 11de607456..a0c9cba613 100644 --- a/components/detournavigator/asyncnavmeshupdater.cpp +++ b/components/detournavigator/asyncnavmeshupdater.cpp @@ -18,6 +18,8 @@ namespace { using DetourNavigator::ChangeType; using DetourNavigator::TilePosition; + using DetourNavigator::UpdateType; + using DetourNavigator::ChangeType; int getManhattanDistance(const TilePosition& lhs, const TilePosition& rhs) { @@ -35,6 +37,13 @@ namespace result = std::min(result, getManhattanDistance(position, tile)); return result; } + + UpdateType getUpdateType(ChangeType changeType) noexcept + { + if (changeType == ChangeType::update) + return UpdateType::Temporary; + return UpdateType::Persistent; + } } namespace DetourNavigator @@ -282,7 +291,7 @@ namespace DetourNavigator const auto offMeshConnections = mOffMeshConnectionsManager.get().get(job.mChangedTile); const auto status = updateNavMesh(job.mAgentHalfExtents, recastMesh.get(), job.mChangedTile, playerTile, - offMeshConnections, mSettings, navMeshCacheItem, mNavMeshTilesCache); + offMeshConnections, mSettings, navMeshCacheItem, mNavMeshTilesCache, getUpdateType(job.mChangeType)); if (recastMesh != nullptr) { diff --git a/components/detournavigator/makenavmesh.cpp b/components/detournavigator/makenavmesh.cpp index 246f9d85b6..892f9c2dd2 100644 --- a/components/detournavigator/makenavmesh.cpp +++ b/components/detournavigator/makenavmesh.cpp @@ -506,7 +506,7 @@ namespace DetourNavigator UpdateNavMeshStatus updateNavMesh(const osg::Vec3f& agentHalfExtents, const RecastMesh* recastMesh, const TilePosition& changedTile, const TilePosition& playerTile, const std::vector& offMeshConnections, const Settings& settings, - const SharedNavMeshCacheItem& navMeshCacheItem, NavMeshTilesCache& navMeshTilesCache) + const SharedNavMeshCacheItem& navMeshCacheItem, NavMeshTilesCache& navMeshTilesCache, UpdateType updateType) { Log(Debug::Debug) << std::fixed << std::setprecision(2) << "Update NavMesh with multiple tiles:" << @@ -562,6 +562,10 @@ namespace DetourNavigator return navMeshCacheItem->lock()->removeTile(changedTile); } + if (updateType == UpdateType::Temporary) + return navMeshCacheItem->lock()->updateTile(changedTile, NavMeshTilesCache::Value(), + makeNavMeshTileData(*prepared, offMeshConnections, agentHalfExtents, changedTile, settings)); + cachedNavMeshData = navMeshTilesCache.set(agentHalfExtents, changedTile, *recastMesh, std::move(prepared)); if (!cachedNavMeshData) diff --git a/components/detournavigator/makenavmesh.hpp b/components/detournavigator/makenavmesh.hpp index 3e07341106..5b4169374b 100644 --- a/components/detournavigator/makenavmesh.hpp +++ b/components/detournavigator/makenavmesh.hpp @@ -47,10 +47,16 @@ namespace DetourNavigator NavMeshPtr makeEmptyNavMesh(const Settings& settings); + enum class UpdateType + { + Persistent, + Temporary + }; + UpdateNavMeshStatus updateNavMesh(const osg::Vec3f& agentHalfExtents, const RecastMesh* recastMesh, const TilePosition& changedTile, const TilePosition& playerTile, const std::vector& offMeshConnections, const Settings& settings, - const SharedNavMeshCacheItem& navMeshCacheItem, NavMeshTilesCache& navMeshTilesCache); + const SharedNavMeshCacheItem& navMeshCacheItem, NavMeshTilesCache& navMeshTilesCache, UpdateType updateType); } #endif