public static UnityEngine.Mesh Mesh(this Polygon this_, string name = "") { // Create geometry. InputGeometry geometry = this_.InputGeometry(); // Triangulate. TriangleNet.Mesh triangulatedMesh = new TriangleNet.Mesh(); triangulatedMesh.Triangulate(geometry); // Counts. int vertexCount = triangulatedMesh.vertices.Count; int triangleCount = triangulatedMesh.triangles.Count; // Debug.Log("Mesh.vertexCount ("+vertexCount+")"); // NumberOfInputPoints // Debug.Log("Mesh.triangleCount ("+triangleCount+")"); // NumberOfInputPoints // Mesh store. Vector3[] _vertices = new Vector3[vertexCount]; Vector2[] _uv = new Vector2[vertexCount]; Vector3[] _normals = new Vector3[vertexCount]; int[] _triangles = new int[triangleCount * 3]; foreach (KeyValuePair <int, TriangleNet.Data.Vertex> eachEntry in triangulatedMesh.vertices) { int index = eachEntry.Key; TriangleNet.Data.Vertex eachVertex = eachEntry.Value; _vertices[index] = new Vector3( (float)eachVertex.x, (float)eachVertex.y, 0.0f // As of 2D ); _uv[index] = _vertices[index]; _normals[index] = Vector3.forward; } int cursor = 0; foreach (KeyValuePair <int, TriangleNet.Data.Triangle> eachPair in triangulatedMesh.triangles) { TriangleNet.Data.Triangle eachTriangle = eachPair.Value; _triangles[cursor] = eachTriangle.P2; _triangles[cursor + 1] = eachTriangle.P1; _triangles[cursor + 2] = eachTriangle.P0; cursor += 3; } // Create / setup mesh. Mesh mesh = new Mesh(); mesh.vertices = _vertices; mesh.uv = _uv; mesh.normals = _normals; mesh.subMeshCount = 1; mesh.SetTriangles(_triangles, 0); mesh.name = name; 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); }
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); } }
public static BarycentricCoordinates GetCoordinates(Point Pp, Vertex Pa, Vertex Pb, Vertex Pc) { double[] p = { Pp.X, Pp.Y }; double[] a = { Pa.X, Pa.Y }; double[] b = { Pb.X, Pb.Y }; double[] c = { Pc.X, Pc.Y }; double[] v0 = { b[0] - a[0], b[1] - a[1] }; double[] v1 = { c[0] - a[0], c[1] - a[1] }; double[] v2 = { p[0] - a[0], p[1] - a[1] }; double d00 = dotProduct(v0, v0); double d01 = dotProduct(v0, v1); double d11 = dotProduct(v1, v1); double d20 = dotProduct(v2, v0); double d21 = dotProduct(v2, v1); double denom = d00 * d11 - d01 * d01; double v = ((d11 * d20 - d01 * d21) / denom); double w = ((d00 * d21 - d01 * d20) / denom); double u = (1.0f - v - w); return new BarycentricCoordinates(u, v, w); }
/// <summary> /// Add a bad triangle to the end of a queue. /// </summary> /// <param name="enqtri"></param> /// <param name="minedge"></param> /// <param name="enqapex"></param> /// <param name="enqorg"></param> /// <param name="enqdest"></param> public void Enqueue(ref Otri enqtri, double minedge, Vertex enqapex, Vertex enqorg, Vertex enqdest) { // Allocate space for the bad triangle. BadTriangle newbad = new BadTriangle(); newbad.poortri = enqtri; newbad.key = minedge; newbad.triangapex = enqapex; newbad.triangorg = enqorg; newbad.triangdest = enqdest; Enqueue(newbad); }
/// <summary> /// Set the destination of the segment that includes the subsegment. /// </summary> public void SetSegDest(Vertex ptr) { seg.vertices[3 - orient] = ptr; }
/// <summary> /// Set the origin of the segment that includes the subsegment. /// </summary> public void SetSegOrg(Vertex ptr) { seg.vertices[2 + orient] = ptr; }
/// <summary> /// Set destination of a subsegment. /// </summary> public void SetDest(Vertex ptr) { seg.vertices[1 - orient] = ptr; }
/// <summary> /// Set the origin or destination of a subsegment. /// </summary> public void SetOrg(Vertex ptr) { seg.vertices[orient] = ptr; }
// 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); }
void Triangulate(out List <int[]> indices, out List <Vertex[]> vertices) { indices = new List <int[]>(); vertices = new List <Vertex[]>(); if (polygons == null) { return; } int pointIndex = 0; var inputGeometry = new InputGeometry(); SearchTree(inputGeometry, ref pointIndex, tree); var mesh = new Mesh(); mesh.Triangulate(inputGeometry); int highestIndex = 0; var currentIndices = new List <int>(); var currentVertices = new List <Vertex>(); var indexDict = new Dictionary <int, int>(); foreach (Triangle triangle in mesh.Triangles) { bool indexOverflow = currentIndices.Count + 3 > Renderer.IndexBufferSize; if (indexOverflow) { indices.Add(currentIndices.ToArray()); vertices.Add(currentVertices.ToArray()); currentIndices.Clear(); currentVertices.Clear(); indexDict.Clear(); highestIndex = 0; } int[] newIndices; List <Vertex> newVertices = IndexVertices(triangle, indexDict, ref highestIndex, out newIndices); if (currentVertices.Count + newVertices.Count > Renderer.VertexBufferSize) { indices.Add(currentIndices.ToArray()); vertices.Add(currentVertices.ToArray()); currentIndices.Clear(); currentVertices.Clear(); indexDict.Clear(); highestIndex = 3; newIndices[0] = 0; newIndices[1] = 1; newIndices[2] = 2; indexDict.Add(triangle.P0, 0); indexDict.Add(triangle.P1, 1); indexDict.Add(triangle.P2, 2); newVertices.Clear(); TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0); TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1); TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2); newVertices.Add(new Vertex((float)vertex0.X, (float)vertex0.Y)); newVertices.Add(new Vertex((float)vertex1.X, (float)vertex1.Y)); newVertices.Add(new Vertex((float)vertex2.X, (float)vertex2.Y)); } currentIndices.AddRange(newIndices); currentVertices.AddRange(newVertices); } indices.Add(currentIndices.ToArray()); vertices.Add(currentVertices.ToArray()); }
/// <summary> /// Set Apex /// </summary> public void SetApex(Vertex ptr) { triangle.vertices[orient] = ptr; }
/// <summary> /// Set Destination /// </summary> public void SetDest(Vertex ptr) { triangle.vertices[minus1Mod3[orient]] = ptr; }
/// <summary> /// Set Origin /// </summary> public void SetOrg(Vertex ptr) { triangle.vertices[plus1Mod3[orient]] = ptr; }