navigation_optimizing_performance.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. .. _doc_navigation_optimizing_performance:
  2. Optimizing Navigation Performance
  3. =================================
  4. .. image:: img/nav_optimization.webp
  5. Common Navigation related performance problems can be categorized into the following topics:
  6. - Performance problems with parsing scene tree nodes for navigation mesh baking.
  7. - Performance problems with baking the actual navigation mesh.
  8. - Performance problems with NavigationAgent path queries.
  9. - Performance problems with the actual path search.
  10. - Performance problems with synchronizing the navigation map.
  11. In the following sections information can be found on how to identify and fix or at least mitigate their impact on framerates.
  12. Performance problems with parsing scene tree nodes
  13. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. .. tip::
  15. Prefer using simple shapes with as few edges as possible e.g. nothing rounded like a circle, sphere or torus.
  16. Prefer using physics collision shapes over complex visual meshes as source geometry as meshes need to be copied from the GPU and are commonly much more detailed than necessary.
  17. In general avoid using very complex geometry as source geometry for baking navigation meshes.
  18. E.g. never use a very detailed visual mesh, as parsing its shape to data arrays and voxelizing it for the navigation mesh baking will take a long time for no real quality gain on the final navigation mesh.
  19. Instead, use a very simplified level of detail version of a shape. Even better, use very primitive shapes like boxes and rectangles that only roughly cover the same geometry but still yield a baked result good enough for pathfinding.
  20. Prefer using simple physics collision shapes over visual meshes, as the source geometry for baking navigation meshes.
  21. Physics shapes are by default very limited and optimized shapes that are easy and quick to parse. A visual mesh on the other hand can range from simple to complex.
  22. On top, to gain access to visual mesh data the parser needs to request the mesh data arrays from the RenderingServer as visual mesh data is stored directly on the GPU and is not cached on the CPU.
  23. This requires locking the RenderingServer thread and can severely impact framerate at runtime while the rendering runs multi-threaded.
  24. If the rendering runs single-threaded, the framerate impact might be even worse and the mesh parsing might freeze the entire game for a few seconds on complex meshes.
  25. Performance problems with navigation mesh baking
  26. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. .. tip::
  28. At runtime, always prefer to use a background thread for baking navigation meshes.
  29. Increase NavigationMesh ``cell_size`` and ``cell_height`` to create less voxels.
  30. Change the ``SamplePartitionType`` from watershed to monotone or layers to gain baking performance.
  31. .. warning::
  32. NEVER scale source geometry with nodes to avoid precision errors. Most scale applies only visually and shapes that are very large at their base scale require still a lot of extra processing even while downscaled.
  33. Baking navigation meshes at runtime should always be done in a background thread if possible. Even small sized navigation meshes can take far longer to bake than what is possible to squeeze into a single frame, at least if the framerate should stay at a bearable level.
  34. Complexity of source geometry data parsed from scene tree nodes has big impact on baking performance as everything needs to be mapped to a grid / voxels.
  35. For runtime baking performance the NavigationMesh cell size and cell height should be set as high as possible without causing navigation mesh quality problems for a game.
  36. If cell size or cell height is set too low the baking is forced to create an excessive amount of voxels to process the source geometry.
  37. If the source geometry spans over a very large game world it is even possible that the baking process runs out off memory in the middle and crashes the game.
  38. The partition type can also be lowered depending on how complex the games source geometry is to gain some performance.
  39. E.g. games with mostly flat surfaces with blocky geometry can get away with the monotone or layers mode that are a lot faster to bake (e.g. because they require no distance field pass).
  40. Never scale source geometry with nodes. Not only can it result in a lot of precision errors with wrongly matched vertices and edges but also some scaling only exists as visuals and not in the actual parsed data.
  41. E.g. if a mesh is downscaled visually in the Editor, e.g. the scale set to 0.001 on a MeshInstance, the mesh still requires a gigantic and very complex voxel grid to be processed for the baking.
  42. Performance problems with NavigationAgent path queries
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. .. tip::
  45. Avoid unnecessary path resets and queries every frame in NavigationAgent scripts.
  46. Avoid updating all NavigationAgent paths in the same frame.
  47. Logical errors and wasteful operations in the custom NavigationAgent scripts are very common causes of performance issues, e.g. watch out for resetting the path every single frame.
  48. By default NavigationAgents are optimized to only query new paths when the target position changes, the navigation map changes or they are forced too far away from the desired path distance.
  49. E.g. when AI should move to the player, the target position should not be set to the player position every single frame as this queries a new path every frame.
  50. Instead, the distance from the current target position to the player position should be compared and only when the player has moved too far away a new target position should be set.
  51. Do not check beforehand if a target position is reachable every frame. What looks like an innocent check is the equivalent of an expensive path query behind the scene.
  52. If the plan is to request a new path anyway should the position be reachable, a path should be queried directly.
  53. By looking at the last position of the returned path and if that position is in a "reachable" distance to the checked position it answers the "is this position reachable?" question.
  54. This avoids doing the equivalent of two full path queries every frame for the same NavigationAgent.
  55. Divide the total number of NavigationAgents into update groups or use random timers so that they do not all request new paths in the same frame.
  56. Performance problems with the actual path search
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. .. tip::
  59. Optimize overdetailed navigation meshes by reducing the amount of polygons and edges.
  60. The cost of the actual path search correlates directly with the amount of navigation mesh polygons and edges and not the real size of a game world.
  61. If a giant game world uses very optimized navigation meshes with only few polygons that cover large areas, performance should be acceptable.
  62. If the game world is splintered into very small navigation meshes that each have tiny polygons (like for TileMaps) pathfinding performance will be reduced.
  63. A common problem is a sudden performance drop when a target position is not reachable in a path query.
  64. This performance drop is "normal" and the result of a too large, too unoptimized navigation mesh with way to much polygons and edges to search through.
  65. In normal path searches where the target position can be reached quickly the pathfinding will do an early exit as soon as the position is reached which can hide this lack of optimization for a while.
  66. If the target position can not be reached the pathfinding has to do a far longer search through the available polygons to confirm that the position is absolutely not reachable.
  67. Performance problems with navigation map synchronization
  68. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. .. tip::
  70. Merge navigation meshes polygons by vertex instead of by edge connection wherever possible.
  71. When changes are made to e.g. navigation meshes or navigation regions, the NavigationServer needs to synchronize the navigation map.
  72. Depending on the complexity of navigation meshes, this can take a significant amount of time which may impact the framerate.
  73. The NavigationServer merges navigation meshes either by vertex or by edge connection.
  74. The merge by vertex happens when the two vertex of two different edges land in the same map grid cells. This is a rather quick and low-cost operation.
  75. The merge by edge connection happens in a second pass for all still unmerged edges. All the free edges are checked for possible edge connections by both distance and angle which is rather costly.
  76. So apart from the general rule to have as few polygon edges as possible, as many edges as possible should be merged by vertex upfront so only a few edges are left for the more costly edge connection calculation.
  77. The debug Navigation PerformanceMonitor can be used to get statistics on how many polygons and edges are available and how many of them are unmerged or not merged by vertex.
  78. If the ratio between vertex merged and edge connections is way off (vertex should be significantly higher) the navigation meshes are properly created or placed very inefficient.