[4.13] How to write alpha channel when rendering to RenderTarget from Blueprints

After inspecting source code I found that there’s not correct way to make it. However there’s workaround if one wants to output all 4 channels to the empty render target. Here are the steps:

  • In a blueprint: clear render target to (0, 0, 0, 1) color
  • Make you material Unlit, set blend mode to AlphaComposite
  • Route RGB to Emissive color
  • Route inverse of our Alpha to Opacity color

This way you will have RGBA written into render target.

It works because of blend state descripion. Following code is taken from the sources:

case BLEND_AlphaComposite:
    // Blend with existing scene color. New color is already pre-multiplied by alpha.
    RHICmdList.SetBlendState(TStaticBlendState<CW_RGBA,
        BO_Add, BF_One, BF_InverseSourceAlpha, 
        BO_Add,  BF_Zero, BF_InverseSourceAlpha>::GetRHI());
    break;

Since Dst.RGB is black it will not affect the result. However Alpha equation is SrcAlphaZero+DstInverseSrcAlpha, so by inversing Opacity ouput and clearing render target alpha to 1 you will have what you need.

It’s shame we don’t have the ability to specify custom blend state in a material…

10 Likes