- Home /
Procedurally generated NavMesh.
I have a procedurally generated scene, where, as the user moves, new tiles are generated with meshes on them.
I want to add a NavMesh to the scene that is also procedurally generated. So when each new tile is loaded, I want the NavMesh to be updated accordingly (note all of this has to be done at runtime, no way around it).
So far I have two solutions but none of them works as desired.
One thing I can do is run:
NavMeshBuildSettings settings = NavMesh.GetSettingsByIndex(0);
List<NavMeshBuildSource> buildSources = new List<NavMeshBuildSource>();
NavMeshBuilder.CollectSources(null, mask, NavMeshCollectGeometry.RenderMeshes, 0, new List<NavMeshBuildMarkup>(), buildSources);
NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, buildSources, new Bounds(Vector3.zero, Vector3.one * 10000), Vector3.zero, Quaternion.identity);
NavMesh.AddNavMeshData(data);
If I run this after my terrain has been loaded I get the correct result, a single NavMesh across the entire terrain.
However, when I try to load it tile-by-tile:
List<NavMeshBuildSource> sources = new List<NavMeshBuildSource>();
NavMeshBuildSource s = new NavMeshBuildSource
{
transform = tile.transform.localToWorldMatrix,
shape = NavMeshBuildSourceShape.Mesh,
sourceObject = tile.MeshFilter.mesh
};
sources.Add(s);
NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, sources, new Bounds(Vector3.zero, Vector3.one * 1000), Vector3.zero, Quaternion.identity);
NavMesh.AddNavMeshData(data);
I get separate NavMeshes for each tile, with a slight gap between them. Is there anything i'm missing here? Optimally I would like to add a part of the NavMesh when a tile is loaded and remove that part when it's unloaded.
I only ever used Aron Granber's Astar Pathfinding Project for this, but anyway it's possible that you would want to take a look at $$anonymous$$esh.Combine$$anonymous$$eshes.
This might just work, however i feel that it's somewhat inefficient. But I do only have 2 triangles per tile, and ~ 50 tiles loaded at the same time, so recalculating everything might not be a bad idea... i'll try it!
Your answer

Follow this Question
Related Questions
Proceduraly Generated Levels vs NPC Navigation 1 Answer
Procedural NavMesh Instantiating 0 Answers
NavMesh with geometry mesh generated at runtime 1 Answer
Dynamic NavMesh Creation at Runtime? 1 Answer
Procedural pathfinding 0 Answers