- Home /
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?
Probably not any better than using a BoxCollider, but there is renderer.bounds and mesh.bounds.
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.
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.
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;
}
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.
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.
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.