Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mtrc · Feb 04, 2013 at 02:24 PM · collidermeshcamerasvisibility

Check if a mesh is *fully* visible to camera

I could simplify this check to a BoxCollider if it becomes easier, but: I need to check if the entirety of a mesh/collider is visible to the camera. Most checks see if part is visible (by raycasting once). Is there a 'best practice' to getting this info for an entire mesh?

Comment
Add comment · Show 2
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 robertbu · Feb 04, 2013 at 03:16 PM 0
Share

Probably not any better than using a BoxCollider, but there is renderer.bounds and mesh.bounds.

avatar image Lockstep · Feb 04, 2013 at 03:31 PM 0
Share

You probably need to raycost from every vertex of your mesh. 8 vertices for a cube will be fine. However if you have a more complex mesh I'd suggest using an enclosing mesh which is more simple (like a cube). The vertices of a mesh can be accessed via mesh.vertices.

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Bunny83 · Feb 04, 2013 at 03:57 PM

To test if a certain point is inside the camera frustum, you can use GeometryUtility.CalculateFrustumPlanes and test every point of the mesh if the distance from each of the 6 planes is positive. At the point where one test returns a negative number you can stop testing.

Plane.GetDistanceToPoint can be used to calculate the distance from the given plane.

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 robhuhn · Nov 08, 2013 at 08:45 AM 2
Share

That helped me. Here is a snippet:

 private bool IsInFrustum(Camera cam)
 {
     Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
     
     foreach (Plane plane in planes) 
     {
         foreach (Vector3 vertice in meshFilter.mesh.vertices) 
         {
             if(plane.GetDistanceToPoint(transform.TransformPoint(vertice)) < 0)
             {
                 return false;
             }
         }
     }
 
     return true;
 }
avatar image npruehs · Nov 11, 2014 at 12:55 PM 2
Share

Good job. You can even improve readability by using Plane.GetSide: http://docs.unity3d.com/ScriptReference/Plane.GetSide.html

Note that you might want to use meshFilter.transform.TransformPoint ins$$anonymous$$d of just transform.TransformPoint, depending on where your script lies.

avatar image CHPedersen · Nov 11, 2014 at 01:02 PM 3
Share

Ouch, please beware that accessing meshFilter.mesh.vertices actually copies the mesh vertex array in its entirety and returns it. Thus, accessing it again and again inside a loop (nested!) like this is potentially incredibly inefficient for large meshes.

Cache the vertex array outside the loop first, then iterate over that local array.

avatar image
1

Answer by ModLunar · Jan 03, 2018 at 07:22 AM

I'm not sure if this would be accurate enough for your needs in this case, but I'll add this for anyone who sees this post afterwards anyway --

An approximation you could do is with the mesh renderer's bounding box (testing only 8 points instead of all the vertices). Similarly to Bunny83's answer, you could implement something like:

 public static bool IsFullyVisible(Renderer renderer, Camera camera) {
         Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
 
         Bounds bounds = renderer.bounds;
         Vector3 size = bounds.size;
         Vector3 min = bounds.min;
 
         //Calculate the 8 points on the corners of the bounding box
         List<Vector3> boundsCorners = new List<Vector3>(8) {
             min,
             min + new Vector3(0, 0, size.z),
             min + new Vector3(size.x, 0, size.z),
             min + new Vector3(size.x, 0, 0),
         };
         for (int i = 0; i < 4; i++)
             boundsCorners.Add(boundsCorners[i] + size.y * Vector3.up);
 
         //Check each plane on every one of the 8 bounds' corners
         for (int p = 0; p < planes.Length; p++) {
             for (int i = 0; i < boundsCorners.Count; i++) {
                 if (planes[p].GetSide(boundsCorners[i]) == false)
                     return false;
             }
         }
         return true;
     }

Note that I used the type Renderer, which will work for generally any kind of renderer including MeshRenderers, SkinnedMeshRenderers, BillboardRenderers, etc.

And of course you could always, for this calculation, scale the bounds a bit if you want to tweak it a bit. To do that, you can multiply the size variable that represented the size of the bounds -- since I use that size to calculate the 8 points.

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

13 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

Related Questions

How do you make a mesh visible to certain cameras? 1 Answer

mesh colliders moving when i press play 1 Answer

how do i make my modle solid? 2 Answers

adding mesh collider in run time slow? 3 Answers

collision between a rigidbody and a mesh collider not working 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