Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice
  2. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

Accessing Navmesh vertices?

Discussion in 'Scripting' started by mgrenier, Apr 5, 2012.

  1. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
    I want to automate rough placement of Lights probe above the Navmesh generated by Unity.

    Is there anyway I can access proper mesh data of a scene Navmesh?
     
  2. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    Maybe you can use NavMesh.SamplePosition to find a grid of navmesh positions
     
  3. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
    Knowing that Unity uses Recast to generate the navmesh convex polygons, I was looking for the raw mesh data that recast also output. Having these triangles that cover the dynamic areas of the scene would make a great way to roughly place lights probe.

    I'm working (not actively) on a recast implementation (see below). So for now, I do have all the data I need to place my lights probe.

     
    EZaca likes this.
  4. aigam

    aigam

    Joined:
    Dec 31, 2005
    Posts:
    170
    I'm also interested on accessing vertices data. Any idea?
    Thks!
     
  5. hipcodder

    hipcodder

    Joined:
    Sep 3, 2009
    Posts:
    221
    There is no method exposing the navmesh nodes and that is understandable since the nodes contain information that user should not have access to..You can use a Reflector decompiler to check you UnityEngine.dll and UnityEditor.dll
     
  6. gunder

    gunder

    Joined:
    Jun 23, 2009
    Posts:
    16
    I have a script to convert navmesh to obj objects.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    anyone else approaching this would likely want to keep in mind that Aron Granbergs A* path finding in its $100 Pro license utilizes the opensource Recast navmesh generation technology as well, with support for multiple meshes instead of just one as in Unitys case.
     
  8. FHU3D

    FHU3D

    Joined:
    Oct 12, 2012
    Posts:
    1
    I'm interested in your script, you can send me?Thank you so much
     
  9. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Since this has been revived, there is an undocumented accessor (the name escapes me) for grabbing the Mesh of the navigation mesh at editor time. Note that it is editor-time *only* though.
     
  10. kresnik

    kresnik

    Joined:
    Sep 28, 2012
    Posts:
    7
    i want it too.. i beg you, please post it or send to vladcaflava@gmail.com

    im frustated as i can't access the navmesh points, because i want to give score for each point which unity automatically bake

    thanks
     
  11. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,398
    This thread wins the prize for:

    - unity sucking at finishing jobs ie navmesh
    - users sucking by almost being helpful but not quite

    Get a grip guys.
     
    pdusen, irenya, jakethagun and 3 others like this.
  12. ZemaChris

    ZemaChris

    Joined:
    Jul 30, 2012
    Posts:
    4
    Can you please tell us how to do this?
     
  13. ZemaChris

    ZemaChris

    Joined:
    Jul 30, 2012
    Posts:
    4
    Can you please tell us how to do this?
     
  14. Drakorian-Labs

    Drakorian-Labs

    Joined:
    Feb 23, 2012
    Posts:
    88
    Hi AngryAnt, May i ask you to do an effort and try to remember that class name? Since you work on Unity it may be easy to ask someone :)
    (And to be honest your post is not that much of help :p...hehehe)
    Currently i'm stuck with the navigation and obstacle implementation in unity 4, because, well, basically is not very good (sorry to be this honest, but take it as a constructive feedback).
    If we can have access to the navmesh data, the we can try to create our own local avoidance system, or use one available in c#, so please please, give us some info
    All the best.
     
    Last edited: Dec 4, 2012
  15. Pangamini

    Pangamini

    Joined:
    Aug 8, 2012
    Posts:
    52
    You can access the navMesh data. I've made a simple script that creates a renderable mesh off it (just like the one you see in the navMesh baking editor). It's not a documented feature though
    Code (csharp):
    1. import UnityEngine
    2.  
    3. class NavMeshVis (MonoBehaviour):
    4.  
    5.     [SerializeField] meshFilter as MeshFilter
    6.  
    7.     def Start ():
    8.         verts as (Vector3)     
    9.         ids as (int)
    10.        
    11.         NavMesh.Triangulate(verts, ids)
    12.        
    13.         mesh = Mesh()
    14.         mesh.vertices = verts
    15.         mesh.triangles = ids
    16.         meshFilter.mesh = mesh
    17.  
     
    davidrochin and Ash-Blue like this.
  16. Yaukey

    Yaukey

    Joined:
    Feb 25, 2013
    Posts:
    8
    This is very helpful! Thank you!!!:p
     
  17. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    776
    I wrote a script that adds a menu option to export the current scene's NavMesh to an .obj file. Unity 4.6.

    Code (csharp):
    1.  
    2. using System.IO;
    3. using System.Text;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. // Obj exporter component based on: http://wiki.unity3d.com/index.php?title=ObjExporter
    8.  
    9. public class ExportNavMeshToObj : MonoBehaviour {
    10.  
    11.     [MenuItem("Custom/Export NavMesh to mesh")]
    12.     static void Export() {
    13.         NavMeshTriangulation triangulatedNavMesh = NavMesh.CalculateTriangulation();
    14.  
    15.         Mesh mesh = new Mesh();
    16.         mesh.name = "ExportedNavMesh";
    17.         mesh.vertices = triangulatedNavMesh.vertices;
    18.         mesh.triangles = triangulatedNavMesh.indices;
    19.         string filename = Application.dataPath +"/" + Path.GetFileNameWithoutExtension(EditorApplication.currentScene) + " Exported NavMesh.obj";
    20.         MeshToFile(mesh, filename);
    21.         print("NavMesh exported as '" + filename + "'");
    22.         AssetDatabase.Refresh();
    23.     }
    24.  
    25.     static string MeshToString(Mesh mesh) {
    26.         StringBuilder sb = new StringBuilder();
    27.  
    28.         sb.Append("g ").Append(mesh.name).Append("\n");
    29.         foreach (Vector3 v in mesh.vertices) {
    30.             sb.Append(string.Format("v {0} {1} {2}\n",v.x,v.y,v.z));
    31.         }
    32.         sb.Append("\n");
    33.         foreach (Vector3 v in mesh.normals) {
    34.             sb.Append(string.Format("vn {0} {1} {2}\n",v.x,v.y,v.z));
    35.         }
    36.         sb.Append("\n");
    37.         foreach (Vector3 v in mesh.uv) {
    38.             sb.Append(string.Format("vt {0} {1}\n",v.x,v.y));
    39.         }
    40.         for (int material = 0; material < mesh.subMeshCount; material++) {
    41.             sb.Append("\n");
    42.             //sb.Append("usemtl ").Append(mats[material].name).Append("\n");
    43.             //sb.Append("usemap ").Append(mats[material].name).Append("\n");
    44.  
    45.             int[] triangles = mesh.GetTriangles(material);
    46.             for (int i=0;i<triangles.Length;i+=3) {
    47.                 sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", triangles[i]+1, triangles[i+1]+1, triangles[i+2]+1));
    48.             }
    49.         }
    50.         return sb.ToString();
    51.     }
    52.  
    53.     static void MeshToFile(Mesh mesh, string filename) {
    54.         using (StreamWriter sw = new StreamWriter(filename)) {
    55.             sw.Write(MeshToString(mesh));
    56.         }
    57.     }
    58. }
    59.  
     
    Last edited: Feb 24, 2015
  18. dozhwal

    dozhwal

    Joined:
    Aug 21, 2014
    Posts:
    59
    A little of necroposting, but thx for this precious snipper :)
    Useful to set the environnement where the player can teleport in VR !
     
    Nition likes this.
  19. MythrilMan51

    MythrilMan51

    Joined:
    Apr 24, 2017
    Posts:
    146
    A little necroposting, but HOLY! I finally Found how Mind Over Mushroom got the nave mesh data. Now how do I malform a circle from it?
     
  20. none3d

    none3d

    Joined:
    Mar 4, 2021
    Posts:
    73

    Hello guys
    i use navSurface for the runtime baking
    Is there a way to load this file to navmeshsurface at the runtime instead of re-baking?
     
unityunity