Пример #1
0
//---------------------------------------------------------------------MONO METHODS:

        void Awake()
        {
            MeshFilter mf   = GetComponent <MeshFilter>();
            Mesh       mesh = null;

            if (mf != null)
            {
                mesh = mf.mesh;
            }
            else
            {
                MeshCollider meshCollider = GetComponent <MeshCollider>();
                if (meshCollider != null)
                {
                    mesh = meshCollider.sharedMesh;
                }
            }
            if (mesh != null)
            {
                vt    = new VertTriList(mesh);
                verts = mesh.vertices;
                tris  = mesh.triangles;
                kd    = KDTree.MakeFromPoints(verts);
            }
        }
Пример #2
0
        private 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).
            //  Since there can be multiple vertices on a single spot, we need to find the correct vert and triangle.
            vertProx.FindNearestEpsilon(pt, nearests);

            Vector3 nearestPt     = Vector3.zero;
            float   nearestSqDist = 100000000f;
            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[tri[triOff]];
                    Vector3 b      = verts[tri[triOff + 1]];
                    Vector3 c      = verts[tri[triOff + 2]];

                    closestPointOnTriangleToPoint(ref pt, ref a, ref b, ref c, out possNearestPt);
                    float possNearestSqDist = (pt - possNearestPt).sqrMagnitude;

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

            return(nearestPt);
        }