I have previously written an article, irregular graphic UV mapping, (http://blog.csdn.net/itolfn/article/details/17240131) used triangulation algorithm, but the algorithm is not complete, There is one rule: optimality: the diagonal of a convex quadrilateral formed by any two adjacent triangles if interchangeable, the smallest angle in the six inner corners of two triangles does not become larger. is to take four of the maximum triangular angle of the triangle to join the diagonal, grouped two triangles, but sometimes not the one you want to the graph, as shown:
Triangulation algorithm will work out the two triangles, ABC and ACD, but we need ABD and BCD, this time can not be used, I study the triangulation is not deep, according to his law to calculate is to take the second figure, who knows please tell the younger brother, later I abandoned the algorithm, rewrite a set, The algorithm of the area partition, so that solves all my problems, any irregular polygon will make the UV paste exactly
Using System.Collections;
Using System.Collections.Generic; public class trianglesubdivision:monobehaviour{///Triangular Division principle///each partition of a triangle, judging the area of the remaining polygons divided into new triangles and partitions and whether equal to the area of the pre-segmented polygon, if equal,
The partition is valid, continues to divide, or skips a vertex to continue judging the next triangle, looping until the remaining polygon is a triangle.
public static int[] Triangulatepolygon (vector2[] xzofvertices, bool is3d) {int vertexcount = xzofvertices.length;
list<triangleobj> trianglelist = new list<triangleobj> ();
list<vector2> verticeslist = new list<vector2> ();
for (int i=0;i<vertexcount;i++) {Verticeslist.add (new Vector2 (XZOFVERTICES[I].X,XZOFVERTICES[I].Y));
} if (vertexcount<3) return null;
int testindex=0;
while (verticeslist.count>=3&&testindex<100) {for (int i = 0; i < Verticeslist.count; i++) {
testindex++;
If the array has only 3 nodes remaining.
if (verticeslist.count==3) {triangleobj nobj = new Triangleobj (verticeslist[0], verticeslist[1], VerticesList[2]);
Trianglelist.add (Nobj);
Verticeslist.removeat (1); BrEak
}//All corner-point staging array list<vector2> surplusverticeslist = new list<vector2> ();
Surplusverticeslist.addrange (verticeslist);
if (i+2<verticeslist.count) {//gets 3 points consisting of a 3-angle vector2[] Trianglepoint = new Vector2[3];
Trianglepoint[0] = Verticeslist[i];
TRIANGLEPOINT[1] = verticeslist[i+1];
TRIANGLEPOINT[2] = verticeslist[i+2];
Removes the middle point used by the triangle, and the remaining set of polygon points.
Surplusverticeslist.removeat (i+1);
vector2[] surpluspoints = new Vector2[surplusverticeslist.count];
for (int m=0;m<surplusverticeslist.count;m++) {surpluspoints[m]=surplusverticeslist[m];
}//Remove front polygon point set vector2[] allpoints = new Vector2[verticeslist.count];
for (int n = 0; n < verticeslist.count; n++) {allpoints[n]=verticeslist[n]; the bool Iscrose = false;//Determines whether the two edges of the polygon intersect for (int ii_1 = 0;ii_1<surpluspoints.length;ii_1++) {for (i
NT Ii_2 = 0;ii_2<surpluspoints.length;ii_2++) { if (Ii_1 < surpluspoints.length-1 && Ii_2 < surpluspoints.length-1) {if (surplusp oints[ii_1].x = = surpluspoints[ii_2].x && surpluspoints[ii_1].y = = surpluspoints[ii_2].y) | | (surpluspoints[ii_1+1].x = = surpluspoints[ii_2].x && surpluspoints[ii_1+1].y = = surpluspoints[ii_2].y) | | (surpluspoints[ii_1].x = = surpluspoints[ii_2+1].x && surpluspoints[ii_1].y = = surpluspoints[ii_2+1].y) | |
(surpluspoints[ii_1+1].x = = surpluspoints[ii_2+1].x && surpluspoints[ii_1+1].y = = surpluspoints[ii_2+1].y))
{continue; } if (Gameobject.find ("Initial"). Getcomponent<sharemethods> (). Checkcrose (surpluspoints[ii_1],surpluspoints[ii_1+1],surpluspoints[ii_2],surpluspoints[ii_2+1]) && Gameobject.find ("Initial"). Getcomponent<sharemethods> ().
Checkcrose (Surpluspoints[ii_2],surpluspoints[ii_2+1],surpluspoints[ii_1],surpluspoints[ii_1+1])) {Iscrose = true;
Break
}}} if (Iscrose) {break;
}}//To determine the area of the remaining polygons divided into new triangles and splits, and is equal to the area of the pre-segmented polygon? if (Mathf.abs (Trpolygonarea (trianglepoint,is3d) +trpolygonarea (surpluspoints,is3d)-trpolygonarea (AllPoints,is3D) <=0.001f&&!iscrose) {//means the partition is valid, continue dividing triangleobj tobj = new Triangleobj (Verticeslist[i],
VERTICESLIST[I+1], verticeslist[i+2]);
Trianglelist.add (Tobj);
Verticeslist.removeat (i+1);
i--;
Break
}else{//Otherwise skips a vertex to continue judging the next triangle. Place the first point at the end of the array.
Vector2 zeropoint = new Vector2 (VERTICESLIST[I].X,VERTICESLIST[I].Y);
Verticeslist.removeat (i);
Verticeslist.add (Zeropoint);
Break
}}}} int[] triangles = new int[3 * Trianglelist.count]; for (int ii1 = 0; II1 < TriaNglelist.count;
ii1++) {Triangleobj tempobj = Trianglelist[ii1];
Triangles[3 * II1 + 1] = Indexofobjfromlist (tempobj.p2,xzofvertices);
if (is3d) {triangles[3 * ii1+2] = indexofobjfromlist (tempobj.p1,xzofvertices);
Triangles[3 * II1] = indexofobjfromlist (tempobj.p3,xzofvertices);
} else {triangles[3 * II1] = indexofobjfromlist (tempobj.p1,xzofvertices);
Triangles[3 * Ii1+2] = indexofobjfromlist (tempobj.p3,xzofvertices);
}} return triangles;
}/////Get polygon area by point Static float Trpolygonarea (vector2[] Points,bool is3d) {float areas = 0; if (is3d) {area = Gameobject.find ("Initial"). Getcomponent<sharemethods> (). Polygonarea (null,points);//area formula algorithm} else {areas = Gameobject.find ("Initial"). Getcomponent<sharemethods> ().
Polygonarea (null,points)/1000000;
} return area;
} static int indexofobjfromlist (Vector2 point,vector2[] points) {int index = 0; for (int i=0;i<points. length;i++) {
Vector2 Temp=points[i];
if (Mathf.abs (point.x-temp.x) <0.001f&&mathf.abs (POINT.Y-TEMP.Y) <0.001f) {index=i;
Break
}} return index;
}} struct Triangleobj {public Vector2 p1;
Public Vector2 p2;
public Vector2 P3;
Public Triangleobj (Vector2 point1, Vector2 Point2, Vector2 point3) {p1 = point1; p2 = point2; p3 = Point3;
}
}
This method does not change, just the algorithm I changed, or this class, or pass all your points, will return to your mesh.triangles, so that the perfect to make their own Mesh according to the point of their own desired graphics UV