Unity integrates a Navmesh function, though it makes people love and hate.
But when you need this Navmesh data somewhere else, it's even more addictive.
For example, when the server needs unity's Navmesh data.
For example, if you want to export the unity scene to other engines, such as the popular H5.
?
Recently I have encountered such a demand, took some detours, studied unity's Navmesh
- Export Unity's Navmesh data
First of all, I wrote an export script that Navmesh data can be exported to an obj, or exported to a JSON file.
You can use SVN to get the code.
Http://code.taobao.org/svn/falconconv/trunk/babylonfan/webgl/res_navmeshexport
?
?
The principle is that unity provides an interface to get Navmesh triangle data
There are direct vertices vertices, and indices vertex indexes
However, this data cannot be exported directly, this data cannot be exported directly.
Lead out of the triangle is basically not a common side, not the common side how to do connectivity diagram, this does not, so we have to analyze him first.
I've already analyzed it for you, unity invented a way to express polygons, and I didn't find anything like this, but it's the way unity invented it.
?
Unity's navmeshdata have some common-side triangles, and the common-side triangles are not connected, but they collectively form a convex polygon, and the common-edged triangles are all fan-shaped.
First, the polygon list is generated with this division. This polygon list, of course, has no common edges.
?
Unity's navmeshdata those polygons that are not coplanar are just index indexes that are not collinear, or have common edges, so we merge the coincident vertices, rearrange the index of the polygon, and we can revert to the Polygon list and vertex list with a common edge.
?
After doing the above two steps, we get the original Navmesh data, with the Polygon list and vertex list of the common edge
It is then written to the obj file or to the JSON file.
The specific unity trick is the above two steps, the code can pull SVN research.
Second, the external use of these data
Then we can get rid of unity to achieve a navmesh, here actually if you touch the road, no longer look, if you lazy, then look down
?
I first implemented a navmesh in the WinForm environment with C #
You can use SVN to pull the test program source code
Http://code.taobao.org/svn/falconconv/trunk/babylonfan/webgl/navmesh_pathfinding_test
Using the exported JSON file to draw the connected graph, the mouse key around to complete the search path.
Navmesh's pathfinding principle is a two-step approach
- First, using the Astar algorithm to find the passed triangle from the Navmesh connected graph, the gray part of the figure
- Using irradiation to find a path in a passing triangle
Then we ported the C # code to typescript code, and I took Navmesh to H5.
The same can be pulled from the SVN replacement code:
Http://code.taobao.org/svn/falconconv/trunk/babylonfan/webgl/canvas/canvasWebgl_NavMesh
Make Unity Navmesh for me