I am making a system, where shapes can be made at runtime, and adopt vertices and triangles to create a procedural shape. These shapes are defined by the users mouse movement. I am not understanding if there is a foolproof way to create the tris, so that they can create the tris in the places, even when shapes are different i.e. if a shape is a kite, triangle, or something more abstract. I know that the triangle array should contain a length divisible by 3, but cannot see a one size fits all solution to the problem. Any help would be appriciated. Code (CSharp): void CreateShape() { GameObject go = new GameObject(); go.AddComponent<MeshFilter>(); go.AddComponent<MeshRenderer>(); go.transform.localScale *= 2.5f; Mesh mesh = new Mesh(); Vector3[] vertices = new Vector3[selectedNodes.Count]; int[] tris = new int[selectedNodes.Count]; for(int i = 0; i < vertices.Length; i++) { vertices[i] = new Vector3(selectedNodes[i].xPos, selectedNodes[i].yPos, 0f); tris[i] = i; } mesh.vertices = vertices; mesh.triangles = tris; go.GetComponent<MeshFilter>().mesh = mesh; }
A square is 2 tris (your tri list will have six indices), a pentagon is 3 tris (9 indixes), etc This is called 'Triangulation' and there are dozens of resources available on performing it
It sounds a bit like you want the user to draw a series of points and then wrap a convex hull around that (and triangulate it internally). A good algorithm for that is Delaunay triangulation (http://www.qhull.org/) Here's a javascript example of convex hull creation: https://www.nayuki.io/page/convex-hull-algorithm And a javascript example of delaunay triangulation: https://github.com/mapbox/delaunator