Comments and answers for "how to Find the vertices of each edge on mesh"
http://answers.unity.com/questions/566779/how-to-find-the-vertices-of-each-edge-on-mesh.html
The latest comments and answers for the question "how to Find the vertices of each edge on mesh"Answer by markus-weiss
http://answers.unity.com/answers/1584393/view.html
Yes this question is from 2013, but here is a very short solution:
Copy all vertices of ur Mesh to a List.
Now remove all duplicate Vector3
The RemoveFunction is from here:
https://answers.unity.com/questions/154923/how-do-i-scan-a-list-and-remove-all-duplicates.html
using System.Linq;
public class Vertices : MonoBehaviour {
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
List<Vector3> VertexList = new List<Vector3>();
foreach(Vector3 vertex in vertices)
{
VertexList.Add(vertex);
}
VertexList = VertexList.Distinct().ToList();
}
}Thu, 27 Dec 2018 15:38:39 GMTmarkus-weissComment by IgorAherne on IgorAherne's answer
http://answers.unity.com/comments/1037837/view.html
thank you!Sat, 29 Aug 2015 09:47:36 GMTIgorAherneComment by Pthorndycraft on Pthorndycraft's answer
http://answers.unity.com/comments/867451/view.html
I love this answer! Seems so simple now you have described the theory, the edges of a mesh are ones which are not shared by other tris... Simple but effective. Thank you so much for this answer, this will help me out greatly... Now to work out how to repeat a texture along a line...Fri, 02 Jan 2015 16:37:44 GMTPthorndycraftAnswer by ArkaneX
http://answers.unity.com/answers/566854/view.html
Firstly you need to create a list of edges (edge could be a struct). Then you have to iterate through `mesh.triangles`, taking 3 elements (vertices) at a time, and then check if any pair of these vertices should be added to your list.
Sample code below, but please note that it's just a very basic solution, not optimized at all. Its performance degrades significantly with triangles count increase.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class EdgesTest : MonoBehaviour
{
public struct Edge
{
public Vector3 v1;
public Vector3 v2;
public Edge(Vector3 v1, Vector3 v2)
{
if (v1.x < v2.x || (v1.x == v2.x && (v1.y < v2.y || (v1.y == v2.y && v1.z <= v2.z))))
{
this.v1 = v1;
this.v2 = v2;
}
else
{
this.v1 = v2;
this.v2 = v1;
}
}
}
void Start()
{
var mesh = this.GetComponent<MeshFilter>().mesh;
var edges = GetMeshEdges(mesh);
//for (int i = 0; i < edges.Length; i++)
//{
// print(i + ": " + edges[i].v1 + ", " + edges[i].v2);
//}
}
private Edge[] GetMeshEdges(Mesh mesh)
{
HashSet<Edge> edges = new HashSet<Edge>();
for (int i = 0; i < mesh.triangles.Length; i += 3)
{
var v1 = mesh.vertices[mesh.triangles[i]];
var v2 = mesh.vertices[mesh.triangles[i + 1]];
var v3 = mesh.vertices[mesh.triangles[i + 2]];
edges.Add(new Edge(v1, v2));
edges.Add(new Edge(v1, v3));
edges.Add(new Edge(v2, v3));
}
return edges.ToArray();
}
}Thu, 31 Oct 2013 11:33:06 GMTArkaneXAnswer by reefwirrax
http://answers.unity.com/answers/566793/view.html
this is a method that takes about 20 lines but it's very simple...
STEP 1
Get the array of mesh.triangles,
STEP 2run through the entire list in a loop, and return 3 sets of edges, made of 2 vertices, for each triangle and put them in a new array of called mesh.edges, preferably, keep it orderly so that the point with the smallest X, Y, Z is the 1st points of the pair in the list for each edge, that means you have to compare XY and Z for each edge prior to storing it in the array.
the array of mesh.edges is going to be the same style the mesh.triangles, except it references the points in the mesh.vertex array in pairs.
if you're not worried about having duplicate edges, they need to compare them and order them, just convert the mesh triangles into a new array of edges with 6 vertices per triangle.
by this time you have about 15 lines. mostly the compare XYZ logic in the loop. The rest is 5 lines.
step 3
after that you can delete duplicates because they are ordered so that the 1st point is always the lowest XYZ, so you can easily compare the 1st and 2nd points to all the other ones in the array. there should be a lot of duplicates because triangles share edges.
It's a good learning project I have done stuff seminar. use debug.drawline if you want, draw the edges using one of the points and the Vector3 the 2nd one.
remember that some models don't share vertices among triangles and 3 times more vertices in the array, duplicates. It doesn't really matter but it's good to know, please vote at my answer if it's useful my feedback is very low, my last profile was the thousand! I can't give people,karma.Thu, 31 Oct 2013 10:57:42 GMTreefwirraxAnswer by wheezy602
http://answers.unity.com/answers/566817/view.html
why not just texture the objects?
it would make it a lot more graphically sound, would it not?Thu, 31 Oct 2013 10:57:15 GMTwheezy602Comment by ArkaneX on ArkaneX's comment
http://answers.unity.com/comments/566829/view.html
OP was asking for edge vertices, not for triangle vertices.Thu, 31 Oct 2013 10:18:33 GMTArkaneXComment by LijuDeveloper
http://answers.unity.com/comments/566821/view.html
This code for finding all vertices
using UnityEngine;
using System.Collections;
public class Example2 : MonoBehaviour
{
void Start ()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float [][] verticesArray;// = new float [vertices.Length][3];
verticesArray = new float [vertices.Length][];
int vertexCount = mesh.vertexCount;
int i = 0;
while (i < vertices.Length)
{
verticesArray[i] = new float [3];
verticesArray[i][0] = vertices[i].x;// X vertex
verticesArray[i][1] = vertices[i].y;// Y vertex
verticesArray[i][2] = vertices[i].z;// Z vertex
Debug.Log (" verticesArray["+i+"] X ="+verticesArray[i][0] +" verticesArray["+i+"] Y ="+ verticesArray[i][1]+" verticesArray["+i+"] Z ="+verticesArray[i][2]+" vertexCount = " +vertexCount );
i++;
}
}
}Thu, 31 Oct 2013 10:00:59 GMTLijuDeveloperComment by reefwirrax on reefwirrax's answer
http://answers.unity.com/comments/566795/view.html
he answer is waiting approval by a moderator. OMGThu, 31 Oct 2013 08:02:10 GMTreefwirrax