Create a RenderTarget2D and ClearColor; Alpha to 0
Drawing with the BeginDrawCanvasToRenderTarget function
When BlendMode is AlphaComposition(…), I can draw on a transparent RenderTarget, but it comes out unsmooth and jagged that way.
The top left corner is the thumbnail, which is an Image, and the code for it to get the color information is:
void UMyKismetRenderingLibrary::CopyRenderTextureToRenderTexture(const UObject* WorldContextObject,
UTextureRenderTarget2D* InRenderTarget,
UTextureRenderTarget2D* OutputRenderTarget,
FVector2D SrcPosition, FVector2D CaptureSize, bool SrcTransparent)
{
UWorld* World = WorldContextObject->GetWorld();
ERHIFeatureLevel::Type FeatureLevel = World->Scene->GetFeatureLevel();
if (FeatureLevel < ERHIFeatureLevel::SM5)
{
UE_LOG(LogTemp, Warning, TEXT("FeatureLevel < ERHIFeatureLevel::SM5"));
return;
}
if (nullptr == InRenderTarget)
{
UE_LOG(LogTemp, Warning, TEXT("InTexture Is NULL"));
return;
}
UKismetRenderingLibrary::ClearRenderTarget2D(WorldContextObject->GetWorld(), OutputRenderTarget, FLinearColor(0, 0, 0, 0));
OutputRenderTarget->ResizeTarget(CaptureSize.X + 1, CaptureSize.Y + 1);
FTextureRenderTargetResource* TextureRenderTargetOut = OutputRenderTarget->GameThread_GetRenderTargetResource();
FTextureRenderTargetResource* TextureRenderTargetInput = InRenderTarget->GameThread_GetRenderTargetResource();
//FTexture* TextureSource = InTexture->Resource;
// 应该为0
int32 CommonMip = FMath::Min(TextureRenderTargetOut->TextureRHI->GetNumMips(), TextureRenderTargetInput->TextureRHI->GetNumMips());
// 渲染命令
ENQUEUE_RENDER_COMMAND(TestShaderCommand)(
[TextureRenderTargetInput, TextureRenderTargetOut, FeatureLevel, CommonMip, SrcPosition, CaptureSize, SrcTransparent](FRHICommandListImmediate& RHICmdList)
{
if (CommonMip >= 2)
{
//Test Copy Subregion
for (int32 MipIndex = 0; MipIndex < CommonMip - 1; ++MipIndex)
{
// 要复制的信息
FRHICopyTextureInfo CopyInfo;
// 起点
CopyInfo.SourcePosition = FIntVector(0, 0, 0);
CopyInfo.DestPosition = FIntVector(0, 0, 0);
CopyInfo.SourceMipIndex = MipIndex;
CopyInfo.DestMipIndex = MipIndex;
// 大小
CopyInfo.Size = FIntVector(TextureRenderTargetInput->GetSizeX() / FMath::Pow(2, MipIndex + 1),
TextureRenderTargetInput->GetSizeY() / FMath::Pow(2, MipIndex + 1),
TextureRenderTargetInput->GetSizeZ() / FMath::Pow(2, MipIndex + 1));
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetOut->TextureRHI,
ERHIAccess::Unknown, ERHIAccess::CopyDest));
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetInput->TextureRHI,
ERHIAccess::SRVMask, ERHIAccess::CopySrc));
FRHITexture* Test = TextureRenderTargetInput->TextureRHI.GetReference();
// 提取图像
RHICmdList.CopyTexture(TextureRenderTargetInput->TextureRHI,
TextureRenderTargetOut->TextureRHI, CopyInfo);
// 写入底图
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetOut->TextureRHI,
ERHIAccess::CopyDest, ERHIAccess::SRVMask));
// 覆盖提取图
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetOut->TextureRHI,
ERHIAccess::CopySrc, ERHIAccess::SRVMask));
}
}
else
{
FRHICopyTextureInfo CopyInfo;
// src起始位置
CopyInfo.SourcePosition = FIntVector(SrcPosition.X, SrcPosition.Y, 0);
// Dest起始位置
CopyInfo.DestPosition = FIntVector(0, 0, 0);
// 截取大小
CopyInfo.Size = FIntVector(CaptureSize.X, CaptureSize.Y, TextureRenderTargetInput->GetSizeZ());
// 备份为Dest
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetOut->TextureRHI,
ERHIAccess::Unknown, ERHIAccess::CopyDest));
// 备份为Src
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetInput->TextureRHI,
ERHIAccess::SRVMask, ERHIAccess::CopySrc));
// 截取Src图像到Dest,根据CopyInfo
RHICmdList.CopyTexture(TextureRenderTargetInput->TextureRHI,
TextureRenderTargetOut->TextureRHI, CopyInfo);
//FIntVector xyz = TextureRenderTargetInput->TextureRHI->GetSizeXYZ();
//UE_LOG(LogTemp, Log, TEXT("SIZE: x = %i, y = %i"), xyz.X, xyz.Y);
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetOut->TextureRHI,
ERHIAccess::CopyDest, ERHIAccess::SRVMask));
RHICmdList.Transition(FRHITransitionInfo(TextureRenderTargetOut->TextureRHI,
ERHIAccess::CopySrc, ERHIAccess::SRVMask));
}
}
);
// 等待渲染线程完成所有挂起的渲染命令的执行。只能从游戏线程中使用。
FlushRenderingCommands();
}
Moving on, to change this, I changed the BlendMode to Translucent
It works well, but the TextureRHI output of the RenderTarget is fully transparent, with no color information at all.
If I call ClearColor for the RenderTarget; Alpha is 1
I can then get the color information of the TextureRHI, but it becomes opaque
What settings do I have to change or what code do I have to refer to in order to output color information with transparency?
**This is a translation. It may not make sense.