zoukankan      html  css  js  c++  java
  • 通过文件路径获取图片纹理

    if (!FPaths::FileExists(FilePath))
           {
                  UE_LOG(LogTemp, Error, TEXT("File not found: %s"), *FilePath);
                  return nullptr;
           }
           // Load the compressed byte data from the file
           TArray<uint8> FileData;
           if (!FFileHelper::LoadFileToArray(FileData, *FilePath))
           {
                  UE_LOG(LogTemp, Error, TEXT("Failed to load file: %s"), *FilePath);
                  return nullptr;
           }
           // Detect the image type using the ImageWrapper module
           IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(TEXT("ImageWrapper"));
           EImageFormat::Type ImageFormat = ImageWrapperModule.DetectImageFormat(FileData.GetData(), FileData.Num());
           if (ImageFormat == EImageFormat::Invalid)
           {
                  UE_LOG(LogTemp, Error, TEXT("Unrecognized image file format: %s"), *FilePath);
                  return nullptr;
           }
           // Create an image wrapper for the detected image format
           IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat);
           if (!ImageWrapper.IsValid())
           {
                  UE_LOG(LogTemp, Error, TEXT("Failed to create image wrapper for file: %s"), *FilePath);
                  return nullptr;
           }
           // Decompress the image data
           const TArray<uint8>* RawData = nullptr;
           ImageWrapper->SetCompressed(FileData.GetData(), FileData.Num());
           ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData);
           if (RawData == nullptr)
           {
                  UE_LOG(LogTemp, Error, TEXT("Failed to decompress image file: %s"), *FilePath);
                  return nullptr;
           }
           // Create the texture and upload the uncompressed image data
           FString TextureBaseName = TEXT("Texture_") + FPaths::GetBaseFilename(FilePath);
           // UObject* Outer = nullptr;
           UTexture2D* mytexture = MyCreateTexture(*RawData, ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), EPixelFormat::PF_B8G8R8A8, FName(*TextureBaseName));
           return mytexture;


    UTexture2D* SDrawHouseTypeTools::MyCreateTexture(const TArray<uint8>& PixelData, int32 InSizeX, int32 InSizeY, EPixelFormat InFormat, FName BaseName)
    {
           // Shamelessly copied from UTexture2D::CreateTransient with a few modifications
           if (InSizeX <= 0 || InSizeY <= 0 ||
                  (InSizeX % GPixelFormats[InFormat].BlockSizeX) != 0 ||
                  (InSizeY % GPixelFormats[InFormat].BlockSizeY) != 0)
           {
                  //UIL_LOG(Warning, TEXT("Invalid parameters specified for UImageLoader::CreateTexture()"));
                  return nullptr;
           }
           // Most important difference with UTexture2D::CreateTransient: we provide the new texture with a name and an owner
    #if 0
           FName TextureName = MakeUniqueObjectName(Outer, UTexture2D::StaticClass(), BaseName);
    #endif
           UTexture2D* NewTexture = NewObject<UTexture2D>(GetTransientPackage(), NAME_None, RF_Transient);
           NewTexture->PlatformData = new FTexturePlatformData();
           NewTexture->PlatformData->SizeX = InSizeX;
           NewTexture->PlatformData->SizeY = InSizeY;
           NewTexture->PlatformData->PixelFormat = InFormat;
           // Allocate first mipmap and upload the pixel data
           int32 NumBlocksX = InSizeX / GPixelFormats[InFormat].BlockSizeX;
           int32 NumBlocksY = InSizeY / GPixelFormats[InFormat].BlockSizeY;
           FTexture2DMipMap* Mip = new(NewTexture->PlatformData->Mips) FTexture2DMipMap();
           Mip->SizeX = InSizeX;
           Mip->SizeY = InSizeY;
           Mip->BulkData.Lock(LOCK_READ_WRITE);
           void* TextureData = Mip->BulkData.Realloc(NumBlocksX * NumBlocksY * GPixelFormats[InFormat].BlockBytes);
           FMemory::Memcpy(TextureData, PixelData.GetData(), PixelData.Num());
           Mip->BulkData.Unlock();
           NewTexture->UpdateResource();
           return NewTexture;
    }
  • 相关阅读:
    Redis入门
    k8s dubbo微服务之maven配置
    NoSQL发展历史与阿里巴巴架构演进分析
    k8s交付dubbo微服务之部署Jenkins
    k8s版本平滑升级
    读 <The Lost Horizon> 感
    luogu P1026 统计单词个数
    acm一些小细节/技巧
    数据结构与算法——常用高级数据结构及其Java实现
    数据结构与算法——常用排序算法及其Java实现
  • 原文地址:https://www.cnblogs.com/huojiaoqingchun0123/p/7083920.html
Copyright © 2011-2022 走看看