bool UCamerTexture::ExprotUTexture2D(UTexture2D* Img,FString fileDestination)
{
TextureCompressionSettings OldCompressionSettings = Img->CompressionSettings;
//TextureMipGenSettings OldMipGenSettings = m_Texture->MipGenSettings;
bool OldSRGB = Img->SRGB;
Img->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
//m_Texture->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
Img->SRGB = false;
Img->UpdateResource();
FTexture2DMipMap & mipmap = Img->PlatformData->Mips[0];
//unsigned char *Data = (unsigned char *)mipmap.BulkData.Lock(LOCK_READ_WRITE);
int texturex = Img->PlatformData->SizeX;
int texturey = Img->PlatformData->SizeY;
TArray<FColor> nColors;
FColor* FormatedImageData = static_cast<FColor*>(Img->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));
for (int32 y = 0; y < texturey; y++)
{
for (int32 x = 0; x < texturex; x++)
{
FColor bColor;
//bColor.B = Data[(y*texturex + x) * 4 + 0];//B
//bColor.G = Data[(y*texturex + x) * 4 + 1];//G
//bColor.R = Data[(y*texturex + x) * 4 + 2];//R
//bColor.A = Data[(y*texturex + x) * 4 + 3];//A 0:全透明;255:全不透明
//nColors.Add(bColor);
int32 curPixelIndex = (y *texturex) + x;
FColor pixel = FormatedImageData[curPixelIndex];
bColor.B = pixel.R;
bColor.G = pixel.G;
bColor.R = pixel.B;
bColor.A = pixel.A;
nColors.Add(bColor);
}
}
//int32 strwide = (int32)(sizeof(uint8) * 4);
//FMemory::Memcpy(Data, colors.GetData(), strwide*texturey*texturex);
mipmap.BulkData.Unlock();
Img->CompressionSettings = OldCompressionSettings;
//m_Texture->MipGenSettings = OldMipGenSettings;
Img->SRGB = OldSRGB;
Img->UpdateResource();
//保存,思路,拿到Color的数据,然后利用FImageUtils转换图片数据,最后写入
TArray<uint8> ImgData;
FImageUtils::CompressImageArray(texturex, texturey, nColors, ImgData);
bool re = FFileHelper::SaveArrayToFile(ImgData, *fileDestination);
return re;
}
4.27版本修改
if (Img == nullptr)
{
return "";
}
FTexture2DMipMap & mipmap = Img->PlatformData->Mips[0];
unsigned char *Data = (unsigned char *)mipmap.BulkData.Lock(LOCK_READ_WRITE);
int texturex = Img->PlatformData->SizeX;
int texturey = Img->PlatformData->SizeY;
TArray<FColor> nColors;
float bPerUV_v = 1.0 / texturey;
for (int32 y = 0; y < texturey; y++)
{
for (int32 x = 0; x < texturex; x++)
{
FColor bColor;
bColor.B = Data[(y*texturex + x) * 4 + 0];//B
bColor.G = Data[(y*texturex + x) * 4 + 1];//G
bColor.R = Data[(y*texturex + x) * 4 + 2];//R
bColor.A = Data[(y*texturex + x) * 4 + 3];//A 0:全透明;255:全不透明
nColors.Add(bColor);
}
}
//int32 strwide = (int32)(sizeof(uint8) * 4);
//FMemory::Memcpy(Data, colors.GetData(), strwide*texturey*texturex);
mipmap.BulkData.Unlock();
Img->UpdateResource();
//保存,思路,拿到Color的数据,然后利用FImageUtils转换图片数据,最后写入
TArray<uint8> ImgData;
FImageUtils::CompressImageArray(texturex, texturey, nColors, ImgData);
bool re = FFileHelper::SaveArrayToFile(ImgData, *fileDestination);
return "";