void saveTexut(UTexture2D* tex,FString filename)
{
TextureCompressionSettings OldCompressionSettings = tex->CompressionSettings; TextureMipGenSettings OldMipGenSettings = tex->MipGenSettings; bool OldSRGB = tex->SRGB;
tex->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
tex->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
tex->SRGB = false;
//tex->UpdateResource();
FTexture2DMipMap* MM = &tex->PlatformData->Mips[0];
TArray<FColor> OutBMP;
int w = MM->SizeX;
int h = MM->SizeY;
OutBMP.InsertZeroed(0, w*h);
FByteBulkData* RawImageData = &MM->BulkData;
//const void* Data = (tex->PlatformData->Mips[0].BulkData.LockReadOnly());
FColor* FormatedImageData = static_cast<FColor*>(RawImageData->Lock(LOCK_READ_ONLY));
//uint8* pixelData = static_cast<uint8*>(RawImageData->Lock(LOCK_READ_ONLY));
//const uint8* PPixelData = (uint8*)Data;
//uint8* pixelData = nullptr;
for (int i = 0; i < (w*h); ++i)
{
OutBMP[i] = FormatedImageData[i];
//OutBMP[i].A = 255;
}
//FMemory::Memset(pixelData, 0, w*h * 4);
//FMemory::Memcpy(pixelData, FormatedImageData,w * h);
RawImageData->Unlock();
tex->CompressionSettings = OldCompressionSettings;
tex->MipGenSettings = OldMipGenSettings;
tex->SRGB = OldSRGB;
tex->UpdateResource();
TArray<uint8> compressedBitmap;
FImageUtils::CompressImageArray(w, h, OutBMP, compressedBitmap);
FFileHelper::SaveArrayToFile(compressedBitmap, *filename);
}
I use this function to get pixels of Texture2D. But TArray is all zeros and the saved png is blank with nothing. Anyone else knows how to solve it.