int findClosestVertexToMouse(MeshDeformer mesh, Camera cam) { Ray cameraRay = mainCamera.ScreenPointToRay(curMousePosition); Vector3 start = filter.transform.InverseTransformPoint(cameraRay.origin); Vector3 end = filter.transform.InverseTransformPoint(cameraRay.origin + (cameraRay.direction * mainCamera.farClipPlane)); int closestVertex = -1; float closestSqrDistance = 100000.0f; for (int i = 0; i < calibrationMesh.distortedMeshVertices.Length; i++) { float currentSqrDistance = SqrDistanceToSegment(calibrationMesh.distortedMeshVertices[i], start, end); if (currentSqrDistance < closestSqrDistance) { closestVertex = i; closestSqrDistance = currentSqrDistance; } } return(closestVertex); }