Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Utilizzo di mesh di navigazione

../../_images/nav_meshes.webp

Le versioni 2D e 3D della mesh di navigazione sono disponibili rispettivamente come NavigationPolygon e NavigationMesh.

Nota

Una mesh di navigazione descrive soltanto un'area percorribile per la posizione centrale di un agente. Qualsiasi valore di raggio che un agente possa avere è ignorato. Se vuoi che la ricerca del percorso tenga conto delle dimensioni (e delle collisioni) di un agente, bisogna rimpicciolire adeguatamente la mesh di navigazione.

La navigazione funziona indipendentemente dalle altre parti del motore, come il rendering o la fisica. Le mesh di navigazione sono le uniche cose considerate durante la ricerca del percorso; ad esempio, gli elementi visivi e le forme di collisione sono completamente ignorati dal sistema di navigazione. Se vuoi tenere conto di altri dati (come ad esempio gli elementi visivi) durante la ricerca del percorso, bisogna adattare adeguatamente le mesh di navigazione. La procedura per tenere conto delle restrizioni nelle mesh di navigazione è comunemente detto "navigation mesh baking".

Confronto tra poligoni di navigazione convessi e concavi

Una mesh di navigazione descrive una superficie su cui un agente può posizionarsi in sicurezza, con il suo centro confrontato con le forme fisiche che descrivono i limiti esterni di collisione.

Se riscontri problemi di clipping o collisione mentre si seguono i percorsi di navigazione, ricorda sempre che devi dire cosa vuoi fare al sistema di navigazione tramite un'apposita mesh di navigazione. Il sistema di navigazione, di per sé, non saprà mai "questa è una forma o una mesh visiva di collisione per alberi/rocce/muri" perché sa solo che "qui mi è stato detto che posso percorrere il percorso in sicurezza perché si trova su una mesh di navigazione".

La preparazione delle mesh di navigazione si può effettuare attraverso un NavigationRegion2D o un NavigationRegion3D, oppure direttamente attraverso le API del NavigationServer2D e del NavigationServer3D.

Preparazione di una mesh di navigazione con una NavigationRegion

Passaggi per preparare una mesh di navigazione

Baking a navigation mesh with agent radius offset from geometry.

La preparazione delle mesh di navigazione è reso più accessibile grazie al nodo NavigationRegion. Quando si utilizza un nodo NavigationRegion per la preparazione, le singole fasi di analisi, preparazione e aggiornamento della regione vengono combinate in un'unica funzione.

The nodes are available in 2D and 3D as NavigationRegion2D and NavigationRegion3D respectively.

Suggerimento

The navigation mesh source_geometry_mode can be switched to parse specific node group names so nodes that should be baked can be placed anywhere in the scene.

When a NavigationRegion2D node is selected in the Editor, bake options as well as polygon draw tools appear in the top bar of the Editor.

../../_images/nav_region_baking_01.webp

Affinché la regione funzioni, è necessario aggiungere una risorsa NavigationPolygon.

The properties to parse and bake a navigation mesh are then part of the used resource and can be found in the resource Inspector.

../../_images/nav_region_baking_02.webp

Il risultato dell'analisi della geometria sorgente può essere influenzato dalle seguenti proprietà.

  • The parsed_geometry_type that filters if visual objects or physics objects or both should be parsed from the SceneTree. For more details on what objects are parsed and how, see the section about parsing source geometry below.

  • The collision_mask filters which physics collision objects are included when the parsed_geometry_type includes static colliders.

  • The source_geometry_mode that defines on which node(s) to start the parsing, and how to traverse the SceneTree.

  • The source_geometry_group_name is used when only a certain node group should be parsed. Depends on the selected source_geometry_mode.

Aggiunta la geometria della sorgente, il risultato della preparazione può essere controllato con le seguenti proprietà.

  • The cell_size sets the rasterization grid size and should match the navigation map size.

  • The agent_radius shrinks the baked navigation mesh to have enough margin for the agent (collision) size.

The NavigationRegion2D baking can also be used at runtime with scripts.

var on_thread: bool = true
bake_navigation_polygon(on_thread)

To quickly test the 2D baking with default settings:

  • Aggiungi un NavigationRegion2D.

  • Aggiungi una risorsa NavigationPolygon al NavigationRegion2D.

  • Add a Polygon2D below the NavigationRegion2D.

  • Draw 1 NavigationPolygon outline with the selected NavigationRegion2D draw tool.

  • Draw 1 Polygon2D outline inside the NavigationPolygon outline with the selected Polygon2D draw tool.

  • Hit the Editor bake button and a navigation mesh should appear.

../../_images/nav_region_baking_01.webp ../../_images/nav_mesh_mini_2d.webp

Baking a navigation mesh with the NavigationServer

