UTexture2D 转为png

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 "";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值