Shader to only draw where no pixels have already been drawn

Is it possible to have a shader that will only draw where no other pixels have been drawn to the screen? I’ve been playing around with it for a while, but I can’t quite get it working. I’m thinking I should set Queue=“AlphaTest” and then do clip(-1) where there’s already a pixel on the screen. But maybe I’m not on the right track. Any suggestions?

What you would want to do (I assume) is have this shader drawn before everything else, and then have everything else draw over the top of it. That would leave whatever was left as the result from when no pixels were drawn. That would basically be skybox, essentially, or you could do something like a command buffer to blit a shader before the process was done.

Thanks. The problem I’m having with that approach is that the background and foreground meshes overlap sometimes, and I want the background mesh to never show on top of the foreground. This is for my own custom terrain meshes. Here’s a screenshot to illustrate. The green terrain is the foreground, and the grey mountains in the back are what I want to be drawn “behind” everything else (but in front of the skybox). If I set Queue=“Background”, the meshes still overlap, and part of the background mountains get drawn on top of the foreground terrain.

The solution I’ve arrived at for now is to just use multiple cameras. If anyone’s got an idea of how to do it just via shaders, I’m still interested in that.

That makes more sense - it’s not simply where pixels are not drawing, but rather the conflict of ordering. So the reason it is conflicting is because of the depth - even though the mountains are in the background, the depth at certain points will be some X value, and the new terrain being placed has a depth at those places that is farther than X, which means the mountains get rendered and the terrain does not.

A simple solution is to make the mountains not write to depth - disable the shadowcaster pass for them, and turn put ZWrite Off into the shader. That should make everything rendered after them (The normal terrain in the Geometry queue, the mountains in Background) be placed correctly.

Alternatively, push them back farther, as it seems like they’re rendering too close to the camera.

Thanks again. I did try ZWrite Off, but I couldn’t get that to play nice with the sky (Time of Day asset, which overall is very very nice BTW).

I tried pushing the mountains back via the Offset keyword, but that screwed up the z order of all the polygons of the mountain mesh. I can’t really push the mountains back further from the camera either because they’re actually centered on the player position and generated on the fly… basically a low-res version of the main terrain.

stencil buffer

Can you modify the time of day asset? It’s probably in the background queue, I assume, so setting the mountains to Background + 1 or somesuch could get them over the top, even with ZWrite Off. I assume the time of day asset is not writing to depth. I don’t know anything about how that asset works, so it’s hard to suggest a better solution, though.