示例#1
0
        public static bool IsBoundsInCameraEx(this Bounds bounds, Camera camera, float leftex, float rightex, float downex,
                                              float upex)
        {
            Matrix4x4 matrix = camera.projectionMatrix * camera.worldToCameraMatrix;

            int code =
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);


            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);

            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);

            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);

            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);

            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);

            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);

            code &=
                MatrixEx.ComputeOutCodeEx(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, leftex, rightex, downex, upex);


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

            return(true);
        }
示例#2
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);
        }
示例#3
0
 protected override int CalculateCullCode(Vector4 position, Matrix4x4 matrix)
 {
     return(MatrixEx.ComputeOutCodeEx(position, matrix, m_LeftEx, m_RightEx, m_DownEx, m_UpEx));
 }
示例#4
0
        //public override int DetecedCode2D(float centerX, float centerY, float centerZ, float sizeX, float sizeY, float sizeZ)
        //{
        //    if (m_Camera == null)
        //        return 0;
        //    Matrix4x4 matrix = m_Camera.cullingMatrix;

        //    float hsizex = sizeX * 0.5f;
        //    float hsizey = sizeY * 0.5f;
        //    float hsizez = sizeZ * 0.5f;

        //    int code = 0;
        //    for (int j = 0; j < 2; j++)
        //    {
        //        for (int i = 0; i < 2; i++)
        //        {
        //            code |= ComputeCellCode(
        //                new Vector3(centerX + (i - 1) * hsizex, centerY - hsizey, centerZ + (j - 1) * hsizez),
        //                hsizex, hsizey, hsizez, matrix, 1 << (j * 2 + i));
        //        }
        //    }

        //    return code;
        //}

        //public override int DetecedCode3D(float centerX, float centerY, float centerZ, float sizeX, float sizeY, float sizeZ)
        //{
        //    return 0;
        //}

        //private int ComputeCellCode(Vector3 position, float sizex, float sizey, float sizez, Matrix4x4 matrix, int code)
        //{
        //    int c = CalculateCullCode(new Vector4(position.x, position.y, position.z, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x + sizex, position.y, position.z, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x, position.y + sizey, position.z, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x, position.y, position.z + sizez, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x + sizex, position.y + sizey, position.z, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x + sizex, position.y, position.z + sizez, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x, position.y + sizey, position.z + sizez, 1.0f), matrix);
        //    c &= CalculateCullCode(new Vector4(position.x + sizex, position.y + sizey, position.z + sizez, 1.0f), matrix);
        //    if (c == 0)
        //        return code;
        //    return 0;
        //}

        protected virtual int CalculateCullCode(Vector4 position, Matrix4x4 matrix)
        {
            return(MatrixEx.ComputeOutCode(position, matrix));
        }