Disable Z-Buffer writing for transparent pixels

I am building a 2D game using sprites. I’d like to use the z-buffer to aid with shading. However, I cannot convince Unity to write only opaque pixels to the z-buffer: it is writing the entire polygons of which the sprites are composed.

This is the shader I’m using (with some junk added while trying to get this to work) https://code.stypi.com/tcnwsd5u

I have tried

  • AlphaTest Greater 0
  • alphatest:MyAlphaTestVar
  • the discard keyword
  • Cg’s clip function keyword.

I suspected that a lighting pass might be writing the depths so I added novertexlights and noambient

Z-buffer showing polygons:

Game view:

How do I get Unity to not write transparent pixels to the z-buffer?

If all you care about is shading on non-transparent pixels, you can make sure any alpha values that are < 1 don’t get the color modifications.

Found! (Thank you Frame Debugger !)

It turns out that Unity renders the Depth Texture in a separate pass before all geometry is rendered. It is hard to find how to include objects in this pass, and how to specify how they are drawn.

39570-screen-shot-2015-01-26-at-142820.png

The documentation is kind of vague, but it tells you that the “RenderType” is important without specifying what it should be in order for objects to be included in this pass. I found out that a SubShader will render if it specifies

"LightMode" = "ShadowCaster"

So, my shader needed a separate SubShader that also performs an AlphaTest with this LightMode set. This in addition to the ‘regular’ SubShader which will draw the sprites; which might not even need an AlphaTest since the zBuffer is not used for the DepthTexture.

Hi everyone,

There is a solution to this problem ? I read everything and I don’t see it.

I have an object, a part of it can be unvisible and the other part is visible. But with the depth managing in the shader, when a pixel is Clip(), it’s always here, transparent, but always here. :frowning:

I find a solution, it’s to change Zwrite set-up to Off. But I can’t change it dynamically with C#. (I don’t found the way to do that).


Here it what I see from a point of view (unvisible part of the object hide me the other part)
114355-bug.png


Here it’s the visible part of the object.(no unvisible part hide me the object)
114356-bug2.png


I would like find a solution like : this pixel is clip so ZWrite Off too.

Any idea ?

Thanks !