public unsafe ContainmentType Contains(ref BoundingFrustum other) { int pointsContained = 0; FrustumCorners corners = other.GetCorners(); Vector3 * cornersPtr = (Vector3 *)&corners; for (int i = 0; i < 8; i++) { if (Contains(cornersPtr[i]) != ContainmentType.Disjoint) { pointsContained++; } } if (pointsContained == 8) { return(ContainmentType.Contains); } else if (pointsContained == 0) { return(ContainmentType.Disjoint); } else { return(ContainmentType.Intersects); } }
public static void GetCorners(float eyeX, float eyeY, float eyeZ, float viewX, float viewY, float viewZ) { Vector3 viewOrigin = new Vector3(eyeX, eyeY, eyeZ); Vector3 viewDir = Vector3.Normalize(new Vector3(viewX, viewY, viewZ)); Matrix4x4 view = Matrix4x4.CreateLookAt(viewOrigin, viewDir, Vector3.UnitY); const float nearDist = 2f; const float farDist = 10f; const float fov = 1.0f; const float ratio = 1.0f; Matrix4x4 perspectiveProj = Matrix4x4.CreatePerspectiveFieldOfView(fov, ratio, nearDist, farDist); BoundingFrustum frustum = new BoundingFrustum(view * perspectiveProj); FrustumCorners corners = frustum.GetCorners(); Vector3 nearCenter = viewOrigin + viewDir * nearDist; float nearHalfHeight = (float)Math.Tan(fov / 2f) * nearDist; float nearHalfWidth = nearHalfHeight * ratio; Vector3 up = Vector3.Transform(Vector3.UnitY, view); Vector3 right = -Vector3.Cross(up, viewDir); FuzzyComparer fuzzyComparer = new FuzzyComparer(); AssertEqual(nearCenter - nearHalfWidth * right + nearHalfHeight * up, corners.NearTopLeft, fuzzyComparer); AssertEqual(nearCenter + nearHalfWidth * right + nearHalfHeight * up, corners.NearTopRight, fuzzyComparer); AssertEqual(nearCenter - nearHalfWidth * right - nearHalfHeight * up, corners.NearBottomLeft, fuzzyComparer); AssertEqual(nearCenter + nearHalfWidth * right - nearHalfHeight * up, corners.NearBottomRight, fuzzyComparer); Vector3 farCenter = viewOrigin + viewDir * farDist; float farHalfHeight = (float)Math.Tan(fov / 2f) * farDist; float farHalfWidth = farHalfHeight * ratio; AssertEqual(farCenter - farHalfWidth * right + farHalfHeight * up, corners.FarTopLeft, fuzzyComparer); AssertEqual(farCenter + farHalfWidth * right + farHalfHeight * up, corners.FarTopRight, fuzzyComparer); AssertEqual(farCenter - farHalfWidth * right - farHalfHeight * up, corners.FarBottomLeft, fuzzyComparer); AssertEqual(farCenter + farHalfWidth * right - farHalfHeight * up, corners.FarBottomRight, fuzzyComparer); }