欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UE4 Runtime下导入IES贴图为TextureLightProfile

程序员文章站 2022-06-11 11:37:47
...

编辑器下导入需要用到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;

}

其他类型贴图也能参考