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; }
public static Vector3 getNearestPointOnMesh(Vector3 pt, MeshFilter mf) { Mesh mesh = mf.mesh; VertTriList vt = new VertTriList(mesh); Vector3[] verts = mesh.vertices; KDTree kd = KDTree.MakeFromPoints(verts); return(getNearestPointOnMesh(pt, verts, kd, mesh.triangles, vt)); }
public static Vector3 NearestPointOnMesh(Vector3 pt, Vector3[] verts, int[] tri, VertTriList vt) { int num = -1; float num2 = 1E+08f; for (int i = 0; i < verts.Length; i++) { float sqrMagnitude = (verts[i] - pt).sqrMagnitude; bool flag = sqrMagnitude >= num2; if (!flag) { num = i; num2 = sqrMagnitude; } } bool flag2 = num == -1; Vector3 result; if (flag2) { result = Vector3.zero; } else { int[] array = vt[num]; Vector3 vector = Vector3.zero; num2 = 1E+08f; for (int j = 0; j < array.Length; j++) { int num3 = array[j] * 3; Vector3 vector2 = verts[tri[num3]]; Vector3 vector3 = verts[tri[num3 + 1]]; Vector3 vector4 = verts[tri[num3 + 2]]; Vector3 vector5 = NearestPointTest.NearestPointOnTri(pt, vector2, vector3, vector4); float sqrMagnitude2 = (pt - vector5).sqrMagnitude; bool flag3 = sqrMagnitude2 >= num2; if (!flag3) { vector = vector5; num2 = sqrMagnitude2; } } result = vector; } return(result); }
public static Vector3 lerpNearest(Vector3 from, MeshFilter to, float t) { //Linearly interpolates between given point and nearest point on mesh by fraction t Mesh mesh = to.mesh; VertTriList vt = new VertTriList(mesh); Vector3 objSpacePt = from; // to.transform.InverseTransformPoint(from); Vector3[] verts = mesh.vertices; KDTree kd = KDTree.MakeFromPoints(verts); Vector3 meshPt = NearestPointOnMesh.getNearestPointOnMesh(objSpacePt, verts, kd, mesh.triangles, vt); Vector3 closest = meshPt; //= to.transform.TransformPoint(meshPt); Debug.Log("selected:" + from.ToString() + " nearest:" + closest.ToString()); Vector3 towards = Vector3.Lerp(from, closest, t); Debug.Log("lerp point" + towards.ToString()); return(towards); }
public static Vector3 NearestPointOnMesh(Vector3 pt, Vector3[] verts, 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 = -1; float nearestSqDist = float.MaxValue; for (int i = 0; i < verts.Length; i++) { float sqDist = (verts[i] - pt).sqrMagnitude; if (sqDist < nearestSqDist) { nearest = i; nearestSqDist = sqDist; } } // Get the list of triangles in which the nearest vert "participates". VertList nearTris = vt.list[nearest]; Vector3 nearestPt = Vector3.zero; 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); }
public static Vector3 getNearestPointOnMesh(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". int[] nearTris = vt[nearest]; Vector3 nearestPt = Vector3.zero; float nearestSqDist = 100000000f; for (int i = 0; i < nearTris.Length; i++) { int triOff = nearTris[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); }