- Home /
How to render objects outside the camera's view?
How do I go about rendering objects outside of the camera's field of view? The shader I am using can slightly change the shape / position of objects visually, but the gameobject is obviously unmoved and so Unity sometimes thinks (correctly) that the object is outside the camera's field of view, but because of the shader they are in view and not being rendered. This also causes objects to disappear just before going off camera. It would be pretty ugly to put a border round the edge of the screen so how would I go about doing this?
Basically I want to make the culling frustum a little larger than the view frustum of the camera. Is this possible?
Answer by Hellium · Jun 24, 2015 at 02:07 PM
If you could, at least, give the code of the shader, it could be helpfull.
You will have to make some research about Frustum culling. This is the method used to not render objects which are outside the cameras' view frustum.
From the doc, it is said that :
Unity ensures that when rendering your objects those which are completely outside of this frustum are not displayed. This is called Frustum Culling. Frustum Culling happens irrespective of whether you use Occlusion Culling in your game.
See : http://docs.unity3d.com/Manual/class-Camera.html
EDIT : it is possible to modifiy the culling distance for specific layers (near and far plane distance)
I think it will be difficult to find a way to tell your cameras to render an object if it is outside the view frustum.
Oops, my bad, see this blog post :
http://allenwp.com/blog/2013/12/19/disabling-frustum-culling-on-a-game-object-in-unity/
The author simply moves the bounds of the gameobject inside the view frustum (but set the transform position back), so that the cameras render it.
// boundsTarget is the center of the camera's frustum, in world coordinates:
Vector3 camPosition = camera.transform.position;
Vector3 normCamForward = Vector3.Normalize(camera.transform.forward);
float boundsDistance = (camera.farClipPlane - camera.nearClipPlane) / 2 + camera.nearClipPlane;
Vector3 boundsTarget = camPosition + (normCamForward * boundsDistance);
// The game object's transform will be applied to the mesh's bounds for frustum culling checking.
// We need to "undo" this transform by making the boundsTarget relative to the game object's transform:
Vector3 realtiveBoundsTarget = this.transform.InverseTransformPoint(boundsTarget);
// Set the bounds of the mesh to be a 1x1x1 cube (actually doesn't matter what the size is)
Mesh mesh = GetComponent().mesh;
mesh.bounds = new Bounds(realtiveBoundsTarget, Vector3.one);
Credits : Allen Pestaluky
So yeah, basically the ideal scenario would be to adjust the culling frustum to make it a little large than the view frustum. I'm using a curvature shader and it's applied to most game objects so adjusting the bounds of each one wouldn't be great, nor efficient.
Using that same "change mesh.bounds
" idea, seems like the textbook solution is to 1-time enlarge mesh.bounds
to include everywhere the mesh might be moved.