0. Z-Buffer and Color-Buffer
There's always a Z-Buffer before a fragment's color can be used. Its color can be wrote into Color-Buffer only when current Z is smaller than Z-Buffer's value. At this moment you can decide whether to change the Z-Buffer value into this one or not.
For a transparent object (A), Z-Write is Off. If a Blend Off object (B) renders after that, B can pass the Z-Test, write its Z into Z-Buffer, and finally write its Blend-Off Color into Color-Buffer. It causes that whether B is in front of A or not, B's opaque color will always replace the color of this pixel.
If B is also transparent, their Blend On color will always write into Color-Buffer, it means that their position is no longer determined by themself, but by the rendering order of the system.
1. Alpha Test
Not only here, when you render any transparent object, you need to declare something in your SubShader Tags.
// some cases:
Tags{"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TranparentCutout"}
Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TranparentCutout"}
However, in alpha test, ZWrite doesn't need to Off.
Clip()
discard current fragment if parameter < 0.
clip(objectValue - clipValue)
Make the clipValue work as a threshold.
2. Alpha Blend
Give each transparent fragment an alpha value.
...
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
...
return fixed4(ambient + diffuse, texColor.a * _Scale);
...
If Z-Write is Off but the object's structure is complex, the rendering sequence will not perform the object structure well.
Double Pass
The first pass is used only to fill the Z-Buffer.
ColorMask 0 means that when fragment passes the Z-Test, fills the Z-Buffer and is ready to fill the Color-Buffer, you tell it to fill zero.
...
// a new pass only used to write something in z-buffer
pass{
ZWrite On
ColorMask 0
}
...
pass{
...
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
...
3. BothSide
A general model certainly doesn't need to render the back, but we shader developers need to consider...
Cull
//AlphaTest
pass{
Cull Off
//AlphaBlend
pass{
Cull Front
...
pass{
Cull Back
HAPPY ENDING! :)
(Chapter 8 is much simpler than Chapter 7... Because I've learned something about blend mode in photoshop XD)