Make Unity Navmesh for me

Source: Internet
Author: User

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

    1. 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

    1. First, using the Astar algorithm to find the passed triangle from the Navmesh connected graph, the gray part of the figure
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.