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
1
Question by zenforhire · Sep 10, 2015 at 01:31 AM · frustrum

Calculate camera frustum intersection with plane

We have play area composed of hex gameobjects. We only display/load what is visible. Impossible to load large maps at one time. We want to calculate the polygon intersection with the game board plane. We cannot load every possible game object and test if in the frustum or ray cast to the screen.

We need custom code to see which gameobject positions are in the visibility polygon? How do you get the corner rays of the camera frustum?

alt text

view-pyramid-clipping.jpg (33.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by troien · Sep 10, 2015 at 08:20 AM

As for the :

How do you get the corner rays of the camera frustum?

To get the ray of each corner you can simply use camera.ViewportPointToRay.

 Ray bottomLeft = camera.ViewportPointToRay(new Vector3(0, 0, 0));
 Ray topLeft = camera.ViewportPointToRay(new Vector3(0, 1, 0));
 Ray topRight = camera.ViewportPointToRay(new Vector3(1, 1, 0));
 Ray bottomRight = camera.ViewportPointToRay(new Vector3(1, 0, 0));

If your plane is on a fixed height you perhaps don't even have to raycast. As a method like this should return the rays intersection on a given y position. (I used this method because I needed a similar thing, but I didn't have a collider to raycast against as I used Graphics.DrawMesh).

 public static Vector3 GetPointAtHeight(Ray ray, float height)
 {
     return ray.origin + (((ray.origin.y - height) / -ray.direction.y) * ray.direction);
 }

Note that this method can thow a division by zero if ray.direction.y is 0.

Comment
Add comment · Show 3 · 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 UnityBrains · Mar 19 at 09:57 PM 0
Share

The GetPointAtHeight function works great. I was wondering if you could elaborate on the maths involved. Clearly you're multiplying the direction vector by a scalar, I'm just confused how dividing the origin.y by the -direction.y results in a scalar that reaches the intersection point.

avatar image Bunny83 UnityBrains · Mar 20 at 02:47 AM 1
Share

Well, that's just simple linear math, nothing special. First of all you should read the forumla carefully. We do not divide origin.y by direction.y but the difference between the origin.y and the desired height. So that's the distance from our "plane" (since the plane is parallel to the x-z plane at a certain y height). Since ray.direction is a normalized vector (a vector with length 1.0), it means the y component is the sine of the pitch angle of the ray. So dividing the vertical height by the sine gives you the length of the hypotenuse or the length along the ray which is exactly what we need. You can also view it like this:


When you multiply the direction by a scaler you move along that direction vector towards our plane. Of course our hit point needs to have the same y value as the plane we want to hit. So we want to know which scaler we need in order to make our ray point to have the desired y component. He has the values flipped around and therefore needs to flip the sign again. It makes more sense to do it like this:

 float dist = (height - ray.origin.y) / ray.direction.y;
 return ray.origin + dist * ray.direction;

Again "height" is the worldspace y position of the plane we want to hit. By subtracting the ray origin's y component we get the vertical difference. So how far are we above the plane. So the first line calculates how many direction vectors do we need until we get from the ray origin down to the height of the plane. Since the y component of the direction is the amount we move in the y direction, we just need to divide the relative vertical distance by the y component of the direction. This tells us how many direction vectors we need to reach down to the plane. By multiplying the whole direction vector by that distance and adding the start of the ray, we get the actual hit point on the plane.

avatar image UnityBrains Bunny83 · Mar 20 at 11:43 AM 0
Share

Perfect, thank you very much.

avatar image
0

Answer by musaranya · Sep 10, 2015 at 08:07 AM

I think Unity performs its frustum culling by default so you should not worry about it. Also keep in mind that if the objects are very large culling is less efective because when a vertex gets into the frustum the entire object is rendered. If you still want to get dirt with frustum culling, this couple of functions may interest you:

GeometryUtility.CalculateFrustumPlanes

GeometryUtility.TestPlanesAABB

regards

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Frustum Cull problem on animated objects 1 Answer

How can i start a sound when a specific object is players frustrum? 0 Answers

Modifying plane to fit view frustrum issue 0 Answers

Oblique Frustrum on Multiple Cameras 1 Answer

Asymmetric view frusta / selective region rendering 2 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