ShaderLab syntax: Blending

Blending is used to make transparent objects.

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.

Syntax

Blend Off
Turn off blending
Blend SrcFactor DstFactor
Configure & enable blending. The generated color is multiplied by the SrcFactor. The color already on screen is multiplied by DstFactor and the two are added together.
Blend SrcFactor DstFactor, SrcFactorA DstFactorA
Same as above, but use different factors for blending the alpha channel.
BlendOp BlendOp
Instead of adding blended colors together, do a different operation on them.

Blend operations

The following blend operations can be used:

AddAdd source and destination together.
SubSubtract source from destination.
RevSubSubtract destination from source.
MinUse the smaller of source and destination.
MaxUse the larger of source and destination.
LogicalClearLogical operation: Clear (0) DX11.1 only.
LogicalSetLogical operation: Set (1) DX11.1 only.
LogicalCopyLogical operation: Copy (s) DX11.1 only.
LogicalCopyInvertedLogical operation: Copy inverted (!s) DX11.1 only.
LogicalNoopLogical operation: Noop (d) DX11.1 only.
LogicalInvertLogical operation: Invert (!d) DX11.1 only.
LogicalAndLogical operation: And (s & d) DX11.1 only.
LogicalNandLogical operation: Nand !(s & d) DX11.1 only.
LogicalOrLogical operation: Or (s | d) DX11.1 only.
LogicalNorLogical operation: Nor !(s | d) DX11.1 only.
LogicalXorLogical operation: Xor (s ^ d) DX11.1 only.
LogicalEquivLogical operation: Equivalence !(s ^ d) DX11.1 only.
LogicalAndReverseLogical operation: Reverse And (s & !d) DX11.1 only.
LogicalAndInvertedLogical operation: Inverted And (!s & d) DX11.1 only.
LogicalOrReverseLogical operation: Reverse Or (s | !d) DX11.1 only.
LogicalOrInvertedLogical operation: Inverted Or (!s | d) DX11.1 only.

Blend factors

All following properties are valid for both SrcFactor & DstFactor in the Blend command. Source refers to the calculated color, Destination is the color already on the screen. The blend factors are ignored if BlendOp is using logical operations.

OneThe value of one - use this to let either the source or the destination color come through fully.
ZeroThe value zero - use this to remove either the source or the destination values.
SrcColorThe value of this stage is multiplied by the source color value.
SrcAlphaThe value of this stage is multiplied by the source alpha value.
DstColorThe value of this stage is multiplied by frame buffer source color value.
DstAlphaThe value of this stage is multiplied by frame buffer source alpha value.
OneMinusSrcColorThe value of this stage is multiplied by (1 - source color).
OneMinusSrcAlphaThe value of this stage is multiplied by (1 - source alpha).
OneMinusDstColorThe value of this stage is multiplied by (1 - destination color).
OneMinusDstAlphaThe value of this stage is multiplied by (1 - destination alpha).

Details

Below are the most common blend types:

Blend SrcAlpha OneMinusSrcAlpha     // Alpha blending
Blend One One                       // Additive
Blend OneMinusDstColor One          // Soft Additive
Blend DstColor Zero                 // Multiplicative
Blend DstColor SrcColor             // 2x Multiplicative

Example

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 }
        }
    }
}

And a more complex one, Glass. This is a two-pass shader:

  1. The first pass renders a lit, alpha-blended texture on to the screen. The alpha channel decides the transparency.
  2. The second pass renders a reflection cubemap on top of the alpha-blended window, using additive transparency.
Shader "Glass" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB) Transparency (A)", 2D) = "white" {}
        _Reflections ("Base (RGB) Gloss (A)", Cube) = "skybox" { TexGen CubeReflect }
    }
    SubShader {
        Tags { "Queue" = "Transparent" }
        Pass {
            Blend SrcAlpha OneMinusSrcAlpha
            Material {
                Diffuse [_Color]
            }
            Lighting On
            SetTexture [_MainTex] {
                combine texture * primary double, texture * primary
            }
        }
        Pass {
            Blend One One
            Material {
                Diffuse [_Color]
            }
            Lighting On
            SetTexture [_Reflections] {
                combine texture
                Matrix [_Reflection]
            }
        }
    }
} 

Page last updated: 2013-08-27