- Home /
create a polygon mesh from a set of points
I have a set of points (empty objects), and I want to create a polygon based on their position. I tried the Triangulator class but when I use more than 4 points, I get weird results. Is there any other way to accomplish this?
I have also been looking on creating a spline of empty objects and create the mesh based on them and my desired width from center, but again with no luck.
I am looking for a solution for any of the above approaches. Here is a screenshot of the points.
do you need to use it in runtime on in editor ?
actually the secont question ... do you need to make the road only? like quads only ?
I dont need the mesh to be generated in editor, just in runtime. But I will set up the points in editor like the picture.
@komodor Actually I need to re-do the road system, because the 3D designer that did the city, did ALL the roads as one objects, so I can not apply AI and road detection easily. I need to know on which specific road my car is.
and the road/quad ... is it like ... 2 lines of points you want to connect with triangles (2 triangles make the road block panel) ? because this is pretty simple to generate, even crossroards, but i am not sure with complicated shapes
well this is what I am trying to accomplish, but the shape of the 2 lines will not be always a rectangle. The points will shape polygons in the shape of roads (just without curves)
Answer by komodor · Dec 05, 2014 at 02:40 PM
well:
http://docs.unity3d.com/ScriptReference/Mesh.html
this is what u need to master, generally you have to generate the mesh by defining points, triangles, colors and uvs (i guess the last two aren't so important now and you will understand it)
vertices are the points you have (coordinates are local to the mesh pivot)
triangles is 3x longer then vertices array and it's array of INDEXES from the vertices array (not values)
and then u just give those two arrays to the mesh (after clearing the mesh or creating new)
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)};
mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
mesh.triangles = new int[] {0, 1, 2};
Thank you, this is really helpful. These are all very new to me but I think I understand now. Well, I will give it a try.