private void GenerateNodes(Vector3[] vectorVertices, int[] triangles, out Vector3[] originalVertices, out VInt3[] vertices) { if (vectorVertices.Length == 0 || triangles.Length == 0) { originalVertices = vectorVertices; vertices = new VInt3[0]; this.nodes = new TriangleMeshNode[0]; return; } vertices = new VInt3[vectorVertices.Length]; int num = 0; for (int i = 0; i < vertices.Length; i++) { vertices[i] = (VInt3)this.matrix.MultiplyPoint3x4(vectorVertices[i]); } Dictionary<VInt3, int> dictionary = new Dictionary<VInt3, int>(); int[] array = new int[vertices.Length]; for (int j = 0; j < vertices.Length; j++) { if (!dictionary.ContainsKey(vertices[j])) { array[num] = j; dictionary.Add(vertices[j], num); num++; } } for (int k = 0; k < triangles.Length; k++) { VInt3 vInt = vertices[triangles[k]]; triangles[k] = dictionary.get_Item(vInt); } VInt3[] array2 = vertices; vertices = new VInt3[num]; originalVertices = new Vector3[num]; for (int l = 0; l < num; l++) { vertices[l] = array2[array[l]]; originalVertices[l] = vectorVertices[array[l]]; } this.nodes = new TriangleMeshNode[triangles.Length / 3]; int graphIndex = this.active.astarData.GetGraphIndex(this); for (int m = 0; m < this.nodes.Length; m++) { this.nodes[m] = new TriangleMeshNode(this.active); TriangleMeshNode triangleMeshNode = this.nodes[m]; triangleMeshNode.GraphIndex = (uint)graphIndex; triangleMeshNode.Penalty = this.initialPenalty; triangleMeshNode.Walkable = true; triangleMeshNode.v0 = triangles[m * 3]; triangleMeshNode.v1 = triangles[m * 3 + 1]; triangleMeshNode.v2 = triangles[m * 3 + 2]; if (!Polygon.IsClockwise(vertices[triangleMeshNode.v0], vertices[triangleMeshNode.v1], vertices[triangleMeshNode.v2])) { int v = triangleMeshNode.v0; triangleMeshNode.v0 = triangleMeshNode.v2; triangleMeshNode.v2 = v; } if (Polygon.IsColinear(vertices[triangleMeshNode.v0], vertices[triangleMeshNode.v1], vertices[triangleMeshNode.v2])) { Debug.DrawLine((Vector3)vertices[triangleMeshNode.v0], (Vector3)vertices[triangleMeshNode.v1], Color.red); Debug.DrawLine((Vector3)vertices[triangleMeshNode.v1], (Vector3)vertices[triangleMeshNode.v2], Color.red); Debug.DrawLine((Vector3)vertices[triangleMeshNode.v2], (Vector3)vertices[triangleMeshNode.v0], Color.red); } triangleMeshNode.UpdatePositionFromVertices(); } DictionaryView<VInt2, TriangleMeshNode> dictionaryView = new DictionaryView<VInt2, TriangleMeshNode>(); int n = 0; int num2 = 0; while (n < triangles.Length) { dictionaryView[new VInt2(triangles[n], triangles[n + 1])] = this.nodes[num2]; dictionaryView[new VInt2(triangles[n + 1], triangles[n + 2])] = this.nodes[num2]; dictionaryView[new VInt2(triangles[n + 2], triangles[n])] = this.nodes[num2]; num2++; n += 3; } ListLinqView<MeshNode> listLinqView = new ListLinqView<MeshNode>(); List<uint> list = new List<uint>(); int num3 = 0; int num4 = 0; int num5 = 0; while (num4 < triangles.Length) { listLinqView.Clear(); list.Clear(); TriangleMeshNode triangleMeshNode2 = this.nodes[num5]; for (int num6 = 0; num6 < 3; num6++) { TriangleMeshNode triangleMeshNode3; if (dictionaryView.TryGetValue(new VInt2(triangles[num4 + (num6 + 1) % 3], triangles[num4 + num6]), out triangleMeshNode3)) { listLinqView.Add(triangleMeshNode3); list.Add((uint)(triangleMeshNode2.position - triangleMeshNode3.position).costMagnitude); } } triangleMeshNode2.connections = listLinqView.ToArray(); triangleMeshNode2.connectionCosts = list.ToArray(); num5++; num4 += 3; } if (num3 > 0) { Debug.LogError("One or more triangles are identical to other triangles, this is not a good thing to have in a navmesh\nIncreasing the scale of the mesh might help\nNumber of triangles with error: " + num3 + "\n"); } NavMeshGraph.RebuildBBTree(this); }
private void GenerateNodes(Vector3[] vectorVertices, int[] triangles, out Vector3[] originalVertices, out VInt3[] vertices) { if ((vectorVertices.Length == 0) || (triangles.Length == 0)) { originalVertices = vectorVertices; vertices = new VInt3[0]; this.nodes = new TriangleMeshNode[0]; } else { vertices = new VInt3[vectorVertices.Length]; int index = 0; for (int i = 0; i < vertices.Length; i++) { vertices[i] = (VInt3)this.matrix.MultiplyPoint3x4(vectorVertices[i]); } Dictionary <VInt3, int> dictionary = new Dictionary <VInt3, int>(); int[] numArray = new int[vertices.Length]; for (int j = 0; j < vertices.Length; j++) { if (!dictionary.ContainsKey(vertices[j])) { numArray[index] = j; dictionary.Add(vertices[j], index); index++; } } for (int k = 0; k < triangles.Length; k++) { VInt3 num5 = vertices[triangles[k]]; triangles[k] = dictionary[num5]; } VInt3[] numArray2 = vertices; vertices = new VInt3[index]; originalVertices = new Vector3[index]; for (int m = 0; m < index; m++) { vertices[m] = numArray2[numArray[m]]; originalVertices[m] = vectorVertices[numArray[m]]; } this.nodes = new TriangleMeshNode[triangles.Length / 3]; int graphIndex = base.active.astarData.GetGraphIndex(this); for (int n = 0; n < this.nodes.Length; n++) { this.nodes[n] = new TriangleMeshNode(base.active); TriangleMeshNode node = this.nodes[n]; node.GraphIndex = (uint)graphIndex; node.Penalty = base.initialPenalty; node.Walkable = true; node.v0 = triangles[n * 3]; node.v1 = triangles[(n * 3) + 1]; node.v2 = triangles[(n * 3) + 2]; if (!Polygon.IsClockwise(vertices[node.v0], vertices[node.v1], vertices[node.v2])) { int num9 = node.v0; node.v0 = node.v2; node.v2 = num9; } if (Polygon.IsColinear(vertices[node.v0], vertices[node.v1], vertices[node.v2])) { Debug.DrawLine((Vector3)vertices[node.v0], (Vector3)vertices[node.v1], Color.red); Debug.DrawLine((Vector3)vertices[node.v1], (Vector3)vertices[node.v2], Color.red); Debug.DrawLine((Vector3)vertices[node.v2], (Vector3)vertices[node.v0], Color.red); } node.UpdatePositionFromVertices(); } DictionaryView <VInt2, TriangleMeshNode> view = new DictionaryView <VInt2, TriangleMeshNode>(); int num10 = 0; int num11 = 0; while (num10 < triangles.Length) { view[new VInt2(triangles[num10], triangles[num10 + 1])] = this.nodes[num11]; view[new VInt2(triangles[num10 + 1], triangles[num10 + 2])] = this.nodes[num11]; view[new VInt2(triangles[num10 + 2], triangles[num10])] = this.nodes[num11]; num11++; num10 += 3; } ListLinqView <MeshNode> view2 = new ListLinqView <MeshNode>(); List <uint> list = new List <uint>(); int num12 = 0; int num13 = 0; int num14 = 0; while (num13 < triangles.Length) { view2.Clear(); list.Clear(); TriangleMeshNode node2 = this.nodes[num14]; for (int num15 = 0; num15 < 3; num15++) { TriangleMeshNode node3; if (view.TryGetValue(new VInt2(triangles[num13 + ((num15 + 1) % 3)], triangles[num13 + num15]), out node3)) { view2.Add(node3); VInt3 num16 = node2.position - node3.position; list.Add((uint)num16.costMagnitude); } } node2.connections = view2.ToArray(); node2.connectionCosts = list.ToArray(); num14++; num13 += 3; } if (num12 > 0) { Debug.LogError("One or more triangles are identical to other triangles, this is not a good thing to have in a navmesh\nIncreasing the scale of the mesh might help\nNumber of triangles with error: " + num12 + "\n"); } RebuildBBTree(this); } }