The answer proved by AlwaysSunny above recommends checking the normal of each plane against the camera's forward.
Unfortunately, the near and far planes have the same forward :(
The easiest solution I have found is to move each plane along the normal. Then I look at the location of the plane relative to the camera's position while the camera is at a zeroed rotational position:
GameObject[] frustumBounginObjects = new GameObject[6];
public void CreateScreenBoundingObjects ()
{
Camera cam = Camera.main;
Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(cam);
for (int i=0; i < frustumPlanes.Length; ++i)
{
frustumBounginObjects[i] = GameObject.CreatePrimitive(PrimitiveType.Plane);
frustumBounginObjects[i].transform.position = -frustumPlanes[i].normal * frustumPlanes[i].distance;
// Which plane?
if(frustumBounginObjects[i].transform.position.y > 0) // Top?
{
frustumBounginObjects[i].name = "Top";
}
else if(frustumBounginObjects[i].transform.position.y < 0) // Bottom?
{
frustumBounginObjects[i].name = "Bottom";
}
else if(frustumBounginObjects[i].transform.position.x > 0) // Right?
{
frustumBounginObjects[i].name = "Right";
}
else if(frustumBounginObjects[i].transform.position.x < 0) // Left?
{
frustumBounginObjects[i].name = "Left";
}
else if(frustumBounginObjects[i].transform.position.z > Camera.main.nearClipPlane) // Far?
{
frustumBounginObjects[i].name = "Far";
}
else // Near?
{
frustumBounginObjects[i].name = "Near";
}
frustumBounginObjects[i].name += "Plane ";
}
}
↧