- Home /
Shader with "alpha" parameter no longer writes to Z-Buffer
I'm working on a set of custom shaders to override Unity's built-in terrain shaders.
When I add the "alpha" parameter:
#pragma surface surf Lambert alpha
...suddenly distant terrain trees start drawing on top of nearby terrain hills — it looks as though the terrain shader is no longer writing to the Z-Buffer.
I have ZWrite On
set for my terrain Lightmap-FirstPass
shader, and ZTest Less
set for my BillboardTree
shader.
Any ideas about how I can fix this?
Answer by Owen-Reynolds · May 23, 2012 at 03:13 AM
I believe it's a problem specific to Terrain, and can't really be solved.
Transparent objects need to be drawn back to front(?) after everything else. But, Terrain is drawn by a special routine -- the ground is cut up on the fly into squares, each subdivided based on LOD, then drawn probably outward from the player (for efficiency.) It's probably also run 1st, also for efficiency (these are just guesses.)
I tried a transparent shader with only Terrain, maxed out the "use low-res overmap" setting, and got the same occasional completely transparent hill. Transparent terrain didn't even draw correctly with itself, never mind trees.
Interesting. Thanks, Owen. I would have assumed that, even if the terrain is being drawn by a special segmented geometry renderer, it would still obey shader parameters as usual — but it looks like some internal magic is going on here.
I imagine the chunks could be created and then sent for general rendering. But, for example, each chunk of the same LOD has the same Triangle array. So, the terrain renderer can save time by only sending that once.
Answer by ScroodgeM · Jul 14, 2012 at 12:16 PM
as you use own shader you just need terrain be rendered before trees and writes it's depth to z-buffer, then on tree-drawings stage z-buffer will hide far trees. just try to decrease queue (in shader parameters) value for terrain shader, to be lower then for tree shader