Version: 2019.4
ShaderLab culling and depth testing
Синтаксис ShaderLab: тэги Pass

Синтаксис ShaderLab: Blending

Блендинг (Blending) используется при создании прозрачных объектов.

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.

Синтаксис

Blend Off: Выключить блендинг

Blend SrcFactor DstFactor: Включить и настроить блендинг. Новый (только что вычисленный) цвет умножается на SrcFactor. Цвет, который уже есть на экране, умножается на DstFactor, после чего оба цвета складываются.

Blend SrcFactor DstFactor, SrcFactorA DstFactorA: То же, что и выше, но для альфа каналов используются отдельные множители.

BlendOp Op: Instead of adding blended colors together, carry out a different operation on them.

Blend SrcFactor DstFactor, SrcFactorA DstFactorA: То же, что и выше, но для альфа каналов используются отдельные множители.

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.

Операции команды Blend

Можно использовать следующие операции блендинга:

Add Новый и старый цвета складываются.
Sub Вычесть старый цвет из нового.
RevSub Вычесть новый цвет из старого.
Min Выбрать минимальное значение между новым и старым цветом.
Max Выбрать максимальное значение между новым и старым цветом.
LogicalClear Логическая операция: Clear (0) только для DX11.1.
LogicalSet Логическая операция: Set (1) только для DX11.1.
LogicalCopy Логическая операция: Copy (s) только для DX11.1.
LogicalCopyInverted Логическая операция: Copy inverted (!s) только для DX11.1.
LogicalNoop Логическая операция: Noop (d) только для DX11.1.
LogicalInvert Логическая операция: Invert (!d) только для DX11.1.
LogicalAnd Логическая операция: And (s & d) только для DX11.1.
LogicalNand Логическая операция: Nand !(s & d) только для DX11.1.
LogicalOr Logical operation: Or (s | d) DX11.1 only.
LogicalNor Logical operation: Nor !(s | d) DX11.1 only.
LogicalXor Логическая операция: Xor (s ^ d) только для DX11.1.
LogicalEquiv Логическая операция: Equivalence !(s ^ d) только для DX11.1.
LogicalAndReverse Логическая операция: Reverse And (s & !d) только для DX11.1.
LogicalAndInverted Логическая операция: Inverted And (!s & d) только для DX11.1.
LogicalOrReverse Logical operation: Reverse Or (s | !d) DX11.1 only.
LogicalOrInverted Logical operation: Inverted Or (!s | d) DX11.1 only.

Коэффициенты для команды Blend

В команде Blend, все представленные ниже свойства применимы и к SrcFactor, и к DstFactor. Source - новый вычисленный цвет, Destination - цвет, который уже присутствует на экране. Эти свойства игнорируются, если в качестве BlendOp выбрана любая логическая операция.

One Единица — на экран будет выведен либо только новый цвет, либо только старый.
Zero Ноль — из конечного отображения будет полностью убран старый или новый цвет.
SrcColor Значение умножается на значение нового цвета.
SrcAlpha Значение умножается на значение альфа канала нового цвета.
DstColor Значение умножается на значение уже присутствующего на экране цвета.
DstAlpha Значение умножается на значение альфа канала уже присутствующего на экране цвета.
OneMinusSrcColor Значение умножается на (1 - srcColor), где srcColor - значение нового цвета.
OneMinusSrcAlpha Значение умножается на (1 - srcAlpha), где srcAlpha - значение альфа канала нового цвета.
OneMinusDstColor Значение умножается на (1 — dscColor), где dstColor - значение уже присутствующего на экране цвета.
OneMinusDstAlpha Значение умножается на (1 — dstAlpha), где dstAlpha - значение альфа канала цвета, уже присутствующего на экране.

Детали

Ниже показаны наиболее часто встречающиеся варианты блендинга:

Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
Blend One OneMinusSrcAlpha // Premultiplied transparency
Blend One One // Additive
Blend OneMinusDstColor One // Soft Additive
Blend DstColor Zero // Multiplicative
Blend DstColor SrcColor // 2x Multiplicative

Alpha blending, alpha testing, alpha-to-coverage

For drawing mostly fully opaque or fully transparent objects, where transparency is defined by the Texture’s alpha channel (e.g. leaves, grass, chain fences etc.), several approaches are commonly used:

Alpha blending

Regular alpha blending
Regular alpha blending

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:

// inside SubShader
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }

// inside Pass
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

Alpha testing/cutout

clip() in pixel Shader
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:

// inside SubShader
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }

// inside CGPROGRAM in the fragment Shader:
clip(textureColor.a - alphaCutoffValue);

Alpha-to-coverage

AlphaToMask On, at 4xMSAA
AlphaToMask On, at 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.

This functionality works best on texures that are mostly opaque or transparent, and have very thin “partially transparent” areas (grass, leaves and similar).

Often, alpha-to-coverage Shaders also set cutout render queue. So the Shader code looks like:

// inside SubShader
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }

// inside Pass
AlphaToMask On

Пример

Here is a small example Shader that adds a Texture to whatever is on the screen already:

Shader "Simple Additive" {
    Properties {
        _MainTex ("Texture to blend", 2D) = "black" {}
    }
    SubShader {
        Tags { "Queue" = "Transparent" }
        Pass {
            Blend One One
            SetTexture [_MainTex] { combine texture }
        }
    }
}
ShaderLab culling and depth testing
Синтаксис ShaderLab: тэги Pass