Example #1
0
    /// <summary>
    /// 判断包围盒是否被相机裁剪
    /// </summary>
    /// <param name="bounds"></param>
    /// <param name="camera"></param>
    /// <returns></returns>
    public static bool IsBoundsInCamera(this Bounds bounds, Camera camera)
    {
        Matrix4x4 matrix = camera.projectionMatrix * camera.worldToCameraMatrix;

        int code =
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x + bounds.size.x / 2, bounds.center.y + bounds.size.y / 2,
                                                bounds.center.z + bounds.size.z / 2, 1), matrix);


        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x - bounds.size.x / 2, bounds.center.y + bounds.size.y / 2,
                                                bounds.center.z + bounds.size.z / 2, 1), matrix);

        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x + bounds.size.x / 2, bounds.center.y - bounds.size.y / 2,
                                                bounds.center.z + bounds.size.z / 2, 1), matrix);

        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x - bounds.size.x / 2, bounds.center.y - bounds.size.y / 2,
                                                bounds.center.z + bounds.size.z / 2, 1), matrix);

        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x + bounds.size.x / 2, bounds.center.y + bounds.size.y / 2,
                                                bounds.center.z - bounds.size.z / 2, 1), matrix);

        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x - bounds.size.x / 2, bounds.center.y + bounds.size.y / 2,
                                                bounds.center.z - bounds.size.z / 2, 1), matrix);

        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x + bounds.size.x / 2, bounds.center.y - bounds.size.y / 2,
                                                bounds.center.z - bounds.size.z / 2, 1), matrix);

        code &=
            MatrixEx.ComputeOutCode(new Vector4(bounds.center.x - bounds.size.x / 2, bounds.center.y - bounds.size.y / 2,
                                                bounds.center.z - bounds.size.z / 2, 1), matrix);


        if (code != 0)
        {
            return(false);
        }

        return(true);
    }
Example #2
0
 protected virtual int CalculateCullCode(Vector4 position, Matrix4x4 matrix)
 {
     return(MatrixEx.ComputeOutCode(position, matrix));
 }