Updating navigation mesh mid-game

Godot Version

4.3

Question

` Hello, I have been trying to update my navigation mesh mid-game. I have tried re-baking it, but it takes way to much resources to do effectively, causing a dip in frames. I have tried using avoidance properties, but it still has issues with advanced movements (for example, moving around L shapes).

What I am working on is a rts style project. When someone builds a building, the nav mesh needs to re-bake to account for that, so units can path around it.`

In general rebaking the navmesh is what you need to do as nothing else affects the pathfinding and can update paths for e.g. a placed building.

If it takes way too much resources you need to figure out what is causing it and how to optimize. There are many different things in Godot that can affect the overall performance when updating navmesh at runtime, not all of them are actually directly related to the navmesh baking.

  • Parsing the geometry
    If you update the navmesh you are likely parsing Nodes for their geometry. Some nodes can be incredibly slow to parse at runtime. Prefer to only use simple shaped static collision objects. Do NEVER parse visual meshes like MeshInstance3D for runtime as they will destroy performance due to GPU stalling to get to the geometry data. Due to nodes the parsing needs to run on the main thread so if the nodes are too many or too slow to parse this will cause frame rate issues at runtime.

  • Baking the navmesh
    The biggest performance cost is how much geometry you add (both complexity and actual size) and the used cell_size and cell_height as that defines the voxel grid for the baking. If it is just too much for a single bake operation you need to either reduce the geometry complexity and cell size or you need to start to chunk your world (recommended anyway). Since the baking by default runs on a thread this should not be that framerate noticeable except that there is a delay for the update.

  • Updating the navigation map
    How fast the update of the map is depends on how complex the navmesh is, aka how many polygons it has. On larger worlds you need to disable the helper function edge connection margin as that has a very high performance cost per navmesh edge. At that point navmeshes will only connect when their edges perfectly overlap. If you upgrade to Godot 4.4 this will happen async on a background thread, in Godot 4.3 this happens on the main thread.

1 Like