/// <summary> /// Returns a list of game objects which are visible to the specified camera. /// </summary> /// <remarks> /// This function detects only objects which have a collider attached to them. /// </remarks> public static List <GameObject> GetVisibleGameObjects(this Camera camera) { // We need the camera view volume to detect the visible game objects var cameraViewVolume = new CameraViewVolume(); cameraViewVolume.BuildForCamera(camera, camera.farClipPlane); // In order to detect the visible game objects, we will loop through all POTTENTIALLY visible // game objects and check if their AABB lies inside the camera frustum. List <GameObject> pottentiallyVisibleGameObjects = camera.GetPottentiallyVisibleGameObjects(); var visibleGameObjects = new List <GameObject>(pottentiallyVisibleGameObjects.Count); // Set initial capacity to avoid resize foreach (GameObject gameObject in pottentiallyVisibleGameObjects) { // If the game object's world space AABB intersects the camera frustum, it means it is visible Box worldSpaceAABB = gameObject.GetWorldBox(); if (worldSpaceAABB.IsInvalid()) { continue; } if (cameraViewVolume.ContainsWorldSpaceAABB(worldSpaceAABB.ToBounds())) { visibleGameObjects.Add(gameObject); } } // Return the visible objects list to the caller return(visibleGameObjects); }
public static bool IsGameObjectVisible(this Camera camera, GameObject gameObject) { var cameraViewVolume = new CameraViewVolume(); cameraViewVolume.BuildForCamera(camera, camera.farClipPlane); Box worldSpaceAABB = gameObject.GetWorldBox(); if (worldSpaceAABB.IsInvalid()) { return(false); } return(cameraViewVolume.ContainsWorldSpaceAABB(worldSpaceAABB.ToBounds())); }