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 Min | Max | Sub | RevSub
Instead of adding blended colors together, do a different operation on them.

Properties

All following properties are valid for both SrcFactor & DstFactor. Source refers to the calculated color, Destination is the color already on the screen.

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: 2012-05-31