以3D分类点

时间:2015-03-04 08:25:58

标签: c# algorithm unity3d 3d

如果我在3D中有一组点和一个摄像机平面,我将如何根据到该平面的距离对它们进行排序?我至少需要pesudo代码。我想用相机构建一个平面然后对点进行排序。

1 个答案:

答案 0 :(得分:1)

class CompareDistance
{
    public float DistanceToCameraPlane(Vector3 pointInSpace)
    {
        var cameraPosition = Camera.main.transform.position;
        var cameraForward = Camera.main.transform.forward;
        var deltaToCamera = pointInSpace - cameraPosition;
        var projection = Vector3.Project(deltaToCamera, cameraForward);
        return projection.magnitude;
    }
}