- Home /
Enable sprite depth write.
I'm using the new Unity 2D sprite functionality, it seems that in doing so, none of the sprites write any values to the zbuffer. Is there a simple way to enable zwrite?
Additionally, is there any way to find the source for say the shader behind the Sprites-Default shader?
Answer by Arelius · Nov 24, 2013 at 12:25 AM
I figured this out by downloading the built-in shaders from http://download.unity3d.com/download_unity/builtin_shaders-4.3.0.zip making a duplicate of sprites-default.shader called sprites-zwrite. And within the shader changing the lines:
"RenderType"="Transparent"
and
ZWrite Off
to
"RenderType"="TransparentCutout"
and
ZWrite On
Then using that new shader in all of my sprites.
Hi! i am trying the same thing in Unity 5.1.3. Did exactly what you described, but the sprite is still not writing into z-buffer. I need it for DOF effect to work on my sprites in the scene...
someone got such problems as well?
Hi there, I was having the same issue lately, just recently started messing around with the CG stuff so if someone finds I wrote something wrong/ incorrect feel free to correct me. In Unity5 the Depth buffer is rendered in a separate pass before any geometry, meaning that if you use the default sprite with ZWrite On that won't work for some reason I believe connected with how sprites are handled. If it does work and you manage to write a shader to do it, it will write the whole quad of the sprite into the depth buffer, simply because the way of sprite rendering is done via the alpha blending. On the other hand a the cutout shader discards pixels. I mixed the default sprite shader and the cutout, and everything works just fine. After a few tests, something really strange was happening, but when I used: Fallback "Transparent/Cutout/VertexLit" With that put on the end and a the mixture of cutout and sprite shader, all seem to work just fine. If anyone has an explanation why it would be awesome. I took a look at the vertexlit shader, and I can't really understand whats being so important from it to make the sprites render to the depth buffer...
Thread necromancy, but would you happen to have a copy of that shader handy?