Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by Draco18s · Aug 03, 2015 at 03:48 AM · unity 5camerafrustum

Various questions relating to CalculateFrustumPlanes

There's a few questions here relating to the same end-goal effect. Trying to figure out what my options are, what level of flexibility those options have, and what limitations each will come with.

1) How expensive is CalculateFrustumPlanes? i.e. is there a more efficient method to calculating whether or not an object is visible from a camera than CalculateFrustumPlanes and TestPlanesAABB? If I can get a close-enough approximation that's faster, that'd be ideal. There will be a bunch of these checks being called regularly (both #2 and #3).

2) Is there a way to generate a set of frutstum planes from a given point, direction, and field of view angle as if there was a camera there without needing a camera (I don't need to render the view). Reusing code that triggers off a camera for non-camera entities as well would be awesome.

3) Is there a way to calculate the camera view for a non square field of view (assume the camera will be rendering to a texture that would be used elsewhere in the scene, vs. the main camera). If not, I can use a square view, but getting non-square results would allow some flexibility. There will be fewer of these at any given time, but likely still several (security camera, with companion displays).

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
0
Best Answer

Answer by Draco18s · Aug 08, 2015 at 08:52 PM

Figured out #2 on my own. This answer got me the points needed to construct 6 planes, combined with tracing out the results of CalculateFrustumPlanes() I was able to construct this function:

     /**
      *  Returns a set of Planes equivalent to the frustum returned by GeometryUtility.CalculateFrustumPlanes(Camera).
      *  @param origin - the point the virtual camera exists at
      *  @param direction - the normalized look-vector to calculate on; must be
      *                     non-parallel to Vector3.up
      *  @param fovRadians - field of view in radians; 1.047 is ~60 degrees
      *  @param viewRatio - width/height ratio
      *  @param distance - the far-clip plane
      **/
     public static Plane[] CalculateFrustum(Vector3 origin, Vector3 direction,
     float fovRadians, float viewRatio, float distance) {
         Vector3 nearCenter = origin + direction * 0.3f;
         Vector3 farCenter  = origin + direction * distance;
         Vector3 camRight   = Vector3.Cross(direction,Vector3.up) * -1;
         Vector3 camUp      = Vector3.Cross(direction,camRight);

         float nearHeight = 2 * Mathf.Tan(fovRadians / 2) * 0.3f;
         float farHeight  = 2 * Mathf.Tan(fovRadians / 2) * distance;
         float nearWidth  = nearHeight * viewRatio;
         float farWidth   = farHeight * viewRatio;

         Vector3 farTopLeft  = farCenter + camUp*(farHeight*0.5f) - camRight*(farWidth*0.5f);
         //not needed; 6 points are sufficient to calculate the frustum
         //Vector3 farTopRight = farCenter + camUp*(farHeight*0.5f) + camRight*(farWidth*0.5f);
         Vector3 farBottomLeft  = farCenter - camUp*(farHeight*0.5f) - camRight*(farWidth*0.5f);
         Vector3 farBottomRight = farCenter - camUp*(farHeight*0.5f) + camRight*(farWidth*0.5f);

         Vector3 nearTopLeft  = nearCenter + camUp*(nearHeight*0.5f) - camRight*(nearWidth*0.5f);
         Vector3 nearTopRight = nearCenter + camUp*(nearHeight*0.5f) + camRight*(nearWidth*0.5f);
         //not needed; 6 points are sufficient to calculate the frustum
         //Vector3 nearBottomLeft  = nearCenter - camUp*(nearHeight*0.5f) - camRight*(nearWidth*0.5f);
         Vector3 nearBottomRight = nearCenter - camUp*(nearHeight*0.5f) + camRight*(nearWidth*0.5f);
         Plane[] planes = {
             new Plane(nearTopLeft,farTopLeft,farBottomLeft),
             new Plane(nearTopRight,nearBottomRight,farBottomRight),

             new Plane(farBottomLeft,farBottomRight,nearBottomRight),
             new Plane(farTopLeft,nearTopLeft,nearTopRight),

             new Plane(nearBottomRight,nearTopRight,nearTopLeft),
             new Plane(farBottomRight,farBottomLeft,farTopLeft)};

         return planes;
     }

The answer to #1 at this point becomes apparent, as there's not a whole lot in that function that is terribly expensive, with #3 being included in with the viewRatio parameter.

Comment
Add comment · 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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scale Vertices propotional to Camera Frustum 0 Answers

Which plane is which with CalculateFrustumPlanes 3 Answers

Applying Image Post-Processing Effects to Right Eye 1 Answer

Oblique Frustum Clipping 0 Answers

how do i make first person character rotate left and right along with camera? 0 Answers


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