检查某个点是否靠近 Bounds 的指定边缘

/// <summary>
    /// 检查某个点是否靠近 Bounds 的指定边缘
    /// </summary>
    /// <param name="point"></param>
    /// <param name="bounds"></param>
    /// <param name="edgeDirection">edgeDirection == "XAxis" || edgeDirection == "YAxis" || edgeDirection == "ZAxis"</param>
    /// <param name="distanceThreshold">边缘容差</param>
    /// <returns></returns>
    public static bool IsPointNearEdge(Vector3 point, Bounds bounds, string edgeDirection, float distanceThreshold = 0.1f)
    {
        // 获取边界框的最小和最大位置
        float minX = Mathf.Min(bounds.min.x, bounds.max.x);
        float maxX = Mathf.Max(bounds.min.x, bounds.max.x);
        float minY = Mathf.Min(bounds.min.y, bounds.max.y);
        float maxY = Mathf.Max(bounds.min.y, bounds.max.y);
        float minZ = Mathf.Min(bounds.min.z, bounds.max.z);
        float maxZ = Mathf.Max(bounds.min.z, bounds.max.z);

        // 判断指定方向是否为有效的边缘方向
        //bool isValidEdgeDirection = edgeDirection == EdgeDirection.XAxis || edgeDirection == EdgeDirection.YAxis || edgeDirection == EdgeDirection.ZAxis;
        bool isValidEdgeDirection = edgeDirection == "XAxis" || edgeDirection == "YAxis" || edgeDirection == "ZAxis";

        // 判断指定点是否在边缘附近指定的距离范围内
        if (!isValidEdgeDirection)
        {
            Debug.LogError("超出范围");
            return false;
        }

        Debug.Log(edgeDirection);

        // 判断指定点是否在指定方向上靠近了另外两个方向的边缘
        switch (edgeDirection)
        {
            case "XAxis":
                return (Mathf.Abs(point.x - minX) <= distanceThreshold || Mathf.Abs(point.x - maxX) <= distanceThreshold) &&
                       (point.y >= minY && point.y <= maxY) && (point.z >= minZ && point.z <= maxZ);
            case "YAxis":
                return (Mathf.Abs(point.y - minY) <= distanceThreshold || Mathf.Abs(point.y - maxY) <= distanceThreshold) &&
                       (point.x >= minX && point.x <= maxX) && (point.z >= minZ && point.z <= maxZ);
            case "ZAxis":
                return (Mathf.Abs(point.z - minZ) <= distanceThreshold || Mathf.Abs(point.z - maxZ) <= distanceThreshold) &&
                       (point.x >= minX && point.x <= maxX) && (point.y >= minY && point.y <= maxY);
            default:
                return false;
        }
    }

 

posted on 2024-01-31 22:22  嗜睡的熊大  阅读(2)  评论(0编辑  收藏  举报

导航