- Home /
Trying to Reduce Overdraw: How can I clip select screen areas?
I have a 2D game with multiple cameras. Screenshots shows overdraw rendering.
First, I have a background effect camera at the lowest depth (-12) to render some particles of various sizes floating about to give a visual interest around the edges of the screen as opposed to just a solid color or static image. Screenshots are for the tvOS widescreen version. Please see image part A.
On top of that, I have other cameras which draws my actual game objects. But that actual play area of my game is smaller than the screen and is fixed - never moves or changes size. I also know that everything that I draw there will be opaque -- none of the background layer effects will be visible through it. (Blue box represents actual play area): Please see image part B.
As you can see, about of a third of the screen has needless overdraw. I've tried to program the background effect particles to float away and avoid the center but I can only do that so much before things look "weird" to me. Ideally, I'd like to find a way to tell the background effect camera to avoid/ignore a section of the screen where I know I will be rendering other stuff on top -- a custom shaped clipping area. My first thought would be to place a quad with a special "clipping shader" that tells the renderer "ignore pixels in this area. A black empty rectangle in the center is what I'd like my background effect camera to be able to do but without additional overdraw. Please see image part C.
How can I do this? If this can be done via a shader as I hope, can someone help write?
Thanks, Manny
I've turned on the camera "Occlusion Culling" but with no apparent effect. The overdraw rendering mode in my scene view still shows apparent overdraw -- or does that not properly reflect whats actually going on?
After re-reading your details and the comment below, I'm not sure occlusion culling will help in this case. If you do have a different "main camera" view it might benefit from occlusion culling though I'm not sure about the effect for a 2D camera. Turning it on probably isn't enough, like Lightmapping you have to do some setup - check the doc it explains it well.
https://docs.unity3d.com/$$anonymous$$anual/OcclusionCulling.html
Answer by Pangamini · Dec 08, 2016 at 08:12 PM
Make sure the occluder is drawn first with write to the depth buffer; Make sure the underlying geometry fails the z-test under the occluder. This should help
Edit: By occluder, I don't mean any particular unity class (means it's unrelated to umbra occlusion culling), but simply any geometry that's rendered into depth buffer.
I'm not sure I follow. Is the "occluder" a mesh object like my quad? Should it be rendered by the same camera as the one I'm using for my background effect? I thought occlusion culling was used primarily for 3d games -- does it work the same way for 2D games?
I've search for articles and tutorials to help me explain and use but nothing that talks to my specific need -- do you know of any?
$$anonymous$$anny
It's not occlusion culling. Pixels are discarded from shading if they fail the z-test. Just render any quad that writes the depth; make it write a very low depth (so other parts of the scene have higher depth / distance from the camera) and make sure it's drawn first (use shader/material render queue for that). Occlusion culling (Umbra) culls entire objects / meshes, so drawcalls are reduced too; this is not the case. However you could try to utilize umbra (or some custom solution) to discard objects completely if they are entirely hidden by your quad. But just discarding pixels with z-test should increase performance, at least of the pixel shading
Thanks for your input. However, try as I might, I having a hard time wrapping my head around this. Are you suggesting, like I think you are, that this is accomplished using a pixel shader? I can't seem to find examples of "rendering to depth buffer" as you suggested.
I have never written a shader and am having to start from scratch. Is there a known shader that I can use?
That's what I did. The Tris and Vertices differences only reflected the quad mesh being turned on and off and the Set Pass reflect the extra draw call I noted above. What's gong on at the lower level of pixel fill rate -- I cannot tell.
There will be no difference in triangle/vertex count, nor the drawcall/batch count, because you are not culling drawcalls / geometry. The 'overdraw' mode scene view simply renders everything with a custom additive shader, it won't actually show you the exact overdraw; All you can do now is benchmark. Write a super expensive shader and render it a lot under the occluded area. Then go playmode, and check the fps/ms with the occluder on and off and see if there's a result. It also depends on the HW, but AFAI$$anonymous$$ modern cards will discard pixels from the pixel shader if they fail the depth test - you should see the result