Skip to content

Instantly share code, notes, and snippets.

@ViktorKrepelka
Created August 8, 2023 05:32
Show Gist options
  • Select an option

  • Save ViktorKrepelka/d6be603f292bf93042d8c7fd7484d7e7 to your computer and use it in GitHub Desktop.

Select an option

Save ViktorKrepelka/d6be603f292bf93042d8c7fd7484d7e7 to your computer and use it in GitHub Desktop.
//return visible percentage of object from camera
public float GetVisiblePercentage(Bounds bounds, Camera cam)
{
float totalVolume = bounds.size.x * bounds.size.y * bounds.size.z;
float visibleVolume = 0f;
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
for (int i = 0; i < planes.Length; i++)
{
visibleVolume += Mathf.Max(0f, Vector3.Dot(planes[i].normal, bounds.max - cam.transform.position));
}
float visiblePercentage = visibleVolume / totalVolume * 100f;
return visiblePercentage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment