- Home /
Having trouble getting objects to render in the background
I'm working on a 3D, first-person project which uses fog over an empty skybox, in part to hide pop-in by objects beyond the camera view. Since blank fog on its own is pretty bland looking, I've also got some background objects (clouds and mountain silhouettes) which move along with the camera to create a sort of "fake skybox."
It actually looks pretty great, with one major hiccup - the background elements need to always render behind other objects, or the illusion is broken. This seems like it should be easy enough, but I've tried a lot of different approaches, from various forums, none of which has been working. I should note that I'm using Unity 2017, so any approach using the newer "HD Render Pipeline" shaders is not going to work for this project.
What I've already tried:
Using a second camera for the background layer. Main camera is set to depth only or don't clear, while the background camera has a Skybox clear flag. Have tried multiple setups for depth, rendering path, etc - but the background camera never seems to render unless its Depth is higher - which of course obscures the rest of the game.
Changing the render queue of the material through debug mode or by a script. I've tried it with both standard and legacy shaders, but it also doesn't seem to do anything, aside from sometimes resulting in pixelation. It also seems like Unity likes to reset the custom queue value to 3000 a lot, and that doing it this way creates a new instance of the material rather than editing the base material - leading me to wonder if this is really a workable approach.
Creating an alternate shader which turns on ZWrite and tries different ZTest settings. Some ZTest options show no changes, while others result in the texture looking badly pixelated (and still not rendering in the background).
Adding a subshader which uses a stencil buffer to cut out parts of objects which are obscured. I'm not particularly great with shader language, but have been able to Frankenstein together some useful shaders in the past, and am pretty sure I've stitched the examples I found in the right place. But still, I'm not having any luck adding this functionality to a standard shader.
All of these seem like practical solutions (if a little convoluted for what seems like it should be a basic feature), and they seemed to work for other people. But none are working for me, and I've tried dozens of variations on these approaches, with absolutely no luck.
So: has anyone else run into a hard time with this, and did you end up finding a solution? Did any of the approaches I tried work for you? If so, was there anything else you had to tweak to get it to work right? Are these just out-of-date approaches from old forum questions, and if so, what would be the "correct" way to do it?
I'm at a complete loss here, even moreso because these solutions have clearly worked for other people. Does anyone have any ideas as to where I'm going wrong?