Hi i am quite new to programming and unreal engine.
i want to learn how to load texture at runtime and so that i was trying to analyse Rama’s Victory plugin.
here is the code i was analyzing.
UTexture2D* UVictoryBPFunctionLibrary::Victory_LoadTexture2D_FromFile(const FString& FullFilePath,EJoyImageFormats ImageFormat, bool& IsValid,int32& Width, int32& Height)
{
IsValid = false;
UTexture2D* LoadedT2D = NULL;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(GetJoyImageFormat(ImageFormat));
//Load From File
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath)) return NULL;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
const TArray<uint8>* UncompressedBGRA = NULL;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
//Valid?
if(!LoadedT2D) return NULL;
//~~~~~~~~~~~~~~
//Out!
Width = ImageWrapper->GetWidth();
Height = ImageWrapper->GetHeight();
//Copy!
void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();
//Update!
LoadedT2D->UpdateResource();
}
}
// Success!
IsValid = true;
return LoadedT2D;
}
as i researched, i guess code flow works like this.
- Get ImageWrapper (which uses for handle binary file as Image file follow by given format) from ImageWrapperModule (which the manager like class for image wrapper).
- Use FFileHelper to read binary file and store it’s binary data in an array.
- Put that binary data into ImageWrapper so that wrapper convert binary data into image data.
- Extract image data from ImageWrapper to UTexture2D variable.
- Check image extracting work was succeed, and set return variables (width, height).
(please let me know if i was wrong) so the question is, why it does copying work?
it locks the texture’s memory and copy that into TextureData variable which is local variable which will be gone when function ended, than unlock it.
i think that is totally meaningless unless you edit or actually copy for other place.
Plus, i have two small questions.
- when lock the texture, it locks the data of first index of Mipmap array which stored in PlatformData. but how this code sure there is at lease one mipmap data? is unreal create texture’s mipmap automatically? or default texture size is first index of mipmap array?
- why it uses RGB format as BGRA? what is diffrence with ARGB or RGBA ?
i am quite new to this community and Eng is not my first language, so if my behavior seems rude or something like that, please let me know.
thanks for reading