The NavigationServer2D and NavigationServer3D have API functions to call each step of the navigation mesh baking process individually.

  • parse_source_geometry_data() can be used to parse source geometry to a reusable and serializable resource.

  • bake_from_source_geometry_data() can be used to bake a navigation mesh from already parsed data e.g. to avoid runtime performance issues with (redundant) parsing.

  • bake_from_source_geometry_data_async() is the same but bakes the navigation mesh deferred with threads, not blocking the main thread.

Compared to a NavigationRegion, the NavigationServer offers finer control over the navigation mesh baking process. In turn it is more complex to use but also provides more advanced options.

Some other advantages of the NavigationServer over a NavigationRegion are:

  • The server can parse source geometry without baking, e.g. to cache it for later use.

  • The server allows selecting the root node at which to start the source geometry parsing manually.

  • The server can accept and bake from procedurally generated source geometry data.

  • The server can bake multiple navigation meshes in sequence while (re)using the same source geometry data.

To bake navigation meshes with the NavigationServer, source geometry is required. Source geometry is geometry data that should be considered in a navigation mesh baking process. Both navigation meshes for 2D and 3D are created by baking them from source geometry.

2D and 3D versions of the source geometry resources are available as NavigationMeshSourceGeometryData2D and NavigationMeshSourceGeometryData3D respectively.

Source geometry can be geometry parsed from visual meshes, from physics collision, or procedural created arrays of data, like outlines (2D) and triangle faces (3D). For convenience, source geometry is commonly parsed directly from node setups in the SceneTree. For runtime navigation mesh (re)bakes, be aware that the geometry parsing always happens on the main thread.

Nota

The SceneTree is not thread-safe. Parsing source geometry from the SceneTree can only be done on the main thread.

Avvertimento

I dati provenienti da mesh e poligoni visivi devono essere ricevuti dalla GPU, di conseguenza bloccando il RenderingServer. Per la (ri)preparazione in fase di esecuzione, è preferibile utilizzare forme fisiche come geometria sorgente analizzata.

La geometria sorgente è memorizzata all'interno delle risorse, così che la geometria creata si possa riutilizzare per più preparazioni. Ad esempio, per preparare più mesh di navigazione per diverse dimensioni di agenti partendo dalla stessa geometria sorgente. Ciò consente anche di salvare la geometria sorgente su disco, così da poterla caricare in seguito, ad esempio per evitare l'impatto di doverla rianalizzare in fase di esecuzione.

The geometry data should be in general kept very simple. As many edges as are required but as few as possible. Especially in 2D duplicated and nested geometry should be avoided as it forces polygon hole calculation that can result in flipped polygons. An example for nested geometry would be a smaller StaticBody2D shape placed completely inside the bounds of another StaticBody2D shape.

Preparare blocchi di mesh di navigazione per grandi mondi

Creazione di blocchi di mesh di navigazione

Creazione e aggiornamento dei singoli blocchi della mesh di navigazione in fase di esecuzione.

Vedi anche

È possibile osservare la preparazione dei blocchi di mesh di navigazione in azione nei progetti demo Navigation Mesh Chunks 2D e Navigation Mesh Chunks 3D.

To avoid misaligned edges between different region chunks the navigation meshes have two important properties for the navigation mesh baking process. The baking bound and the border size. Together they can be used to ensure perfectly aligned edges between region chunks.

Navigation mesh chunk with bake bound and border size

Navigation mesh chunk baked with bake bound or baked with additional border size.

Il confine di preparazione, che è un Rect2 allineato agli assi per il 2D e un AABB per il 3D, limita la geometria sorgente utilizzata, scartando tutta la geometria che si trova fuori dai limiti.

Le proprietà baking_rect e baking_rect_offset del NavigationPolygon si possono usare per creare e posizionare il confine di preparazione 2D.

Le proprietà filter_baking_aabb e filter_baking_aabb_offset del NavigationMesh si possono usare per creare e posizionare il confine di preparazione 3D.

Anche impostando solo il confine di preparazione, esiste ancora un altro problema. La mesh di navigazione risultante sarà inevitabilmente influenzata da offset necessari come agent_radius, che non fanno allineare correttamente i bordi.

Blocchi di mesh di navigazione con distacchi

Navigation mesh chunks with noticeable gaps due to baked agent radius offset.

È qui che entra in gioco la proprietà border_size per la mesh di navigazione. La dimensione del bordo è un margine interno rispetto al confine di preparazione. La caratteristica importante della dimensione del bordo è che non è influenzata dalla maggior parte degli offset e delle post-elaborazioni, come agent_radius.

Instead of discarding source geometry, the border size discards parts of the final surface of the baked navigation mesh. If the baking bound is large enough the border size can remove the problematic surface parts so that only the intended chunk size is left.

Blocchi di mesh di navigazione senza distacchi

Navigation mesh chunks with aligned edges and without gaps.

Nota

The baking bounds need to be large enough to include a reasonable amount of source geometry from all the neighboring chunks.

Avvertimento

In 3D the functionality of the border size is limited to the xz-axis.