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?


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.

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