UE4 RenderTexture转UTexture2D

4 篇文章 0 订阅

运行时RenderTexture转UTexture2D

#include "Engine/TextureRenderTarget2D.h"

UTexture2D* ATester::GetCurGameraRT(UTextureRenderTarget2D* RT)
{
    if (RT != nullptr)
    {
        UTexture2D* Result = NULL;

        // Check render target size is valid and power of two.
        const bool bIsValidSize = (RT->SizeX != 0 && !(RT->SizeX & (RT->SizeX - 1)) &&
        RT->SizeY != 0 && !(RT->SizeY & (RT->SizeY - 1)));
        // The r2t resource will be needed to read its surface contents
        FRenderTarget* RenderTarget = RT->GameThread_GetRenderTargetResource();

        const EPixelFormat PixelFormat = RT->GetFormat();
        ETextureSourceFormat TextureFormat = TSF_Invalid;
        switch (PixelFormat)
            {
                case PF_B8G8R8A8:
                    TextureFormat = TSF_BGRA8;
                    break;
                case PF_FloatRGBA:
                    TextureFormat = TSF_RGBA16F;
                    break;
                case PF_G8:
                    TextureFormat = TSF_G8;
                    break;
                default:
                    {
                        return nullptr;
                    }
            }

        // exit if source is not compatible.
        if (bIsValidSize == false || RenderTarget == NULL || TextureFormat == TSF_Invalid)
        {
            return Result;
        }

        // create the 2d texture
        Result = UTexture2D::CreateTransient(RT->SizeX, RT->SizeY, PF_B8G8R8A8);

        // Lock the texture so it can be modified
        auto& BulkD = Result->PlatformData->Mips[0].BulkData;
        uint8* MipData = static_cast<uint8*>(BulkD.Lock(LOCK_READ_WRITE));

        TextureCompressionSettings CompressionSettingsForTexture = TC_HDR;

        TArray<FColor> SurfData;
        RenderTarget->ReadPixels(SurfData);

        // read the 2d surface
        if (TextureFormat == TSF_BGRA8)
        {
            check(BulkD.GetBulkDataSize() == SurfData.Num() * sizeof(FColor));
            FMemory::Memcpy(MipData, SurfData.GetData(), BulkD.GetBulkDataSize());
        }
        Result->LODGroup = TextureGroup::TEXTUREGROUP_UI;
        Result->SRGB = false;
        Result->CompressionSettings = CompressionSettingsForTexture;
        Result->Filter = TextureFilter::TF_Trilinear;
        BulkD.Unlock();
        Result->UpdateResource();
        return Result;
    }
    return nullptr;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值