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

When rendering to RenderTarget in a blueprint I need to write all 4 channels of the render target. There’s no problem in writing color values (rgb), but I can’t find the way to write directly to the alpha channel of the render target. Value in alpha channel is not related to color, but will be used as the mask in subsequent calls.

3 Likes

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…

7 Likes

##In 4.17 it worked by:

  • Setting Draw material to AlphaComposite blend mode;
  • Using standard Alpha (not inverted)
  • Setting the mesh/widget material that display the RenderTexture to use inverted alpha output from RenderTexture (OneMinus node)
  • Clear RenderTexture to (0, 0, 0, 1)
3 Likes

Setting the blend mode to AlphaComposite in the particle system Material worked nicely. Thanks.

it works in 4.26

It do not work on mobile in 4.27.

This is a old discussion, AlphaComposite did not work well for me, I share my solution with you. The following setting can be find in Material Editor → Details.

  1. Material Domain: Post Process
  2. Blend Mode: Opaque
  3. Output Alpha: On (This option shows up when using Post Process domain)
  4. Is Blendable: OFF (probably not important as it is already Opaque, but I set it OFF)