RDG - Rendering Material to Render Target Doesn't Give Correct Alpha Channel

I’m rendering a material to a render target on the render thread, the material renders fine except there are no alpha channel values other than 0. I’ve tried setting the material to be a Post Process material with alpha out enabled and I’ve tried Surface > Unlit > Alpha Composite Blend/Translucent/Masked and no luck.

The render target format is set as RGBA16f so the RT has alpha enabled.

Does anyone have any idea? I’ve seen others with a similar issue but using the BP function to do it.

To render on the render thread I use the following code. It’s run inside a Scene View Extension function and is based on the UKismetRenderingLibrary::DrawMaterialToRenderTarget() function.

// Get the material
UMaterialInterface* MaterialInterface = Resource->GetMaterial();
UMaterial* Material = MaterialInterface->GetMaterial();

// Ensure it's compiled
Material->EnsureIsComplete()

const FMaterialRenderProxy* MaterialRenderProxy = MaterialInterface->GetRenderProxy();
		
FRDGTextureRef TemporaryTexture = Resource->CreateTemporaryTexture(GraphBuilder);
FVector2D TempTextureSize = FVector2D(TemporaryTexture->Desc.Extent.X, TemporaryTexture->Desc.Extent.Y);
FScreenPassRenderTarget TemporaryTextureRenderTarget = FScreenPassRenderTarget(TemporaryTexture, ERenderTargetLoadAction::ENoAction);

check(TemporaryTextureRenderTarget.IsValid());

// Create the tile item used for rendering the material
FCanvasTileItem TileItem(FVector2D::ZeroVector, MaterialRenderProxy, TempTextureSize);

const FSceneViewFamily& ViewFamily = *View.Family;

FCanvas& RenderCanvas = *FCanvas::Create(GraphBuilder, TemporaryTextureRenderTarget.Texture, nullptr, ViewFamily.Time, View.GetFeatureLevel());
RenderCanvas.SetRenderTargetRect(TemporaryTextureRenderTarget.ViewRect);
RenderCanvas.DrawItem(TileItem);
RenderCanvas.Flush_RenderThread(GraphBuilder);

// Copy the texture to the render target
URenderTargetProxy* OutRenderTarget = Resource->GetRenderTarget();
FRDGTextureRef OutRenderTargetTexture = Resource->GetPooledTextureRef();
AddCopyTexturePass(GraphBuilder, TemporaryTexture, OutRenderTargetTexture);

Hi, Does the Material use “Pre-Multiply Alpha” mode, and you’re plugging in 1-alpha to the alpha?

Yup, this is how the material output is currently set up:

In the render target, the result I get is just the green outline but the alpha channel is then all 0.

Also, I’m using 5.3 for this, in case that makes any difference

Before doing the Draw, Clear the render target to 0,0,0,1 - and add a “1-” between the Mask A and the Opacity - that should get it working.

That did the job, thank you!!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.