【Alpha Testing】
The alpha test is a last chance to reject a pixel from being written to the screen.
After the final output color has been calculated, the color can optionally have its alpha value compared to a fixed value. If the test fails, the pixel is not written to the display.
在最终颜色(pixel-color)被计算后,可以通过Alpha Test来排除顶点。
[Syntax]
- AlphaTest Off
- Render all pixels (default).
- AlphaTest comparison AlphaValue
- Set up the alpha test to only render pixels whose alpha value is within a certain range.
Comparison
AlphaValue
A floating-point number between 0 and 1. This can also be a variable reference to a float or range property, in which case it should be written using the standard square bracket notation ([VariableName]).
[Alpha测试的用途]
对于半透明的凹形图形,用Alpha测试与Blending配合使用才能产生较好的效果。
左图:ZWrite On,AlphaTest Equal 1.0。由于Alpha均为1,所以无Blending。在此条件下产生出一个锐利的图像。
中图:ZWrite On,AlphaTest Off。在此条件下,Alpha为0的部分会遮挡住Alpha非零的部分,产生奇怪的图。
右图:1)ZWrite On,AlphaTest Equal 1.0。此遍渲染非透明物体。
2)ZWrite Offet,AlphaTest Less 1.0。此遍渲染透明物体。
右图能够产生较为完美的图像。
右图的Shader如下:
Shader "Vegetation" { Properties { _Color ("Main Color", Color) = (.5, .5, .5, .5) _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {} _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5 } SubShader { // Set up basic lighting Material { Diffuse [_Color] Ambient [_Color] } Lighting On // Render both front and back facing polygons. Cull Off // first pass: // render any pixels that are more than [_Cutoff] opaque Pass { AlphaTest Greater [_Cutoff] SetTexture [_MainTex] { combine texture * primary, texture } } // Second pass: // render in the semitransparent details. Pass { // Dont write to the depth buffer ZWrite off // Don't write pixels we have already written. ZTest Less // Only render pixels less or equal to the value AlphaTest LEqual [_Cutoff] // Set up alpha blending Blend SrcAlpha OneMinusSrcAlpha SetTexture [_MainTex] { combine texture * primary, texture } } } }