Are there examples anywhere of creating a UTextureCube from an .hdr file at runtime?
The UTexture::Source member is editor only and cannot be used in shipping builds. I’ve got as far as using the FHDRLoadHelper and FDDSLoadHelper classes to get the raw data, but my attempts at populating a dynamically allocated UTextureCube have failed. Typically with a crash down in FTextureCubeResource::GetData as I’ve clearly set things up wrong.
I’ve been able to successfully create UTexture2Ds in a similar fashion leveraging the ImageWrapperModule.
// Create ImageWrapper
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::HDR);
/* Load texturecube */
IsValid = false;
UTextureCube* LoadedT2D = NULL;
//Load From File, shall check if unit8 is useable
TArray<uint8> RawFileData;
//TArray64<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath))
{
UE_LOG(LogTemp, Warning, TEXT("Load file failed"));
return NULL;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UE_LOG(LogTemp, Warning, TEXT("Load file succeed"));
//Create T2D!
// Check if ImageWrapper is valid and set the compressed data
UE_LOG(LogTemp, Warning, TEXT("Ready get raw data"));
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
TArray<uint8> UncompressedBGRA;
//TArray64<uint8> UncompressedBGRA;
UE_LOG(LogTemp, Warning, TEXT("Getting raw data..."));
// ERGBFormat::BGRA may have good performance
// This if shall be test
// The format and/or the bit depth is not supported by the HdrImageWrapper. Only the BGRE format and a bitdepth of 8 is supported
// for test
bool bRawdata = false;
//if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
if (ImageWrapper->GetRaw(ERGBFormat::BGRE, 8, UncompressedBGRA))
{
bRawdata = true;
// Format for HDR:PF_ASTC_8x8_HDR,shall be check if this format is useable
UE_LOG(LogTemp, Warning, TEXT("Ready create TextureCube..."));
LoadedT2D = UTextureCube::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_ASTC_8x8_HDR);
UE_LOG(LogTemp, Warning, TEXT("Create TextureCube"));
//Valid?
if (!LoadedT2D || !LoadedT2D->GetPlatformData())
{
UE_LOG(LogTemp, Warning, TEXT("Create texturecube failed"));
return NULL;
}
//~~~~~~~~~~~~~~
//Copy!
void* TextureData = LoadedT2D->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
// this will case crash
LoadedT2D->GetPlatformData()->Mips[0].BulkData.Unlock();
//Update!
LoadedT2D->UpdateResource();
}
}
return LoadedT2D;
//return LoadTexture2D(FullFilePath,ImageWrapper,IsValid,Width,Height);
But the color and position of imported texturecube is not right …
You can try my plugin @Randy_Croucher1@Andaharoo@Interestingname
I’ve developed RuntimeImageLoader specifically to handle runtime needs. It can load basically any popular image format as well as .HDR images (cubemaps):