I copied the viewport image to UTextureRenderTarget2D without sacrificing performance.
But, the color of the image in the viewport and the image in the RenderTarget are different.
The widget displays the RenderTarget.
This is the code that copies the viewport image to the UTextureRenderTarget2D.
void UCustom_GameViewportClient::Draw(FViewport* InViewport, FCanvas* Canvas)
{
Super::Draw(Viewport, Canvas);
if (InViewport && PendingCopyViewportToRenderTarget)
{
CopyTextureRHI(InViewport, MyRenderTarget);
}
}
void UCustom_GameViewportClient::CopyViewportToRenderTarget(UTextureRenderTarget2D* RenderTarget)
{
MyRenderTarget = RenderTarget;
PendingCopyViewportToRenderTarget = true;
}
void UCustom_GameViewportClient::CopyTextureRHI(FRenderTarget* MyViewRenderTarget, UTextureRenderTarget2D* DestRenderTarget)
{
ENQUEUE_RENDER_COMMAND(CopyToResolveTarget)(
[MyViewRenderTarget, DestRenderTarget](FRHICommandListImmediate& RHICmdList)
{
FTexture2DRHIRef src = MyViewRenderTarget->GetRenderTargetTexture();
FTexture2DRHIRef dst = DestRenderTarget->GetRenderTargetResource()->GetRenderTargetTexture();
if (src->GetSizeXY() != dst->GetSizeXY())
{
DestRenderTarget->ResizeTarget(src->GetSizeX(), src->GetSizeY());
}
FResolveParams ResolveParams;
RHICmdList.CopyToResolveTarget(src, dst, ResolveParams);
});
FlushRenderingCommands();
}
How can I copy the correct color?
The format of UTextureRenderTarget2D is RTF RGB10A2.