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

引擎版本: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存储颜色通道信息即可。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/justtestlike/article/details/79957505
个人分类: UE4 unreal4
上一篇UE4_Task的创建(多线程任务)
下一篇虚幻引擎中的数组---TArray: Arrays
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