- Home /
layerCullDistances farther than farClipPlane
I am looking for a way to overrule the clipping plane of the camera. Why? Because I have an object that is very far away, but should always be drawn. The rest of the objects in the camera should use their normal farClipPlane.
I tested this by setting my layerCullDistances
for layer 12
(where the object is in) to 100000, but it still got clipped by the farClipPlane.
Camera.main.layerCullDistances[12] = 100000;
I also read the API: http://unity3d.com/support/documentation/ScriptReference/Camera-layerCullDistances.html
It only tells me about setting smaller values, but I want it to be larger.
How can I draw my object without it being clipped by the farClipPlane?
My second option is to use a second camera with a larger frustum, but this is not my intention, because it is against my design principles to have two cameras rendering the same way. There is most probably some setting that I can't find.
Update
Here is a screenshot of what is happening. The object is not culled completely, because the pivot point is in the cameras range, but it is clipped by the shader at the farClip's distance.
You can see in this image, that it should actually continue to the end. It is a simple sky dome. The complete object is not clipped, but changing the far clip plane does change this image.
Also asked my question in a more general form at SO: http://stackoverflow.com/questions/6525464/disable-culling-on-an-object
Answer by Marnix · Jun 30, 2011 at 05:16 PM
I decided to make a camera managing system that has two cameras. One with a giant frustum that only renders the skydome. Its depth
is set to -1. The other camera has a normal frustum, but only clears the depth buffer (`clearFlags = depth`) and depth = 0
.
The cameras are drawn over one another, which is fine, but the camera needs polling to values like the FoV to maintain the same ratios.
The clearing of the depth buffer was very important to keep a good ratio for the SSAO-effect (Screen Space Ambient Occlusion-effect).
Another solution could have been, to set the farClipPlane to a large number at OnWillRenderObject
. This also works, but the depth buffer still contains large values. So clearing the depth buffer is actually quite important to have high detail in the z-buffer.
This is a good solution: it's like you had two depth buffers, one for the "far" camera and other for the "near". I suppose that if you're using culling$$anonymous$$ask to enable only the skydome layer in the "far" camera (and to disable it in the other), there will be no loss of performance - probably the layer culling is performed before any other proccess, thus avoiding waste of time with unnecessary frustum culling of other objects in scene.
At the SO forum, someone did tell me that there will be a loss of performance, because the engine has to loop through 2 cameras ins$$anonymous$$d of one. I can imagine that every list is checked twice now. Although there isn't more or less to draw.
If you are using culling$$anonymous$$ask, the extra cost will probably be the following: for each camera, the system will clear the buffers and search objects in the layers enabled by culling$$anonymous$$ask. This will be very fast in the new camera, since there's only one object. The really heavy part is the rendering process itself, which will not be different since the objects are the same as before - only the skydome was assigned to another camera.
@aldonaletto Thanks for the status update. I'll just use the two cameras, but it's still not exactly as I would want it, because I need the polling to keep both cameras at equal values.
Answer by aldonaletto · Jun 29, 2011 at 04:24 PM
The layerCullDistances can only reduce the far clip plane; I think it's a kind of "fake" far plane, used to avoid drawing unnecessary far objects. The "official" near and far planes play an important additional rule - they set the range of the Z plane. You can set a really far camera's Far Clipping Plane, but you can experience problems with borders of objects that intersect others or the terrain - the borders become to tremulate when you move the camera. If possible, you can increase the Near Clipping Plane distance to solve this problem - it has a much higher influence in the Z buffer resolution. The problem is that you can have very near objects clipped, since nothing before the near plane is drawn.
That is exactly what I already knew =) But isn't there a solution to set the cullHint to Never
? So, let it always render? There is no possibility for me to adjust the near clip plane, because we have a small scale game, but I am using UniSky, which wants me to set it the far plane to 100000. What would you recommend other than changing the clipping planes?
The near and far clipping is done first at a higher level, where the engine decides which should be drawn or not - that's the problem, nothing in the shaders can change this because whole clipped objects don't even are sent to the renderer. Let me do a little research, and I'll be back asap.
Well, maybe... the complete object is not culled, a part of it is visible actually. I'll post a screenshot of the case.
I'm back! I've searched around a lot, but haven't found any useful trick to cheat the far clipping plane. Anyway, your object isn't being culled off since parts of it are inside the camera frustum - the problem is the clipping itself.
Restarting from the basics, I decided to test in my project the real relation between the far plane distance and the Z buffer accuracy - the project contains several lakes created by intersection between water planes and the terrain, which are by far the most sensible itens to Z buffer inaccuracies.
The result was surprising: setting the far plane to 100,000 had no noticeable effect on the Z buffer accuracy! I moved the camera around my terrain, and didn't see any apreciable change in the margins of the lakes (in a previous test, when I set the near plane to 0.01 the lake margins changed madly at any small camera movement). I let the near plane set to the default 0.3, and the only movement noticeable in the margins was the usual aliasing effect. I also tested 1,000,000, but then very small fluctuations could be noticed in the margins.
$$anonymous$$y computer has a simple Intel 945G$$anonymous$$ GPU, and as far as I know its Z buffer is 24 bits wide. If you're designing your game to run in PCs or $$anonymous$$ACs, I believe you will have no trouble setting the far plane to 100,000 - even with the default 0.3 at the near plane. This however may not be true in mobile devices, which can use 16 bits depth buffers.
Unfortunately I do have problems with the settings between 0.3 and 100.000. $$anonymous$$y SSAO is creating all sorts of artifacts and I found that adjusting the values inside SSAO cannot be adjusted in a way that it still looks good. I would really like to use a smaller frustum, also for optimizations. I'll probably use 2 cameras then. One for the sky with a far frustum and one with a near frustum for the rest of the scene.
Your answer
Follow this Question
Related Questions
uGUI culling layers alternative 0 Answers
Combining multiple layers and water 0 Answers
Create Camera Frustrum planes for specific layer 0 Answers
Setting a layer to a gameObject dynamically doesn't work 0 Answers
How to display a plane with an image only in the orthographic camera and not in the fps camera?? 1 Answer