UTexture2D* Texture = nullptr;
Texture = FImageUtils::ImportFileAsTexture2D(TexFilePath);
bool bGenMips = false;
if(bCompressImage)
{
FTextureBuildSettings BuildSettings;
BuildSettings.TextureFormatName = "TFO_AutoDXT";
BuildSettings.MipGenSettings = TMGS_NoMipmaps;
ITextureFormatManagerModule* TFM = GetTextureFormatManager();
if (const ITextureFormat* TextureFormat = TFM->FindTextureFormat(BuildSettings.TextureFormatName))
{
FTexture2DMipMap& MipMap = Texture->GetPlatformData()->Mips[0];
FImage Image;
Image.SizeX = MipMap.SizeX;
Image.SizeY = MipMap.SizeY;
Image.NumSlices = 1;
Image.Format = ERawImageFormat::BGRA8;
Image.RawData.SetNumUninitialized(MipMap.SizeX * MipMap.SizeY * 4);
FMemory::Memcpy(Image.RawData.GetData(), MipMap.BulkData.LockReadOnly(), Image.RawData.Num());
MipMap.BulkData.Unlock();
FCompressedImage2D CompressedImage;
FIntVector3 Extent;
TextureFormat->CompressImage(Image, BuildSettings, Extent, 1, 0, 1, Texture->GetPathName(), false, CompressedImage);
// UE_LOG( LogTemp, Warning, TEXT("MipMap.BulkData.GetElementCount() = %d"), MipMap.BulkData.GetElementCount());
// UE_LOG( LogTemp, Warning, TEXT("CompressedImage.RawData.Num() = %d"), CompressedImage.RawData.Num());
MipMap.FileRegionType = FFileRegion::SelectType(EPixelFormat(CompressedImage.PixelFormat));
MipMap.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(MipMap.BulkData.Realloc(CompressedImage.RawData.Num()), CompressedImage.RawData.GetData(), CompressedImage.RawData.Num());
MipMap.BulkData.Unlock();
Texture->GetPlatformData()->PixelFormat =(EPixelFormat(CompressedImage.PixelFormat));
Texture->UpdateResource();
}
}
UE5 Oodle图像压缩
最新推荐文章于 2024-03-25 23:14:29 发布