- Home /
Can I use both alpha and depth to test for pixel rendering?
So I am making a game with many many semi-transparent objects. There is no definite order in which these objects will be stacked. It seems the Ztest Lequal is stopping the object from being drawn at all if it is behind another transparent object and if I use Ztest Always then it draws even behind opaque objects. AlphaTest seems to do nothing maybe because I am using vertex and fragment shaders. Is there a way to do something like 'If pixel is behind an opaque pixel then don't draw, otherwise blend'? I think there should be a way to do this because the pixel already drawn should have an alpha and depth value right? And the pixel to be drawn also has these, so just compare both the alpha and depth. Is this possible?
As far as I can tell, you should get the behaviour you want from ZTest Lequal
, ZWrite On
, and "Queue" = "Transparent"
. If this isn't the case, you may want to post the shader you're using.
No because all objects are in the transparent queue. So one may or may not be drawn first or last. If a object that is farther away is drawn first then great, otherwise ZTest Lequal fails and it is not drawn at all.
I am using the Hard Surface Pro transparent shaders that I modified to work with Unity 5.0. https://www.assetstore.unity3d.com/en/#!/content/724
That's the reason to use the Transparent queue: "this render queue is rendered after Geometry and AlphaTest, in back-to-front order" - http://docs.unity3d.com/$$anonymous$$anual/SL-SubShaderTags.html
I think @tanoshimi has the right idea: selecting the right queue and Ztest should get it working for you. But if you have some special situation that is not covered by these options, you can look at using the stencil buffer in your shaders. " 'If pixel is behind an opaque pixel then don't draw, otherwise blend'? " If you draw the opaque pixel, with a shader of your choice, you can use the "stencil" bit in the opaque shader to specify the pixel should not drawn with subsequent transparent shader calls. Of course, those "subsequent transparent shader calls" will need to include code that specifically checks the stencil bit. http://docs.unity3d.com/$$anonymous$$anual/SL-Stencil.html Edit: sorry, I keep calling it the stencil BIT rather than the stencil BUFFER- probably because that the way I usually USE it ;)