UE4_C++_读取本地图片至UE4中使用

时间:2022-11-01 20:03:08

引擎版本:4.17
支持图片类型:png,jpg,bmp,ico,exr,icns
输出图片:UTexture2D
.h

 #include "Engine/Texture2D.h"

UFUNCTION(BlueprintCallable, Category = "Image")        static UTexture2D* LoadTexture2D(const FString path, bool& IsValid, int32& OutWidth, int32& OutHeight); .cpp \#include " ImageTest.h" \#include "FileManagerGeneric.h" \#include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapper.h" \#include "Runtime/ImageWrapper/Public/Interfaces/IImageWrapperModule.h"

//图片类型

static IImageWrapperPtr GetImageWrapperByExtention(const FString InImagePath)
{
    IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
    if (InImagePath.EndsWith(".png"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
    }
    else if (InImagePath.EndsWith(".jpg")||InImagePath.EndsWith(".jpeg"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
    }
    else if (InImagePath.EndsWith(".bmp"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);
    }
    else if (InImagePath.EndsWith(".ico"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO);

    }
    else if (InImagePath.EndsWith("exr"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::EXR);
    }
    else if (InImagePath.EndsWith(".icns"))
    {
        return ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS);
    }
    return nullptr;
}

//根据路径加载贴图

UTexture2D* ImageTest::LoadTexture2D(const FString path,bool& IsValid,int32& OutWidth,int32& OutHeight)
{
    UTexture2D* Texture = nullptr;
    IsValid = false;
    if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*path))
    {
        return nullptr;
    }
    TArray<uint8> RawFileData;
    if (!FFileHelper::LoadFileToArray(RawFileData,*path))
    {
        return nullptr;
    }
    IImageWrapperPtr ImageWrapper = GetImageWrapperByExtention(path);
    if (ImageWrapper.IsValid()&&ImageWrapper->SetCompressed(RawFileData.GetData(),RawFileData.Num()))
    {
        const TArray<uint8>* UncompressedRGBA = nullptr;
        if (ImageWrapper->GetRaw(ERGBFormat::
        ,8,UncompressedRGBA))
        {
            Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);
            if (Texture != nullptr)
            {
                IsValid = true;
                OutWidth = ImageWrapper->GetWidth();
                OutHeight = ImageWrapper->GetHeight();
                void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
                FMemory::Memcpy(TextureData, UncompressedRGBA->GetData(), UncompressedRGBA->Num());
                Texture->PlatformData->Mips[0].BulkData.Unlock();
                Texture->UpdateResource();
            }
        }
    }
    return Texture;

}

.build.cs中添加ImageWrapper模块

另:要解决JPG图片读入UE中出现的Red通道和Blue通道交叉出现的图片颜色错误问题,可将LoadTexture2D中ERGBFormat::RGBA和PF_R8G8B8A8替换为ERGBFormat::BGRA和PF_B8G8R8A8,以BGR存储颜色通道信息即可。