Question

I need to get the position of the farthest vertice fo a model given the direction vector and the model.

Eg

obj has the following vertices:

vertice a = (0,0,1)
vertice b = (0,1,0)
vertice c = (1,0,0)
vertice d = (0,0,0)

so

GetPoint(obj, Vector3.up);

will return vertice b

thanks in advance

Was it helpful?

Solution

The below code will give the farthest point but only for meshes with no submeshes you can change the code a little to do it for submeshes..

Vector3 GetFarPoint (Transform obj, Vector3 direction) {

        Vector3[] vertices;
        Vector3 farthestPoint;
        float farDistance;

        vertices = obj.GetComponent<MeshFilter>().mesh.vertices;
        farDistance=0f;

        foreach(Vector3 vert in vertices)
        {
            float temp = Vector3.Dot(direction,vert);
            if(temp>farDistance)
            {
                farDistance = temp;
                farthestPoint = vert;
            }
        }
        return farthestPoint;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top