I’m currently working on a system which uses the Canvas system to draw other assets (such as text, textures, etc) onto a TextureRenderTarget2D.
These elements are then overlayed on top of another texture in a material according to the rendered texture’s alpha channel.
However, Canvas’ handling of alpha channels appears to be pretty much completely random.
I was able to get basic stuff running by setting the ClearColor of the TextureRenderTarget2D to be completely translucent, which gives me a translucent base to start drawing to.
When I draw text to the canvas via an FCanvasTextItem, by default, the text is drawn to the color channels of the TextureRenderTarget2D, but it leaves the Alpha unchanged from the base transparency despite the color of the CanvasTextItem set to be opaque. However, I was able to get this to draw to the alpha properly by setting the CanvasTextItem’s BlendMode to ESimpleElementBlendMode::SE_BLEND_Opaque. However, this has the side effect of doing weird things to the color channels: the text’s color is drawn to the texture as just a rectangular blob, though only the letters are visible since only the actual font is drawn to the alpha channel. So that’s okay, I guess.
But when it comes to drawing other CanvasItems to the Canvas (specifically, FCanvasBoxItems and FCanvasLineItems) nothing I do seems to make the items draw to the TextureRenderTarget2D’s alpha channel. The color of the items are completely opaque, and I’ve tried going through every single option for ESimpleElementBlendMode with no effect.
I know the drawing is otherwise working, since I can see the boxes and lines in the color channels of the texture. But without them setting the alpha, they’re invisible.
There’s a bit of suspicious code in the line drawing function:
// Ensure the line isn’t masked out. Some legacy code relies on Color.A being ignored.
FLinearColor OpaqueColor(Color);
OpaqueColor.A = 1;
But given that it’s setting the alpha to entirely opaque, I don’t see why this would cause problems, but may indicate some weirdness around the handling of transparency elsewhere. Does anyone know what the deal is with CanvasItems and transparency? Is there a correct way of doing this?