static void NavMeshTriangulation_indices(JSVCall vc) { if (vc.bGet) { UnityEngine.NavMeshTriangulation _this = (UnityEngine.NavMeshTriangulation)vc.csObj; var result = _this.indices; var arrRet = result; for (int i = 0; arrRet != null && i < arrRet.Length; i++) { JSApi.setInt32((int)JSApi.SetType.SaveAndTempTrace, arrRet[i]); JSApi.moveSaveID2Arr(i); } JSApi.setArrayS((int)JSApi.SetType.Rval, (arrRet != null ? arrRet.Length : 0), true); } else { System.Int32[] arg0 = JSDataExchangeMgr.GetJSArg <System.Int32[]>(() => { int jsObjID = JSApi.getObject((int)JSApi.GetType.Arg); int length = JSApi.getArrayLength(jsObjID); var ret = new System.Int32[length]; for (var i = 0; i < length; i++) { JSApi.getElement(jsObjID, i); ret[i] = (System.Int32)JSApi.getInt32((int)JSApi.GetType.SaveAndRemove); } return(ret); }); UnityEngine.NavMeshTriangulation _this = (UnityEngine.NavMeshTriangulation)vc.csObj; _this.indices = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
public static NavMeshTriangulation CalculateTriangulation() { NavMeshTriangulation result = default(NavMeshTriangulation); NavMesh.TriangulateInternal(ref result.vertices, ref result.indices, ref result.areas); return(result); }
static void Build() { GameObject g = new GameObject("navMesh"); MeshFilter mf = g.AddComponent <MeshFilter>(); #if UNITY_5_5_OR_NEWER UnityEngine.AI.NavMeshTriangulation triangles = UnityEngine.AI.NavMesh.CalculateTriangulation(); #else UnityEngine.NavMeshTriangulation triangles = UnityEngine.NavMesh.CalculateTriangulation(); #endif Mesh mesh = new Mesh(); mesh.vertices = triangles.vertices; mesh.triangles = triangles.indices; Vector2[] uvs = new Vector2[mesh.vertices.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(0f, 0f); } mesh.uv = uvs; mesh.RecalculateNormals(); mf.mesh = mesh; // MeshRenderer mr= g.AddComponent<MeshRenderer>(); }
static public int set_layers(IntPtr l) { UnityEngine.NavMeshTriangulation o = (UnityEngine.NavMeshTriangulation)checkSelf(l); System.Int32[] v; checkType(l, 2, out v); o.layers = v; setBack(l, o); return(0); }
static public int set_vertices(IntPtr l) { UnityEngine.NavMeshTriangulation o = (UnityEngine.NavMeshTriangulation)checkSelf(l); UnityEngine.Vector3[] v; checkType(l, 2, out v); o.vertices = v; setBack(l, o); return(0); }
static public int constructor(IntPtr l) { try { UnityEngine.NavMeshTriangulation o; o=new UnityEngine.NavMeshTriangulation(); pushValue(l,true); pushValue(l,o); return 2; } catch(Exception e) { return error(l,e); } }
static public int CalculateTriangulation_s(IntPtr l) { try{ UnityEngine.NavMeshTriangulation ret = UnityEngine.NavMesh.CalculateTriangulation(); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int constructor(IntPtr l) { try { UnityEngine.NavMeshTriangulation o; o = new UnityEngine.NavMeshTriangulation(); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } }
public static int constructor(IntPtr l) { try { UnityEngine.NavMeshTriangulation o; o=new UnityEngine.NavMeshTriangulation(); pushValue(l,o); return 1; } catch(Exception e) { LuaDLL.luaL_error(l, e.ToString()); return 0; } }
static public int constructor(IntPtr l) { try { UnityEngine.NavMeshTriangulation o; o = new UnityEngine.NavMeshTriangulation(); pushValue(l, o); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
//private GameObject navmeshGo; void Start() { navMeshTri = UnityEngine.NavMesh.CalculateTriangulation(); this.name = "NavmeshGo"; MeshFilter rMeshFilter = this.gameObject.AddComponent <MeshFilter>(); Mesh rMesh = new Mesh(); rMesh.vertices = navMeshTri.vertices; rMesh.SetIndices(navMeshTri.indices, MeshTopology.Triangles, 0); rMeshFilter.sharedMesh = rMesh; MeshRenderer rMeshRenderer = this.gameObject.AddComponent <MeshRenderer>(); rMeshRenderer.sharedMaterial = mat; }
static int _CreateNavMeshTriangulation(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 0); NavMeshTriangulation obj = new NavMeshTriangulation(); LuaScriptMgr.PushValue(L, obj); return 1; }
static public int get_layers(IntPtr l) { UnityEngine.NavMeshTriangulation o = (UnityEngine.NavMeshTriangulation)checkSelf(l); pushValue(l, o.layers); return(1); }
/// \brief Modifies the given NavMesh so that only the Navigation areas are present in the mesh. This is done only /// by swapping, so that no new memory is allocated. /// /// The data stored outside of the returned array sizes should be considered invalid and will contain garbage data. /// \param navMesh NavMesh data to modify /// \param area Area mask to include in returned mesh. Areas outside of this mask are culled. /// \param vert_size New size of navMesh.vertices /// \param tri_size New size of navMesh.areas and one third of the size of navMesh.indices private static void CullNavmeshTriangulation(ref NavMeshTriangulation navMesh, int area, out int vert_size, out int tri_size) { // Step 1: re-order triangles so that valid areas are in front. Then determine tri_size. tri_size = navMesh.indices.Length / 3; for(int i=0; i < tri_size; i++) { Vector3 p1 = navMesh.vertices[navMesh.indices[i * 3]]; Vector3 p2 = navMesh.vertices[navMesh.indices[i * 3 + 1]]; Vector3 p3 = navMesh.vertices[navMesh.indices[i * 3 + 2]]; Plane p = new Plane(p1, p2, p3); bool vertical = Mathf.Abs(Vector3.Dot(p.normal, Vector3.up)) > 0.99f; // If the current triangle isn't flat (normal is up) or if it doesn't match // with the provided mask, we should cull it. if(((1 << navMesh.areas[i]) & area) == 0 || !vertical) // If true this triangle should be culled. { // Swap area indices and triangle indices with the end of the array int t_ind = tri_size - 1; int t_area = navMesh.areas[t_ind]; navMesh.areas[t_ind] = navMesh.areas[i]; navMesh.areas[i] = t_area; for(int j=0;j<3;j++) { int t_v = navMesh.indices[t_ind * 3 + j]; navMesh.indices[t_ind * 3 + j] = navMesh.indices[i * 3 + j]; navMesh.indices[i * 3 + j] = t_v; } // Then reduce the size of the array, effectively cutting off the previous triangle tri_size--; // Stay on the same index so that we can check the triangle we just swapped. i--; } } // Step 2: Cull the vertices that aren't used. vert_size = 0; for(int i=0; i < tri_size * 3; i++) { int prv = navMesh.indices[i]; if (prv >= vert_size) { int nxt = vert_size; // Bring the current vertex to the end of the "active" array by swapping it with what's currently there Vector3 t_v = navMesh.vertices[prv]; navMesh.vertices[prv] = navMesh.vertices[nxt]; navMesh.vertices[nxt] = t_v; // Now change around the values in the triangle indices to reflect the swap for(int j=i; j < tri_size * 3; j++) { if (navMesh.indices[j] == prv) navMesh.indices[j] = nxt; else if (navMesh.indices[j] == nxt) navMesh.indices[j] = prv; } // Increase the size of the vertex array to reflect the changes. vert_size++; } } }
/// \brief Converts a NavMesh (or a NavMesh area) into a standard Unity mesh. This is later used /// to render the mesh on-screen using Unity's standard rendering tools. /// /// \param navMesh Precalculated Nav Mesh Triangulation /// \param vert_size size of vertex array /// \param tri_size size of triangle array private static Mesh ConvertNavmeshToMesh(NavMeshTriangulation navMesh, int vert_size, int tri_size) { Mesh ret = new Mesh(); if(vert_size >= 65535) { Debug.LogError("Playable NavMesh too big (vertex count >= 65535)! Limit the size of the playable area using"+ "Area Masks. For now no preview mesh will render."); return ret; } Vector3[] vertices = new Vector3[vert_size]; for (int x = 0; x < vertices.Length; x++) // Note: Unity navmesh is offset 0.05m from the ground. This pushes it down to 0 vertices[x] = navMesh.vertices[x]; int[] triangles = new int[tri_size * 3]; for (int x = 0; x < triangles.Length; x++) triangles[x] = navMesh.indices[x]; ret.name = "Navmesh"; ret.vertices = vertices; ret.triangles = triangles; RemoveMeshDuplicates(ret); ret.RecalculateNormals(); ret.RecalculateBounds(); return ret; }
public GenerateGraph(Vector3 start) { //get nav mesh characteristics from pre-made nav mesh. Will write script later that generates //a nav-mesh for any map. navmesh = NavMesh.CalculateTriangulation(); //initialize triangles array meshTriangles = new Triangle[navmesh.indices.Length/3]; //will contain mapping from a string containing the Vector3 pair side and the lane type of a node(ex: (1,2,3) and (4,5,6) //in "middle" will be respresented as "1,2,3 - 4,5,6 middle" with the smaller Vector3 coming first) to the node on that //side with that lane type. Dictionary<string, Node> sideToNode = new Dictionary<string, Node>(); //will contain mapping from a Node to the list of Triangles that contain that Node on a side Dictionary<Node, List<Triangle>> nodeToTriangles = new Dictionary<Node, List<Triangle>>(); nodes = new List<Node>(); //will contain a mapping from Vector3 coordinates (ex: (1,2,3) will be represented as "1,2,3") to //a Node Dictionary<string, Node> coordinatesToNode = new Dictionary<string, Node>(); //Made sure nav mesh indices is a multiple of 3 for (int i = 0; i < navmesh.indices.Length / 3; i++) { Vector3[] currentVectors = new Vector3[3]; Vector3 v1 = navmesh.vertices[navmesh.indices[i*3]]; Vector3 v2 = navmesh.vertices[navmesh.indices[i*3 + 1]]; Vector3 v3 = navmesh.vertices[navmesh.indices[i*3 + 2]]; meshTriangles[i] = new Triangle(v1, v2, v3, NavMesh.GetAreaCost(navmesh.areas[i])); List<Vector3Pair> trianglePairs = new List<Vector3Pair>(); //Add the pair v1, v2 to trianglePairs trianglePairs.Add(new Vector3Pair(v1, v2)); //Add the pair v2, v3 trianglePairs trianglePairs.Add(new Vector3Pair(v2, v3)); //Add the pair v1, v3 trianglePairs.Add(new Vector3Pair(v1, v3)); //Calculate bisections. Needed to generate smoother paths foreach (Vector3Pair currentVector3Pair in trianglePairs) { Vector3 currentFirst = currentVector3Pair.first; Vector3 currentSecond = currentVector3Pair.second; Vector3 bisect1 = new Vector3((currentFirst.x + currentSecond.x)/2, (currentFirst.y + currentSecond.y)/2, (currentFirst.z + currentSecond.z)/2); Vector3 bisect2 = new Vector3((bisect1.x + currentFirst.x)/2, (bisect1.y + currentFirst.y)/2, (bisect1.z + currentFirst.z)/2); Vector3 bisect3 = new Vector3((bisect1.x + currentSecond.x)/2, (bisect1.y + currentSecond.y)/2, (bisect1.z + currentSecond.z)/2); Node bisect1Node = getNodeWithVectorCoordinates(ref coordinatesToNode, bisect1, "middle"); Node bisect2Node = getNodeWithVectorCoordinates(ref coordinatesToNode, bisect2, "outer"); Node bisect3Node = getNodeWithVectorCoordinates(ref coordinatesToNode, bisect3, "outer"); AddToDictionary(ref nodeToTriangles, bisect1Node, meshTriangles[i]); AddToDictionary(ref nodeToTriangles, bisect2Node, meshTriangles[i]); AddToDictionary(ref nodeToTriangles, bisect3Node, meshTriangles[i]); sideToNode[GetPairString(currentFirst, currentSecond) + " middle"] = bisect1Node; sideToNode[GetPairString(currentFirst, currentSecond) + " outer1"] = bisect2Node; sideToNode[GetPairString(currentFirst, currentSecond) + " outer2"] = bisect3Node; } Vector3 currentCentroid = meshTriangles[i].Centroid (); Node centroidNode = getNodeWithVectorCoordinates(ref coordinatesToNode, currentCentroid, "middle"); AddToDictionary(ref nodeToTriangles, centroidNode, meshTriangles[i]); sideToNode[GetPairString (currentCentroid, currentCentroid) + " middle"] = centroidNode; } //set neighbors of each node foreach (var item in nodeToTriangles) { Node currentNode = item.Key; //iterate through all triangles that contain the currentNode on a side foreach (Triangle t in item.Value) { List<Vector3Pair> trianglePairs = new List<Vector3Pair>(); trianglePairs.Add(new Vector3Pair(t.vertex1, t.vertex2)); trianglePairs.Add(new Vector3Pair(t.vertex2, t.vertex3)); trianglePairs.Add(new Vector3Pair(t.vertex1, t.vertex3)); foreach (Vector3Pair trianglePair in trianglePairs) { Vector3 currentFirst = trianglePair.first; Vector3 currentSecond = trianglePair.second; if (currentNode.laneType == "middle") { addNodeNeighbor(sideToNode, ref currentNode, currentFirst, currentSecond, "middle"); } else { addNodeNeighbor(sideToNode, ref currentNode, currentFirst, currentSecond, "outer1"); addNodeNeighbor(sideToNode, ref currentNode, currentFirst, currentSecond, "outer2"); } } } nodes.Add(currentNode); } //set start node of the car startNode = getClosestNode(start); //set end node of the car int possible_end_node = 900; endNode = getClosestNode (GameObject.Find ("FinishLine").transform.position); /*while (true) { if (nodes[possible_end_node].laneType == startNode.laneType) { endNode = nodes[possible_end_node]; break; } else { possible_end_node = possible_end_node + 1; } }*/ }