示例#1
0
        /// <summary>
        /// Returns the nearest point on the kd mesh. meshTransform is the transform of the mesh that the point should be searched for. This is needed for correctly rotating orienting the mesh data.
        /// </summary>
        /// <returns>The point on mesh.</returns>
        /// <param name="pt">Point.</param>
        /// <param name="verts">Verts.</param>
        /// <param name="vertProx">Vert prox.</param>
        /// <param name="tri">Tri.</param>
        /// <param name="vt">Vt.</param>
        public Vector3 FindNearestPointOnMesh(Vector3 point, Transform meshTransform)
        {
            //transform point to mesh local space
            point = meshTransform.InverseTransformPoint(point);

            //    First, find the nearest vertex (the nearest point must be on one of the triangles
            //    that uses this vertex if the mesh is convex).
            //  Since there can be multiple vertices on a single spot, we need to find the correct vert and triangle.
            FindNearestEpsilon(point, nearests);

            Vector3 nearestPt     = Vector3.zero;
            float   nearestSqDist = float.MaxValue;
            Vector3 possNearestPt;

            for (int i = 0; i < nearests.Count; i++)
            {
                //    Get the list of triangles in which the nearest vert "participates".
                int[] nearTris = vt[nearests[i]];

                for (int j = 0; j < nearTris.Length; j++)
                {
                    int     triOff = nearTris[j] * 3;
                    Vector3 a      = verts[tris[triOff]];
                    Vector3 b      = verts[tris[triOff + 1]];
                    Vector3 c      = verts[tris[triOff + 2]];

                    ClosestPoint.ClosestPointOnTriangleToPoint(ref point, ref a, ref b, ref c, out possNearestPt);
                    float possNearestSqDist = (point - possNearestPt).sqrMagnitude;

                    if (possNearestSqDist < nearestSqDist)
                    {
                        nearestPt     = possNearestPt;
                        nearestSqDist = possNearestSqDist;
                    }
                }
            }

            //Transform point to world space and return
            return(meshTransform.TransformPoint(nearestPt));
        }
 /// <summary>
 /// Returns the closest point on a mesh
 /// </summary>
 /// <returns>The point on mesh.</returns>
 /// <param name="sphere">Sphere.</param>
 /// <param name="mesh">Mesh.</param>
 public static Vector3 ClosestPointOnMesh(this SphereCollider sphere, MeshCollider mesh)
 {
     return(ClosestPoint.ClosestPointOnMesh(mesh, sphere.transform.position));
 }
 /// <summary>
 /// Returns the closest point on a terrain
 /// </summary>
 /// <returns>The point on terrain.</returns>
 /// <param name="sphere">Sphere.</param>
 /// <param name="terrain">Terrain.</param>
 public static Vector3 ClosestPointOnTerrain(this SphereCollider sphere, TerrainCollider terrain)
 {
     return(ClosestPoint.ClosestPointOnTerrain(terrain, sphere.transform.position));
 }
 /// <summary>
 /// Returns the closest point on a capsule
 /// </summary>
 /// <returns>The point on capsule.</returns>
 /// <param name="sphere">Sphere.</param>
 /// <param name="capsule">Capsule.</param>
 public static Vector3 ClosestPointOnCapsule(this SphereCollider sphere, CapsuleCollider capsule)
 {
     return(ClosestPoint.ClosestPointOnCapsule(capsule, sphere.transform.position));
 }
 /// <summary>
 /// Returns the closest point on a sphere
 /// </summary>
 /// <returns>The point on sphere.</returns>
 /// <param name="sphere">Sphere.</param>
 /// <param name="otherSphere">Other sphere.</param>
 public static Vector3 ClosestPointOnSphere(this SphereCollider sphere, SphereCollider otherSphere)
 {
     return(ClosestPoint.ClosestPointOnSphere(otherSphere, sphere.transform.position));
 }
 /// <summary>
 /// Returns the closest point on a box
 /// </summary>
 /// <returns>The point on box.</returns>
 /// <param name="sphere">Sphere.</param>
 /// <param name="box">Box.</param>
 public static Vector3 ClosestPointOnBox(this SphereCollider sphere, BoxCollider box)
 {
     return(ClosestPoint.ClosestPointOnBox(box, sphere.transform.position));
 }