Answers for "Lower-level way to find NEARBY TRIANGLES on the mesh? (Answered.)"
http://answers.unity.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html
The latest answers for the question "Lower-level way to find NEARBY TRIANGLES on the mesh? (Answered.)"Answer by Trionet
http://answers.unity.com/answers/1176185/view.html
I've done this in a project before. I have a parallell list of MeshLinks I call them. They know all of their 3 adjancent triangles. With that you can also do a gathering of all triangles sharing a vertex through the neighbours neighbour and so on.
By also having a is_found boolean in the MeshLink you can check where you were and find any n adjancent triangles in O(n) time. You must ofcourse buffer the MeshLinks before hand. But then it is good to go. By some complex linear algorithms in searching adjancents you may also extend the mesh by splitting one in to 3 or 2 in to 4 without ever having to go through the whole mesh again.
In other words. Have you considered buffering all the adjancency of a mesh? It shouldn't be a performance issue then.Sun, 24 Apr 2016 17:11:59 GMTTrionetAnswer by Tasarran
http://answers.unity.com/answers/194488/view.html
<p>There is no quick and easy way to do it, the only way is by comparing spatial coordinates, and this is why:</p>
<p>It might not be possible to explain w/o a picture, if I need to elaborate, I will.</p>
<p>In that cube example, there are two shared verts per square side, and that is ALL the shared points.
The corner points are not shared between faces (squares), only between triangles on the same square.
Any edge that is hard (such as the actual edges of the square faces) will not share vertices.
In a sphere without hard edges, or UV boundaries, ALL vertices WILL be shared by at least one other triangle. Same thing goes for a mesh with no hard edges or UV boundaries.</p>
<p>If you have a cube in Unity, you have 12 triangles. They are paired in two triangles per face, each sharing two verts.
Each side will have two triangles, not sharing verts with any of the other faces, except the two verts on the other triangle in the pair.
The vertices from the separate sides will not share vertices, even though they are adjacent.</p>
<p>The only way to find out that the triangle on the other side of the hard border is adjacent, is to compare the actual coordinates of the points...</p>
<p>As you can see from the stats that Aldo posted (which are correct), if you have a cube, you basically have six squares that are INDEPENDANT of each other and share NO vertexes between them, and each of those squares is made up of two triangles which share two vertices each.
So, Square One has Triangle One, made up of Vert 0,1,& 2. It also has Triangle Two, with Verts 1 (shared), 2(shared), & 3.
This would be repeated all around: Square Two has Triangle Three (4,5,6), and Triangle Four (5,6,7), etc.
Until you get to Square Six, with Triangle Eleven (20, 21, 22) and Triangle Twelve (21,22,23).</p>
<p>Twenty four vertices, twelve shared.</p>
<p>The point is, even though vertices lie on the same exact coordinate as another point, they are no necessarily shared.</p>
<p>If you move one of those corner vertices, you will rip the cube open, because the vertices on the other two squares are NOT shared, even though they share the same coordinates. You'd have to move all three corners of the involved squares to not rip the cube.</p>
<p>It's not like the real world where you can point to the corner of a cube and say "That point is common to all three sides."</p>
<p>It just doesn't work that way in Unity.</p>
<p>In the end, using search trees (one example of which is discussed above), or other sorts of data-manipulation techniques can help you speed up the process, but when you get down to it, the only way is brute-force looping and comparing.</p>Mon, 12 Dec 2011 18:09:48 GMTTasarranAnswer by Statement
http://answers.unity.com/answers/193770/view.html
**Spatial culling** might help you out here. There are many ways of doing this, but one of the more popular ones is probably the [Octree][1]. The idea is to store information in the tree about which triangles belong to which nodes. This obviously has a setup phase, but once the Octree is populated, getting the actual nearby triangles is much easier. Say you have triangle #379, and you want to find triangles that are close to it; you'd check which nodes that #379 is overlapping and you'll find all *potential* matches. So you'll get a *subset* of the triangles to work with, probably a few orders of magnitude less, which should quicken up your neighbor tests considerably.
You [might be lucky][2] and find some existing Octree implementation, but making your own can be a fun experience.
[1]: http://en.wikipedia.org/wiki/Octree
[2]: https://www.google.com/search?q=Unity+OctreeSat, 10 Dec 2011 00:54:59 GMTStatementAnswer by aldonaletto
http://answers.unity.com/answers/193769/view.html
Since neighbor triangles in a mesh share one or more vertex *(the vertex coordinates, not necessarily the vertex indexes!)*, one can loop over all triangles comparing their vertex coordinates to the desired triangle vertices - any match means that the triangle is a neighbor (not necessarily in the same plane):
var i: int = myTriangle * 3; // find the vertices of myTriangle...
var v1: Vector3 = vertices[triangles[i++]]; // and store them in
var v2: Vector3 = vertices[triangles[i++]]; // v1, v2 and v3
var v3: Vector3 = vertices[triangles[i]];
for (i = 0; i < triangles.length; i++){
// compare each vertex to v1, v2 and v3:
var v: Vector3 = vertices[triangles[i]];
if (v == v1 || v == v2 || v == v3){ // if any match found...
var nTri: int = i / 3; // get this triangle's number...
if (nTri != triNum){ // and if it's not myTriangle...
// triangle #nTri is neighbour of #myTriangle
}
}
}
NOTE: Different from the float equality operator (==), which only returns true if both operands are exactly the same, [Vector3 equality operator][1] returns true if both operands are close enough to be considered equal.
SPECIAL CASE: Some "well behaved" meshes (simple shapes, like Unity's cubes or planes) are constructed to avoid vertex duplication: if two or more triangles lie in the same face and share the same vertex, the vertex is stored only once. In these "well behaved" cases there's a much faster method to find neighbor triangles: loop over all triangles and compare their vertex indexes to the three ones of the main triangle - any match means that the triangle is a neighbor, and is in the same face:
var i: int = myTriangle * 3; // each triangle occupies 3 entries in the triangles array
var v1: int = triangles[i++]; // get v1, v2 and v3,
var v2: int = triangles[i++]; // the 3 vertex indexes of
var v3: int = triangles[i]; // triangle #myTriangle
for (i = 0; i < triangles.length; i++){
// compare each vertex index to v1, v2 and v3:
var v: int = triangles[i];
if (v == v1 || v == v2 || v == v3){ // if any common vertex found...
var nTri: int = i / 3; // find the triangle number...
if (nTri != myTriangle){ // and if it's diff from #myTriangle...
// triangle #nTri is neighbour of triangle #myTriangle
}
}
}
Int comparisons are way faster than Vector3 equality (which requires some internal distance evaluation). Obviously, this method is restricted to the "well behaved" cases above mentioned - if applied to a "badly behaved" mesh, some neighbors will not be found. If you're not sure about your meshes, it's better to compare the actual vertex coordinates using the first method.
[1]: http://unity3d.com/support/documentation/ScriptReference/Vector3-operator_eq.htmlSat, 10 Dec 2011 00:39:03 GMTaldonaletto