- Home /
Which plane is which with CalculateFrustumPlanes
I understand that CalculateFrustumPlanes() in Unity3D returns an array of Plane objects, each representing a different frustum plane, but I can't find any documentation to suggest which element is which?
for example
[0] = Front [1] = Back
etc.
I need to calculate whether a point in space (like the centre point of a Bounding volume) is in the camera frustum, for a Quad tree system.
If there's no documented answer, it's likely the only way to learn this is by checking. It's even possible (though probably unlikely) that it won't be the same order in every scenario or version of Unity.
In whichever method initializes and populates the array, check the normal of each plane against the camera's forward, then sort them however you like with some vector math. Not pretty, but the calculations should be fast, and probably only need to happen once to establish ordering.
Answer by MrEastwood · Feb 13, 2015 at 01:34 AM
I got this arrangement:
[0] = Left
[1] = Right
[2] = Down
[3] = Up
[4] = Near
[5] = Far
All normals point towards the inside of the frustum
Thanks a lot! The Unity documentation code supports your last statement. The direction of frustum plane normals should be added to the documentation. Since the normals are facing inwards, a cheap cull test for a point Vector3 p with a safe margin float safe$$anonymous$$argin would have to compare with the negated frustum plane normals as follows:
if(Vector3.Dot(-planes[i].normal, p) - planes[i].distance) > safe$$anonymous$$argin) cull = true;
Answer by correia55 · Dec 06, 2015 at 02:40 PM
The answer to this question is now described in the unity documentation , including a script that creates planes in the correct positions. Either way the answer from @MrEastwood is correct so you should mark it as solved.
Answer by KevinCodes4Food · May 05, 2014 at 07:14 PM
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 ";
}
}