/// <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); }
//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)); }