UnrealEngine4 – 将TextureRenderTarget2D保存为图片
1 UE4中将TextureRenderTarget2D保存为图片
为了更易扩展,我们可以将这个函数封装在UE的自定义插件中,首先在UE4中新建插件,然后新建一个C++类,其父类为蓝图函数库类,并且挂靠在新建的插件库中。
假如新建的蓝图函数库为MyBlueprintFunctionBPLibrary,
则MyBlueprintFunctionBPLibrary.h的代码为:
#include "Runtime/Engine/Public/ImageUtils.h"
UCLASS()
class UMyBlueprintFunctionBPLibrary : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
//保存UTextureRenderTarget2D到本地文件
UFUNCTION(BlueprintCallable, meta = (DisplayName = "SaveRenderTargetToFile", Keywords = "SaveRenderTargetToFile"), Category = "SaveToFile")
static bool SaveRenderTargetToFile(UTextureRenderTarget2D* rt, const FString& fileDestination);
};
则MyBlueprintFunctionBPLibrary.cpp的代码为:
bool UMyBlueprintFunctionBPLibrary::SaveRenderTargetToFile(UTextureRenderTarget2D* rt, const FString& fileDestination)
{
FTextureRenderTargetResource* rtResource = rt->GameThread_GetRenderTargetResource();
FReadSurfaceDataFlags readPixelFlags(RCM_UNorm);
TArray<FColor> outBMP;
outBMP.AddUninitialized(rt->GetSurfaceWidth() * rt->GetSurfaceHeight());
rtResource->ReadPixels(outBMP, readPixelFlags);
for (FColor& color : outBMP)
color.A = 255;
FIntPoint destSize(rt->GetSurfaceWidth(), rt->GetSurfaceHeight());
TArray<uint8> CompressedBitmap;
FImageUtils::CompressImageArray(destSize.X, destSize.Y, outBMP, CompressedBitmap);
bool imageSavedOk = FFileHelper::SaveArrayToFile(CompressedBitmap, *fileDestination);
return imageSavedOk;
}
参考链接
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:UnrealEngine4 – 将TextureRenderTarget2D保存为图片
原文链接:https://www.stubbornhuang.com/984/
发布于:2020年11月19日 16:00:05
修改于:2023年06月26日 22:05:40
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论
50