// You don't usually need to call this - it's just to assist the implementation of the constructors. public void Init(int[] tri, int numVerts) { // First, go through the triangles, keeping a count of how many times each vert is used. int[] counts = new int[numVerts]; for (int i = 0; i < tri.Length; i++) { counts[tri[i]]++; } // Initialise an empty jagged array with the appropriate number of elements for each vert. //list = new int[numVerts][]; list = new VertList[numVerts]; //[]; for (int i = 0; i < counts.Length; i++) { list[i] = new VertList(); list[i].t = new int[counts[i]]; } // Assign the appropriate triangle number each time a given vert is encountered in the triangles. for (int i = 0; i < tri.Length; i++) { int vert = tri[i]; list[vert].t[--counts[vert]] = i / 3; } }
public static Vector3 NearestPointOnMesh(Vector3 pt, Vector3[] verts, KDTree vertProx, int[] tri, VertTriList vt) { // First, find the nearest vertex (the nearest point must be on one of the triangles // that uses this vertex if the mesh is convex). int nearest = vertProx.FindNearest(pt); // Get the list of triangles in which the nearest vert "participates". VertList nearTris = vt.list[nearest]; Vector3 nearestPt = Vector3.zero; float nearestSqDist = float.MaxValue; for (int i = 0; i < nearTris.t.Length; i++) { int triOff = nearTris.t[i] * 3; Vector3 a = verts[tri[triOff]]; Vector3 b = verts[tri[triOff + 1]]; Vector3 c = verts[tri[triOff + 2]]; Vector3 possNearestPt = NearestPointOnTri(pt, a, b, c); float possNearestSqDist = (pt - possNearestPt).sqrMagnitude; if (possNearestSqDist < nearestSqDist) { nearestPt = possNearestPt; nearestSqDist = possNearestSqDist; } } return(nearestPt); }
// You don't usually need to call this - it's just to assist the implementation of the constructors. public void Init(int[] tri, int numVerts) { // First, go through the triangles, keeping a count of how many times each vert is used. int[] counts = new int[numVerts]; for ( int i = 0; i < tri.Length; i++ ) counts[tri[i]]++; // Initialise an empty jagged array with the appropriate number of elements for each vert. //list = new int[numVerts][]; list = new VertList[numVerts]; //[]; for ( int i = 0; i < counts.Length; i++ ) { list[i] = new VertList(); list[i].t = new int[counts[i]]; } // Assign the appropriate triangle number each time a given vert is encountered in the triangles. for ( int i = 0; i < tri.Length; i++ ) { int vert = tri[i]; list[vert].t[--counts[vert]] = i / 3; } }