ShaderLab: Blending

El Blending se utiliza para volver los objetos transparentes.

When graphics are rendered, after all Shaders have executed and all Textures have been applied, the pixels are written to the screen. How they are combined with what is already there is controlled by the Blend command.

Additionally, you can set upper-rendertarget blending modes. When
using multiple render target (MRT) rendering, the regular syntax
above sets up the same blending modes for all render targets. The following syntax can set up different blending modes for individual render targets, where N is the render target index (0..7). This feature works on most modern APIs/GPUs (DX11/12, GLCore, Metal, PS4):

Blend N SrcFactor DstFactor

Blend N SrcFactor DstFactor, SrcFactorA DstFactorA

BlendOp N Op

BlendOp N OpColor, OpAlpha

AlphaToMask On: Turns on alpha-to-coverage. When MSAA is used, alpha-to-coverage modifies multisample coverage mask proportionally to the pixel Shader result alpha value. This is typically used for less aliased outlines than regular alpha test; useful for vegetation and other alpha-tested Shaders.

Alpha blending

Un alpha blending regular

This often means that objects have to be considered as “semitransparent”, and thus can’t use some of the rendering features (for example: deferred shading, can’t receive shadows). Concave or overlapping alpha-blended objects often also have draw ordering issues.

Often, alpha-blended Shaders also set transparent render queue, and turn off depth writes. So the Shader code looks like:

Alpha testing/cutout

clip() in pixel Shader

By using clip() HLSL instruction in the pixel Shader, a pixel can be “discarded” or not based on some criteria. This means that object can still be considered as fully opaque, and has no draw ordering issues. However, this means that all pixels are fully opaque or transparent, leading to aliasing (“jaggies”).

Often, alpha-tested Shaders also set cutout render queue, so the Shader code looks like this:

Alpha-to-coverage

AlphaToMask prendida, en 4xMSAA

When using multisample anti-aliasing (MSAA, see QualitySettings), it is possible to improve the alpha testing approach by using alpha-to-coverage GPU functionality. This improves edge appearance, depending on the MSAA level used.