Hi, I had a similar problem.
While I’m still looking for a more efficient solution since I have to generate the navmesh for the whole map due to using user created maps and an AI that may need to pathfind through the whole map depending on what the user wants out of their custom content, this should work well for you.
This Channel/Video has a lot of videos on NavMeshBuilder and generating a navmesh at runtime. The good thing about this method is you can run it asynchronously, use unity jobs to speed the generation up if you have multiple cores, and most importantly, you can control what gets sent to the generator. For your use case, you may want to load the navmesh async around the player and update it outwards depending on your needs.
For your terrain, I suggest splitting it up into sections and only generating a navmesh for the section you are in or limiting the navmesh to a box around your character like in the linked video.
When it came to my version of unity, I had to write Navmesh.CalculateWorldBounds since it was marked as private and not accessible for some reason.
Bounds CalculateWorldBounds(List<NavMeshBuildSource> sources)
{
// Use the unscaled matrix for the NavMeshSurface
Matrix4x4 worldToLocal = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
worldToLocal = worldToLocal.inverse;
var result = new Bounds();
foreach (var src in sources)
{
switch (src.shape)
{
case NavMeshBuildSourceShape.Mesh:
{
var m = src.sourceObject as Mesh;
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, m.bounds));
break;
}
case NavMeshBuildSourceShape.Terrain:
{
var t = src.sourceObject as TerrainData;
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(0.5f * t.size, t.size)));
break;
}
case NavMeshBuildSourceShape.Box:
case NavMeshBuildSourceShape.Sphere:
case NavMeshBuildSourceShape.Capsule:
case NavMeshBuildSourceShape.ModifierBox:
result.Encapsulate(GetWorldBounds(worldToLocal * src.transform, new Bounds(Vector3.zero, src.size)));
break;
}
}
// Inflate the bounds a bit to avoid clipping co-planar sources
result.Expand(0.1f);
return result;
}
private void ResetSources()
{
// Clear previous sources and bounds
_navMeshSources.Clear();
_navMeshData.Clear();
_navMeshBounds = new Bounds(Vector3.zero, Vector3.zero);
// Create sources and bounds
var mask = PhysicsLayer.GetMask(PhysicsLayer.MapObjectEntities);
List<NavMeshBuildMarkup> modifiers = new List<NavMeshBuildMarkup>();
// Collect sources of physics colliders, exclude components with NavMeshObstacles
NavMeshBuilder.CollectSources(null, mask, NavMeshCollectGeometry.PhysicsColliders, 0, modifiers, _navMeshSources);
_navMeshBounds = CalculateWorldBounds(_navMeshSources);
_navMeshBounds.size = Vector3.Min(_navMeshBounds.size, new Vector3(15000, 15000, 15000));
}
/// <summary>
/// Use NavMeshBuilder to create a navmesh surface for the given agent id.
/// This should be called asynchronously and sync progress back to the main thread.
/// </summary>
/// <param name="agentID">An integer representing the agent.</param>
private AsyncOperation CreateNavMeshSurfaceAsyncOperation(int agentID, List<NavMeshBuildSource> sources, Bounds bounds)
{
// Create a new navmeshsurface object
NavMeshData data = new NavMeshData();
NavMeshBuildSettings settings = NavMesh.GetSettingsByID(agentID);
settings.maxJobWorkers = 3;
settings.overrideTileSize = true;
settings.tileSize = 256;
settings.overrideVoxelSize = true;
settings.voxelSize = 4f;
settings.minRegionArea = 100;
settings.buildHeightMesh = true;
_navMeshData.Add(agentID, data);
NavMesh.AddNavMeshData(data);
return NavMeshBuilder.UpdateNavMeshDataAsync(data, settings, sources, bounds);
}
And the code is called here, my use of it is not async as I wait for the navmesh to be done before loading into the map, but it can be done whenever.
if (MapManager.NeedsNavMeshUpdate || _hasNavMeshData == false)
{
NavMesh.RemoveAllNavMeshData();
_hasNavMeshData = false;
if (PhotonNetwork.IsMasterClient && SettingsManager.InGameCurrent.Titan.TitanSmartMovement.Value && gamemodeNeedsNav)
{
ResetSources();
List<int> agentIDs = Util.GetAllTitanAgentIds();
List<AsyncOperation> operations = new List<AsyncOperation>();
foreach (int agentID in agentIDs)
{
AsyncOperation createOp = CreateNavMeshSurfaceAsyncOperation(agentID, _navMeshSources, _navMeshBounds);
operations.Add(createOp);
}
// Check progress of the operations and use the min progress
while (operations.Count > 0)
{
float minProgress = 1f;
for (int i = 0; i < operations.Count; i++)
{
if (operations[i].isDone)
{
operations.RemoveAt(i);
i--;
}
else
minProgress = Mathf.Min(minProgress, operations[i].progress);
}
UIManager.LoadingMenu.UpdateLoading(0.75f + 0.25f * minProgress);
yield return new WaitForEndOfFrame();
}
_hasNavMeshData = true;
}
}