Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by michael 4 · Jul 13, 2011 at 02:47 PM · camerafieldofviewfrustrum

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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();


Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image michael 4 · Jul 13, 2011 at 06:01 PM 0
Share

Great, thanks! These are the coordinates I needed to generate a mesh,

avatar image michael 4 · Jul 13, 2011 at 07:15 PM 0
Share

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));  
avatar image michael 4 · Jul 13, 2011 at 07:56 PM 0
Share

From Jul 13, 2011

avatar image michael 4 · Jul 13, 2011 at 07:57 PM 0
Share

The image above shows what im talking about, the fustrum isnt lining up with the camera, other then that, the shape is perfect.

avatar image Bunny83 · Jul 13, 2011 at 09:22 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges