Search Unity

Question How to visualize NavMeshData correctly in SceneView?

Discussion in 'Navigation' started by Enderlook, May 10, 2023.

  1. Enderlook

    Enderlook

    Joined:
    Dec 4, 2018
    Posts:
    50
    We're writing our own implementation of the `NavMeshSurface` component (still using the underlying Unity AI) which better fits our game requirements, and we are having problems visualizing the `NavMeshData` in the editor.

    In order to build the nav mesh data we do something like:
    Code (CSharp):
    1. NavMeshBuildSettings settings = NavMesh.GetSettingsByID(agentSetting.AgentTypeId);
    2. NavMeshBuildDebugSettings debug = settings.debug;
    3. debug.flags = NavMeshBuildDebugFlags.All;
    4. settings.debug = debug;
    5. NavMeshData navMeshData = oldData;
    6. if (navMeshData == null)
    7. {  
    8.     navMeshData = UnityEngine.AI.NavMeshBuilder.BuildNavMeshData(settings, sources, surfaceBounds, position, rotation);
    9.     // Save file as asset...
    10. }
    11. else
    12. {
    13.     UnityEngine.AI.NavMeshBuilder.UpdateNavMeshData(navMeshData, settings, sources, surfaceBounds);
    14. }
    15. EditorUtility.SetDirty(navMeshData);
    16. AssetDatabase.Refresh();
    17.  
    And to visualize it we do:
    Code (CSharp):
    1.  
    2. private void OnDrawGizmosSelected()
    3. {
    4.     Matrix4x4 matrix = Gizmos.matrix;
    5.     Transform transform = this.transform;
    6.     Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
    7.     Gizmos.color = Color.grey;  
    8.     NavMeshData navMeshData = this.navMeshData;
    9.     if (navMeshData != null)
    10.     {
    11.         NavMeshEditorHelpers.DrawBuildDebug(navMeshData, NavMeshBuildDebugFlags.Regions);
    12.         Bounds bounds = navMeshData.sourceBounds;
    13.         Gizmos.DrawWireCube(bounds.center, bounds.size);
    14.     }
    15.     Gizmos.matrix = matrix;
    16. }
    17.  
    But:
    1) The form in which data is visualized in the scene is different to how Unity `NavMeshSurface` is seen. Thought this is not very important.
    2) Data is only visualized in the same place it was baked. That is, if I bake in a prefab, it's only shown in the prefab view. If I place the prefab in a scene, it's not shown in the scene view. This is very problematic for us, as in our workflow, the levels are separated into rooms, and each room has its own navigation data and is a prefab. So we can't see the navigation data in the scene.

    What am I missing? Is there another way to visualize the data?
     
  2. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    250
    Heres how I visualize a navmesh

    Code (CSharp):
    1. NavMeshTriangulation triangulation = NavMesh.CalculateTriangulation();
    2.  
    3. GL.Begin(GL.TRIANGLES);
    4.             for (int i = 0; i < triangulation.indices.Length; i += 3)
    5.             {
    6.                 var i1 = triangulation.indices[i];
    7.                 var i2 = triangulation.indices[i + 1];
    8.                 var i3 = triangulation.indices[i + 2];
    9.                 var p1 = triangulation.vertices[i1];
    10.                 var p2 = triangulation.vertices[i2];
    11.                 var p3 = triangulation.vertices[i3];
    12.  
    13.                 GL.Vertex(p1);
    14.                 GL.Vertex(p2);
    15.                 GL.Vertex(p3);
    16.             }
    17.             GL.End();
     
  3. Enderlook

    Enderlook

    Joined:
    Dec 4, 2018
    Posts:
    50
    Thanks for the information.
    When implementing that, I also discovered that if you manually use `NavMesh.AddNavMeshData(navMeshData)` in the editor, it's shown in the scene view, which I wanted.
     
    Last edited: Jun 5, 2023