public static Polygon GetPolygon(ref TriangleNet.Data.Triangle triangle) { List<Coordinate> coords = new List<Coordinate>(); Vertex v1 = triangle.GetVertex(0); coords.Add(new Coordinate(v1.X, v1.Y)); v1 = triangle.GetVertex(1); coords.Add(new Coordinate(v1.X, v1.Y)); v1 = triangle.GetVertex(2); coords.Add(new Coordinate(v1.X, v1.Y)); return new Polygon(coords); }
public static List<Point> GetTriangleAsPoints(ref TriangleNet.Data.Triangle triangle) { Vertex v1 = triangle.GetVertex(0); Vertex v2 = triangle.GetVertex(1); Vertex v3 = triangle.GetVertex(2); List<Point> points = new List<Point>(); points.Add(new Point(v1.X, v1.Y, v1.Attributes[0])); points.Add(new Point(v2.X, v2.Y, v2.Attributes[0])); points.Add(new Point(v3.X, v3.Y, v3.Attributes[0])); return points; }
public static bool Contains(ref TriangleNet.Data.Triangle triangle, double x, double y) { Vertex v1 = triangle.GetVertex(0); Vertex v2 = triangle.GetVertex(1); Vertex v3 = triangle.GetVertex(2); Vertex pt = new Vertex(x, y); bool b1, b2, b3; b1 = Sign(ref pt, ref v1, ref v2) < 0.0f; b2 = Sign(ref pt, ref v2, ref v3) < 0.0f; b3 = Sign(ref pt, ref v3, ref v1) < 0.0f; return ((b1 == b2) && (b2 == b3)); }
void OnDrawGizmos() { if (holes.Length <= 0) { return; } Gizmos.color = Color.black; logoOutline.GizmoDraw(); Gizmos.color = Color.white; foreach (UPolygon hole in holes) { hole.GizmoDraw(); } if (meshRepresentation == null) { return; } Gizmos.color = Color.cyan; foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in meshRepresentation.triangles) { TriangleNet.Data.Triangle triangle = pair.Value; TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0); TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1); TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2); Vector2 p0 = new Vector2((float)vertex0.x, (float)vertex0.y); Vector2 p1 = new Vector2((float)vertex1.x, (float)vertex1.y); Vector2 p2 = new Vector2((float)vertex2.x, (float)vertex2.y); Gizmos.DrawLine(p0, p1); Gizmos.DrawLine(p1, p2); Gizmos.DrawLine(p2, p0); } }
// Use this for initialization void Start() { geometry = new InputGeometry(); List <Point> shape = new List <Point> (); shape.Add(new Point(0f, 0f)); shape.Add(new Point(2f, 2f)); shape.Add(new Point(1f, 4f)); shape.Add(new Point(3f, 5f)); shape.Add(new Point(-3f, 5f)); shape.Add(new Point(-1f, 4f)); shape.Add(new Point(-2f, 2f)); geometry.AddRing(shape); //it is necessary to put a border around all the points in order to get triangulation to work correctly when holes are used // List<Point> border = new List<Point>(); // border.Add(new Point(distance, verticalDistance)); // border.Add(new Point(distance, -verticalDistance)); // border.Add(new Point(-distance, -verticalDistance)); // border.Add(new Point(-distance, verticalDistance)); // geometry.AddRing(border); // List<Point> outlinePoints = new List<Point>(logoOutline.points.Length); // foreach (Vector2 coordinates in logoOutline.points) // { // outlinePoints.Add(new Point(coordinates)); // } // // geometry.AddRingAsHole(outlinePoints, 0); // // // foreach(UPolygon hole in holes) // { // List<Point> holePoints = new List<Point>(hole.points.Length); // // foreach (Vector2 coordinates in hole.points) // holePoints.Add(new Point(coordinates)); // // geometry.AddRing(holePoints, 0); // } // List<Point> points = new List<Point>(); // for (float offsetX = -distance; offsetX < distance; offsetX += boxDistance) // { // for (float offsetY = -verticalDistance; offsetY < verticalDistance; offsetY += boxDistance) // { // Vector2 offset = new Vector2(offsetX, offsetY) + Vector2.one * boxDistance * 0.5f; // // float radians = Random.RandomRange(0, 2 * Mathf.PI); // float length = Random.RandomRange(0, circleDistance); // // Vector2 pos = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * length; // pos += offset; // // bool insideOutline = logoOutline.PointInPolygon(pos); // // bool stillAlloved = false; // for (int i = 0; i < holes.Length; i++ ) // { // if (holes[i].PointInPolygon(pos)) // stillAlloved = true; // } // // if (!insideOutline || stillAlloved) // geometry.AddPoint((float)pos.x, (float)pos.y, 0); // } // } meshRepresentation = new TriangleNet.Mesh(); meshRepresentation.Triangulate(geometry); //generate mesh based on triangulation Dictionary <int, float> zOffsets = new Dictionary <int, float>(); // foreach(KeyValuePair<int, TriangleNet.Data.Vertex> pair in meshRepresentation.vertices) // { // zOffsets.Add(pair.Key, Random.RandomRange(-zOffset, zOffset)); // } int triangleIndex = 0; List <Vector3> vertices = new List <Vector3>(meshRepresentation.triangles.Count * 3); List <int> triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3); foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in meshRepresentation.triangles) { TriangleNet.Data.Triangle triangle = pair.Value; TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0); TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1); TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2); Vector3 p0 = new Vector3(vertex0.x, vertex0.y, 0); Vector3 p1 = new Vector3(vertex1.x, vertex1.y, 0); Vector3 p2 = new Vector3(vertex2.x, vertex2.y, 0); vertices.Add(p0); vertices.Add(p1); vertices.Add(p2); triangleIndices.Add(triangleIndex + 2); triangleIndices.Add(triangleIndex + 1); triangleIndices.Add(triangleIndex); triangleIndex += 3; } mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangleIndices.ToArray(); mesh.RecalculateNormals(); GetComponent <MeshFilter>().mesh = mesh; }
public static void CreateMesh(GameObject meshContainer, string path, string path2, string adfname) { var subgraphs = _LoadMarkerToSubgraph(path, path2); meshContainer.name = adfname + "Mesh"; InputGeometry geometry = new InputGeometry(); TriangleNet.Mesh meshRepresentation; MeshFilter mf = meshContainer.GetComponent <MeshFilter> () as MeshFilter; Mesh mesh; float zOffset = 0.1f; float[] maxArea = new float[2] { 0, 0 }; List <List <Point> > clusters = new List <List <Point> > (); foreach (Graph <Node> s in subgraphs) { List <Point> points = new List <Point> (); foreach (Node n in s.Nodes) { Point tmp = new Point(n.position.x, n.position.z); zOffset = n.position.y; points.Add(tmp); } if (points.Count > 2) { clusters.Add(points); // Calculate areas of the clusters. The largest is the external ring, while the others must be holes. float num = CalculateArea(points); if (num > maxArea [0]) { maxArea [0] = num; maxArea [1] = clusters.Count - 1; } } Debug.Log(clusters [clusters.Count - 1].Count); } geometry.AddRing(clusters[(int)maxArea[1]]); clusters.RemoveAt((int)maxArea [1]); foreach (List <Point> c in clusters) { geometry.AddRingAsHole(c); } meshRepresentation = new TriangleNet.Mesh(); meshRepresentation.Triangulate(geometry); // Dictionary<int, float> zOffsets = new Dictionary<int, float>(); // // foreach(KeyValuePair<int, TriangleNet.Data.Vertex> pair in meshRepresentation.vertices) // { // zOffsets.Add(pair.Key, Random.RandomRange(-zOffset, zOffset)); // } int triangleIndex = 0; List <Vector3> vertices = new List <Vector3>(meshRepresentation.triangles.Count * 3); List <int> triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3); foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in meshRepresentation.triangles) { TriangleNet.Data.Triangle triangle = pair.Value; TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0); TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1); TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2); Vector3 p0 = new Vector3(vertex0.x, zOffset, vertex0.y); Vector3 p1 = new Vector3(vertex1.x, zOffset, vertex1.y); Vector3 p2 = new Vector3(vertex2.x, zOffset, vertex2.y); // Vector3 p0 = new Vector3( vertex0.x, vertex0.y, zOffsets[vertex0.id]); // Vector3 p1 = new Vector3( vertex1.x, vertex1.y, zOffsets[vertex1.id]); // Vector3 p2 = new Vector3( vertex2.x, vertex2.y, zOffsets[vertex2.id]); vertices.Add(p0); vertices.Add(p1); vertices.Add(p2); triangleIndices.Add(triangleIndex + 2); triangleIndices.Add(triangleIndex + 1); triangleIndices.Add(triangleIndex); triangleIndex += 3; } mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangleIndices.ToArray(); mesh.RecalculateNormals(); //GetComponent<MeshFilter>().mesh = mesh; mf.mesh = mesh; Exporter e = meshContainer.GetComponent <Exporter> () as Exporter; e.DoExport(true); // // File.WriteAllBytes(Application.persistentDataPath + "/" + adfname + "Mesh", MeshSerializer.WriteMesh (mesh, true)); meshContainer.SetActive(true); meshContainer.GetComponent <NavMeshSurface> ().BuildNavMesh(); Debug.Log("NavMesh created"); //MeshSaverEditor.SaveMesh (mesh, "meshtest", true, true); //ObjExporterScript.MeshToString(GetComponent<MeshFilter>(), }
public Mesh TriangulateMesh(Vector3[] Vertices) { if (Vertices.Length < 3) { return(null); } geometry = new InputGeometry(); foreach (Vector3 Vert in Vertices) { geometry.AddPoint(Vert.x, Vert.y); } List <Point> points = new List <Point>(); for (float offsetX = -distance; offsetX < distance; offsetX += boxDistance) { for (float offsetY = -verticalDistance; offsetY < verticalDistance; offsetY += boxDistance) { Vector2 offset = new Vector2(offsetX, offsetY) + Vector2.one * boxDistance * 0.5f; float radians = Random.Range(0, 2 * Mathf.PI); float length = Random.Range(0, circleDistance); Vector2 pos = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * length; pos += offset; } } meshRepresentation = new TriangleNet.Mesh(); meshRepresentation.Triangulate(geometry); //generate mesh based on triangulation Dictionary <int, float> zOffsets = new Dictionary <int, float>(); foreach (KeyValuePair <int, TriangleNet.Data.Vertex> pair in meshRepresentation.vertices) { zOffsets.Add(pair.Key, Random.Range(-zOffset, zOffset)); } int triangleIndex = 0; List <Vector3> vertices = new List <Vector3>(meshRepresentation.triangles.Count * 3); List <int> triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3); foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in meshRepresentation.triangles) { TriangleNet.Data.Triangle triangle = pair.Value; TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0); TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1); TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2); Vector3 p0 = new Vector3(vertex0.x, vertex0.y, zOffsets[vertex0.id]); Vector3 p1 = new Vector3(vertex1.x, vertex1.y, zOffsets[vertex1.id]); Vector3 p2 = new Vector3(vertex2.x, vertex2.y, zOffsets[vertex2.id]); vertices.Add(p0); vertices.Add(p1); vertices.Add(p2); triangleIndices.Add(triangleIndex + 2); triangleIndices.Add(triangleIndex + 1); triangleIndices.Add(triangleIndex); triangleIndex += 3; } mesh = new Mesh(); mesh.name = "Triangulated Terrain"; mesh.vertices = vertices.ToArray(); mesh.triangles = triangleIndices.ToArray(); return(mesh); }
static List<Vertex> IndexVertices(Triangle triangle, Dictionary<int, int> indexDict, ref int highestIndex, out int[] newIndices) { var newVertices = new List<Vertex>(); newIndices = new int[3]; if (indexDict.ContainsKey(triangle.P0)) { newIndices[0] = indexDict[triangle.P0]; } else { TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0); newVertices.Add(new Vertex((float)vertex0.X, (float)vertex0.Y)); indexDict.Add(vertex0.ID, highestIndex); newIndices[0] = highestIndex; highestIndex++; } if (indexDict.ContainsKey(triangle.P1)) { newIndices[1] = indexDict[triangle.P1]; } else { TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1); newVertices.Add(new Vertex((float)vertex1.X, (float)vertex1.Y)); indexDict.Add(vertex1.ID, highestIndex); newIndices[1] = highestIndex; highestIndex++; } if (indexDict.ContainsKey(triangle.P2)) { newIndices[2] = indexDict[triangle.P2]; } else { TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2); newVertices.Add(new Vertex((float)vertex2.X, (float)vertex2.Y)); indexDict.Add(vertex2.ID, highestIndex); newIndices[2] = highestIndex; highestIndex++; } return newVertices; }