- Home /
More questions on generating camera frustrum
According to the documentation this function should return the six planes that make up the camera fustrum. (http://unity3d.com/support/documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html)
GeometryUtility.CalculateFrustumPlanes();
and also according to the documentation, the following code should turn those 6 planes into mesh planes in order to create geometry that makes up the camera frustrum
public class example : MonoBehaviour {
private Camera cam;
private Plane[] planes;
void Start() {
cam = Camera.main;
planes = GeometryUtility.CalculateFrustumPlanes(cam);
int i = 0;
while (i < planes.Length) {
GameObject p = GameObject.CreatePrimitive(PrimitiveType.Plane);
p.name = "Plane " + i.ToString();
p.transform.position = -planes[i].normal * planes[i].distance;
p.transform.rotation = Quaternion.FromToRotation(Vector3.up, planes[i].normal);
i++;
}
}
}
However when I try to run this code, I just get random planes floating in random places in my world. Anyone have any idea why? My goal is to create a textured transparent box that represents the frustum of a security camera.
Answer by Bunny83 · Jul 13, 2011 at 05:50 PM
No, it won't work that way. The planes you get back from CalculateFrustumPlanes are logical infinite planes. They don't have a limit. The example code just creates 6 square meshes that are placed on the logical planes. I've tested the code and all planes are correct. All planes faces their normal direction. The position is quite irrelevant since the planes are infinite.
If you want to create a Mesh that represents the viewing frustum, the 4 sides are not squares, they are trapezoids. You could get the corners of this volume by intersecting 3 planes but that's kinda complicated. It's much easier to use Camera.ViewportToWorldPoint.
edit
I've created a "small" extention for the Camera class so you can create a frustumMesh with a single function.
public static class CameraExtention
{
private static int[] m_VertOrder = new int[24]
{
0,1,2,3, // near
6,5,4,7, // far
0,4,5,1, // left
3,2,6,7, // right
1,5,6,2, // top
0,3,7,4 // bottom
};
private static int[] m_Indices = new int[36]
{
0, 1, 2, 3, 0, 2, // near
4, 5, 6, 7, 4, 6, // far
8, 9, 10, 11, 8, 10, // left
12, 13, 14, 15, 12, 14, // right
16, 17, 18, 19, 16, 18, // top
20, 21, 22, 23, 20, 22, // bottom
}; // |______|---> shared vertices
public static Mesh GenerateFrustumMesh(this Camera cam)
{
Mesh mesh = new Mesh();
Vector3[] v = new Vector3[8];
v[0] = v[4] = new Vector3(0,0,0);
v[1] = v[5] = new Vector3(0,1,0);
v[2] = v[6] = new Vector3(1,1,0);
v[3] = v[7] = new Vector3(1,0,0);
v[0].z = v[1].z = v[2].z = v[3].z = cam.nearClipPlane;
v[4].z = v[5].z = v[6].z = v[7].z = cam.farClipPlane;
// Transform viewport --> world --> local
for (int i = 0; i < 8; i++)
v[i] = cam.transform.InverseTransformPoint(cam.ViewportToWorldPoint(v[i]));
Vector3[] vertices = new Vector3[24];
Vector3[] normals = new Vector3[24];
// Split vertices for each face (8 vertices --> 24 vertices)
for (int i = 0; i < 24; i++)
vertices[i] = v[m_VertOrder[i]];
// Calculate facenormal
for (int i = 0; i < 6; i++)
{
Vector3 faceNormal = Vector3.Cross(vertices[i*4+2] - vertices[i*4+1],vertices[i*4+0] - vertices[i*4+1]);
normals[i*4+0] = normals[i*4+1] = normals[i*4+2] = normals[i*4+3] = faceNormal;
}
mesh.vertices = vertices;
mesh.normals = normals;
mesh.triangles = m_Indices;
return mesh;
}
}
With that class somewhere in your project you can simply do that:
camera.GetComponent<MeshFilter>().mesh = camera.GenerateFrustumMesh();
Great, thanks! These are the coordinates I needed to generate a mesh,
I got the basic shape of the frustrum but its facing in the wrong direction. Here is how I set up my verts:
vertices[0] = cam.ViewportToWorldPoint(new Vector3(0,0,cam.farClipPlane ));
vertices[1] = cam.ViewportToWorldPoint(new Vector3(0,1,cam.farClipPlane ));
vertices[2] = cam.ViewportToWorldPoint(new Vector3(1,1,cam.farClipPlane ));
vertices[3] = cam.ViewportToWorldPoint(new Vector3(1,0,cam.farClipPlane ));
vertices[4] = cam.ViewportToWorldPoint(new Vector3(0,0,cam.nearClipPlane));
vertices[5] = cam.ViewportToWorldPoint(new Vector3(0,1,cam.nearClipPlane));
vertices[6] = cam.ViewportToWorldPoint(new Vector3(1,1,cam.nearClipPlane));
vertices[7] = cam.ViewportToWorldPoint(new Vector3(1,0,cam.nearClipPlane));
The image above shows what im talking about, the fustrum isnt lining up with the camera, other then that, the shape is perfect.
I tested it and it works. Where is your $$anonymous$$eshRenderer attached? To your camera? That wouldn't work because the vertex coordinates are in world-space ;).
You can either use an empty-Gameobject with $$anonymous$$eshRenderer & $$anonymous$$eshFilter that sits at 0,0,0 to render it in worldspace or, what would be better, transform the vertices into localspace and attach the Renderer to the camera.
camera.transform.InverseTransformPoint is what you need ;)
Also if you want the planes to "look like planes" you need to split your 8 vertices into 24 so you can specify a seperate normal for each face. Otherwise the edges will look like it's a cone.
Your answer
Follow this Question
Related Questions
Camera Frustrum Calculation 1 Answer
How to cut a camera view in half 1 Answer
Lerping Field Of View is buggy 1 Answer
Camera FOV lower than 1? 1 Answer
Change field of view over time 1 Answer