UE4 Runtime下导入IES贴图为TextureLightProfile

编辑器下导入需要用到IESLoadHelper,此类在IESLoader.h中 先拷贝IESLoader.h和IESLoader.cpp到自己目录

主函数:

UTextureLightProfile* XXX::func(const FString& FullFilePath)
{
	TArray<uint8> Result;
    //导入数据
	if (!FFileHelper::LoadFileToArray(Result, *FullFilePath)) return nullptr;
    
    //使用LoadHelper类解析数据
	uint8* Buffer = &Result[0];
	int32 Length;
	Length = Result.Num();
	FIESLoadHelper IESLoadHelper(Buffer, Length);

	if (IESLoadHelper.IsValid())
	{
		TArray<uint8> RAWData;
		float Multiplier = IESLoadHelper.ExtractInRGBA16F(RAWData);
		UTextureLightProfile* Texture = NewObject<UTextureLightProfile>();
        
        //编辑器下使用Source.Init()函数,但不能Runtime下使用
		//if (Texture)
		//{
		//	Texture->Source.Init(
		//		IESLoadHelper.GetWidth(),
		//		IESLoadHelper.GetHeight(),
		//		/*NumSlices=*/ 1,
		//		1,
		//		TSF_RGBA16F,
		//		RAWData.GetData()
		//	);

		//	Texture->AddressX = TA_Clamp;
		//	Texture->AddressY = TA_Clamp;
		//	Texture->CompressionSettings = TC_HDR;
		//	Texture->Brightness = IESLoadHelper.GetBrightness();
		//	Texture->TextureMultiplier = Multiplier;

		//	Texture->UpdateResource();
		//}
		//return Texture;
        
        //仿照UTexture2D::CreateTransient()流程,在BulkData.Lock时写入数据
		if (Texture&&RAWData.GetData())
		{
			EPixelFormat InPixelFormat = EPixelFormat::PF_FloatRGBA;
			/* Create transient texture */
			Texture->PlatformData = new FTexturePlatformData();
			Texture->PlatformData->SizeX = IESLoadHelper.GetWidth();
			Texture->PlatformData->SizeY = IESLoadHelper.GetHeight();
			Texture->PlatformData->PixelFormat = InPixelFormat;

			// Allocate first mipmap.
			int32 NumBlocksX = IESLoadHelper.GetWidth() / GPixelFormats[InPixelFormat].BlockSizeX;
			int32 NumBlocksY = IESLoadHelper.GetHeight() / GPixelFormats[InPixelFormat].BlockSizeY;
			FTexture2DMipMap* Mip = new(Texture->PlatformData->Mips) FTexture2DMipMap();
			Mip->SizeX = IESLoadHelper.GetWidth();
			Mip->SizeY = IESLoadHelper.GetHeight();
			int32 TotalBytes = NumBlocksX * NumBlocksY * GPixelFormats[InPixelFormat].BlockBytes;
               
            //锁死,写入数据
			Mip->BulkData.Lock(LOCK_READ_WRITE);
			uint8* DestData = (uint8*)Mip->BulkData.Realloc(TotalBytes);
			FMemory::Memcpy(DestData, RAWData.GetData(), TotalBytes);
			Mip->BulkData.Unlock();

			if (!Texture) return NULL;
			Texture->PlatformData->NumSlices = 1;
			Texture->NeverStream = true;

			Texture->AddressX = TA_Clamp;
			Texture->AddressY = TA_Clamp;
			Texture->CompressionSettings = TC_HDR;
			Texture->Brightness = IESLoadHelper.GetBrightness();
			Texture->TextureMultiplier = Multiplier;

			Texture->UpdateResource();
		}
		return Texture;
	}
	return nullptr;

}

其他类型贴图也能参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值