- Home /
Transparency shader that also writes to Camera Depth Texture
We have a game which is set in a 3D world, and the character graphics are 2D (as in made of Sprites, with transparency) the net effect is something similar to Paper Mario. In the world, we have some spherical fog, the way this effect is achieved is that opaque geometry gets rendered into the Camera Depth Texture (as a separate pass), the scene is then rendered (opaque first, then transparent) and then at the end the spherical fog volume is rendered (in the transparent pass), it does this by basically seeing where each pixels depth is, in relation to the fog centre, and based on that adding in some fog colour. For the scene this works great.
For the characters, the amount of fog which gets added on is incorrect, as the player isn't being added to the Camera Depth Texture (from our tests, it appears impossible to do this if you also want transparency support). Our characters transparency is more or less 'on/off' information (this isn't quite true, as there is blending on the edges to avoid jaggies), but if we force the character to be opaque, so it renders into the Camera Depth Texture, we can use a low alpha cut off value so the render to the Camera Depth Texture looks as we want, the fogging will then in turn render correctly, however our character will then be opaque. We can set the shader to be in the alpha test queue, which allows us to get a correctly fogged character, but with jagged edges. What we really want to do is have:
The character render into the Camera Depth Texture render pass
The character be rendered in the transparent render pass
So far we haven't found a way to achieve this; it appears to be impossible to write a shader which will add passes both to the Camera Depth Texture and Transparent. As a proof of concept, we attached two materials to one mesh, set each material to use a different shader, one which only renders into the Camera Depth Texture, and one that only renders in the Transparent pass and it does achieve the end result we want (see image, notice alpha blending on players leg in first image and 'normal' levels of fog, the second half of the image is with the fog value cranked crazy high), but it is not easy for us to do this in game (as our characters are rendered with Spine a 2D animation package + Unity Runtime, and it doesn't support having multiple materials per mesh).
Is there a way to coax Unity into allowing a shader to render to Camera Depth Texture + Transparent?