public FrustumIntersection GetIntersect(AxisAlignedBoundingBox boundingBox) { FrustumIntersection returnValue = FrustumIntersection.Inside; Vector3 vmin, vmax; for (int i = 0; i < Planes.Length; ++i) { // X axis if (Planes[i].PlaneNormal.x > 0) { vmin.x = boundingBox.minXYZ.x; vmax.x = boundingBox.maxXYZ.x; } else { vmin.x = boundingBox.maxXYZ.x; vmax.x = boundingBox.minXYZ.x; } // Y axis if (Planes[i].PlaneNormal.y > 0) { vmin.y = boundingBox.minXYZ.y; vmax.y = boundingBox.maxXYZ.y; } else { vmin.y = boundingBox.maxXYZ.y; vmax.y = boundingBox.minXYZ.y; } // Z axis if (Planes[i].PlaneNormal.z > 0) { vmin.z = boundingBox.minXYZ.z; vmax.z = boundingBox.maxXYZ.z; } else { vmin.z = boundingBox.maxXYZ.z; vmax.z = boundingBox.minXYZ.z; } if (Vector3.Dot(Planes[i].PlaneNormal, vmin) - Planes[i].DistanceToPlaneFromOrigin > 0) { if (Vector3.Dot(Planes[i].PlaneNormal, vmax) - Planes[i].DistanceToPlaneFromOrigin >= 0) { return(FrustumIntersection.Outside); } } if (Vector3.Dot(Planes[i].PlaneNormal, vmax) - Planes[i].DistanceToPlaneFromOrigin >= 0) { returnValue = FrustumIntersection.Intersect; } } return(returnValue); }
public FrustumIntersection GetIntersect(AxisAlignedBoundingBox boundingBox) { FrustumIntersection returnValue = FrustumIntersection.Inside; Vector3 vmin, vmax; for (int i = 0; i < Planes.Length; ++i) { // X axis if (Planes[i].PlaneNormal.X > 0) { vmin.X = boundingBox.minXYZ.X; vmax.X = boundingBox.maxXYZ.X; } else { vmin.X = boundingBox.maxXYZ.X; vmax.X = boundingBox.minXYZ.X; } // Y axis if (Planes[i].PlaneNormal.Y > 0) { vmin.Y = boundingBox.minXYZ.Y; vmax.Y = boundingBox.maxXYZ.Y; } else { vmin.Y = boundingBox.maxXYZ.Y; vmax.Y = boundingBox.minXYZ.Y; } // Z axis if (Planes[i].PlaneNormal.Z > 0) { vmin.Z = boundingBox.minXYZ.Z; vmax.Z = boundingBox.maxXYZ.Z; } else { vmin.Z = boundingBox.maxXYZ.Z; vmax.Z = boundingBox.minXYZ.Z; } if (Vector3.Dot(Planes[i].PlaneNormal, vmin) - Planes[i].DistanceToPlaneFromOrigin > 0) { if (Vector3.Dot(Planes[i].PlaneNormal, vmax) - Planes[i].DistanceToPlaneFromOrigin >= 0) { return(FrustumIntersection.Outside); } } if (Vector3.Dot(Planes[i].PlaneNormal, vmax) - Planes[i].DistanceToPlaneFromOrigin >= 0) { returnValue = FrustumIntersection.Intersect; } } return(returnValue); }
public void FrustumIntersetAABBTests() { { Frustum frustum = new Frustum( new Plane(new Vector3(1, 0, 0), 20), new Plane(new Vector3(-1, 0, 0), 20), new Plane(new Vector3(0, 1, 0), 20), new Plane(new Vector3(0, -1, 0), 20), new Plane(new Vector3(0, 0, 1), 20), new Plane(new Vector3(0, 0, -1), 20)); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-30, -10, -10), new Vector3(-25, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-25, 0, -10), new Vector3(-15, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, -5, -5), new Vector3(5, 5, 5)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } { Frustum frustum = new Frustum( new Plane(new Vector3(-1, -1, 0), 0), new Plane(new Vector3(1, -1, 0), 0), new Plane(new Vector3(0, -1, -1), 0), new Plane(new Vector3(0, -1, 1), 0), new Plane(new Vector3(0, -1, 0), 0), new Plane(new Vector3(0, 1, 0), 10000)); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-110, 0, -10), new Vector3(-100, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect with origin (front) { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-10, -10, -10), new Vector3(10, 10, 10)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, 100, -5), new Vector3(5, 110, 5)); FrustumIntersection intersection = frustum.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } { // looking down -z Frustum frustum5PlaneNegZ = new Frustum( new Vector3(-1, 0, 1), new Vector3(-1, 0, 1), new Vector3(0, 1, 1), new Vector3(0, -1, 1), new Vector3(0, 0, -1), 10000); // outside to left { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-110, 0, -10), new Vector3(-100, 10, 10)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Outside); } // intersect with origin (front) { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-10, -10, -10), new Vector3(10, 10, 10)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Intersect); } // inside { AxisAlignedBoundingBox aabb = new AxisAlignedBoundingBox(new Vector3(-5, -5, -110), new Vector3(5, 5, -100)); FrustumIntersection intersection = frustum5PlaneNegZ.GetIntersect(aabb); Assert.IsTrue(intersection == FrustumIntersection.Inside); } } }