Ok, i’ve finnaly made a tool to patch mesh and remove small triangles… but i should remove all triangles that are smaller than Vector3.kEpsilon * 1000.0f, but with this params it’s also do not work’s correctly… ammm… look at my steps… this post would be to long.
Before we got started, i post some simple snippets of scripts:
- Script to check distances from each to each point:
void CheckDistances()
{
Mesh m = gameObject.GetComponent().mesh;
Vector3[] points = m.vertices;
for (int i = 0; i < points.Length; i++)
{
for (int j = i + 1; j < points.Length; j++)
{
float d = Vector3.Distance(points*, points[j]);*
* if (d < Vector3.kEpsilon)*
* {*
* Debug.Log("I: "+i+" J: "+ j);*
* }*
* }*
* }*
* }*
**
2) Method i used to check if my triangles are smaller than requared size:
- bool checkTriangleForPatch(int index, ref Vector3 vertices, ref int triangles)*
- {*
-
bool result = false;*
_ Vector3 p1 = vertices[triangles[index3]];_
_ Vector3 p2 = vertices[triangles[index3+1]];_
_ Vector3 p3 = vertices[triangles[index*3+2]];_
-
float a = Vector3.Distance(p1, p2);*
-
float b = Vector3.Distance(p2, p3);*
-
float c = Vector3.Distance(p3, p1);*
-
float p = (a+b+c) / 2.0f;*
_ float r = Mathf.Sqrt(((p-a)(p-b)(p-c)) / p); //радиус вписанной окружности_
_ if (r*2 < Vector3.kEpsilon)_
-
{ *
-
result = true;*
-
}*
-
return result;*
-
}*
3) Main function to patch mesh:
int next = FindNextToPatch(0, ref vertices, ref triangles); *
if (next == -1)*
{*
result = PatchMeshState.NotPatched;*
return result;*
}*
while (next != -1)*
{*
List sharedTriangles = GetSharedTriangles(next, ref vertices, ref triangles);*
if (sharedTriangles.Count > 0)*
{ *
Edge longestEdge = GetLongestEdge(next, ref vertices, ref triangles);*
if (longestEdge.Length > Vector3.kEpsilon)*
{ *
ClipToLongestEdge(next, longestEdge, sharedTriangles, ref vertices, ref triangles); *
}*
else*
{*
DeleteTriangleAndFillEmptySpace(next, ref sharedTriangles, ref vertices, ref triangles);*
}*
}*
next = FindNextToPatch(0, ref vertices, ref triangles); *
}*
Ok let’s took for example a capsule to look how this tool is working(i’ve edited this tool to remove triangle at id):
![view large][1]
[See more][2]
Now we patch this triangle via ClipToLongestEdge:
![alt text][3]
[See more][4]
And here we patch same triangle via DeleteTriangleAndFillEmptySpace:
![alt text][5]
[see more][6]
Ok… next step is checking which points are very close to each other in mesh we are interested in:
I: 37 J: 70
I: 41 J: 71
i → first point; j → second point; distance between them is less than vector3.kEpsilon (it’s like doubles)
And now lets look to these points to be sure that they are close enought(i post one picture, becouse groups at the same positions):
37:
x: -3.504073;
y: 0.7674599;
z: -3.379824;
70:
x: -3.504073;
y: 0.7674594;
z: -3.379824;
41:
x: 0.4926167;
y: 0.6595895;
z: -0.7416723;
71:
x: 0.4926167;
y: 0.65959;
z: -0.7416723’
Getting control mesh stats:
144 → triangles
74 → vertices
And checking for bad triangles (if inradius < Vector3.kEpsilon):
Bad triangle id: 12
Bad triangle id: 13
Bad triangle id: 14
Bad triangle id: 15
Bad triangle id: 38
Bad triangle id: 82
Bad triangle id: 83
Bad triangle id: 134
Bad triangle id: 135
Num of bad triangles: 9
Patch it with vector3.kEpsilon:
Patched.
Patched triangles count: 12
(why numbers are different is becouse in ClipToLongestEdge, we remove triangle with shared edge equals to shortest edge of bad triangle, if longest is bigger than vector3.kEpsilon, and in secont method, if all edges are less than vector3.kEpsilon, we remove all sharedEdge triangles, and badTriangle (also we remove their bad vertices, in first 1, in second 2 vertices.) )
Ok… now let’s check for distances:
non of vertices is placed near other one at distance is less then Vector3.kEpsilon;
ok, let’s check for inradiuses:
Num of bad triangles: 0
and check for stats:
132 ← triangles;
68 ← vertices;
… ok… adding mesh collider and make it convex:
![alt text][7]
[See more][8]
WTF ? I guess, this distance is too short, let’s do… vector3.kEpsilon*10, for patch;
NotPatched.
132 ← triangles;
68 ← vertices;
Ok, let’s try vector3.kEpsilon*100:
Patched
Patched triangles count: 4
stats:
128 ← triangles;
66 ← vertices;
And check for distances and inradius to kEpsilon*100.0f:
Num of bad triangles: 0
triing mesh collider:
fck… not working… okey… making min distance to kEpsilon1000.0f ( 0.01f):
Bad triangle id: 19
Bad triangle id: 25
Bad triangle id: 27
Bad triangle id: 40
Bad triangle id: 48
Bad triangle id: 52
Bad triangle id: 60
Bad triangle id: 61
Bad triangle id: 62
Bad triangle id: 63
Bad triangle id: 65
Bad triangle id: 69
Bad triangle id: 76
Bad triangle id: 80
Bad triangle id: 82
Bad triangle id: 85
Bad triangle id: 88
Bad triangle id: 92
Bad triangle id: 97
Bad triangle id: 98
Bad triangle id: 99
Bad triangle id: 100
Bad triangle id: 102
Bad triangle id: 106
Bad triangle id: 114
Bad triangle id: 120
Bad triangle id: 121
Bad triangle id: 125
Bad triangle id: 126
Num of bad triangles: 29
this tiangles indiametr is less then 0.01 and greater then 0.001 (becouse other we patched in previous steps);
patching it:
Patched.
Patched triangles count: 48
80 ← triangles;
42 ← vertices;
trying convex collider:
![alt text][9]
[See more][10]
Yeah… it’s finally works… but wait… Not all points inside a hull… What’s NOW ?
and if we slightly change scale, then it works fine:
![alt text][11]
[See more][12]
btw, on every step i removed and added mesh collider to make it’s sharedMesh update
and resulting distances:
----------
0.1624586 0.1407853 0.1527793
0.0854082 0.1262902 0.1472641
0.1262902 0.09064174 0.0431602
0.1387644 0.126594 0.12164
0.126594 0.01682952 0.1342831
0.1152456 0.1422343 0.1624586
0.1422343 0.05491636 0.0964755
----------
see more at: http://pastebin.com/DVnBRb82
What’s wrong now ?
http://forum.unity3d.com/attachment.php?attachmentid=41644&d=1355047169 ← package with all this meshes
[1]: http://s004.radikal.ru/i205/1212/98/60aa041e54e8t.jpg
[2]: http://radikal.ru/F/s004.radikal.ru/i205/1212/98/60aa041e54e8.png
[3]: http://s019.radikal.ru/i635/1212/fc/b348a996a26et.jpg
[4]: http://radikal.ru/F/s019.radikal.ru/i635/1212/fc/b348a996a26e.png
[5]: http://s017.radikal.ru/i443/1212/66/c65a51a69d6at.jpg
[6]: http://radikal.ru/F/s017.radikal.ru/i443/1212/66/c65a51a69d6a.png
[7]: http://s018.radikal.ru/i527/1212/be/d52268679afet.jpg
[8]: http://radikal.ru/F/s018.radikal.ru/i527/1212/be/d52268679afe.png
[9]: http://s020.radikal.ru/i700/1212/12/f8d4d64f6393t.jpg
[10]: http://radikal.ru/F/s020.radikal.ru/i700/1212/12/f8d4d64f6393.png
[11]: http://s006.radikal.ru/i215/1212/e3/c28c72b5ffc5t.jpg
[12]: http://radikal.ru/F/s006.radikal.ru/i215/1212/e3/c28c72b5ffc5.png
Any idea ?
– XienDev