做金属小飞机的网站,怎么在各个网站免费推广信息,e龙岩官网下载电脑版,中山市建设工程UE5_加载图片到UTexture __Desc使用方式源码 __Desc
__Time__: 2024-06-05 16:30
__Author__: Yblackd
__Desc__: UE5.2 加载本地图片 转 UTexture2D, 给材质 和 UMG 使用使用方式
新建继承BlueprintFunctionLibrary c 类复制下面源码#xff0c;修改类名实测加载 jpg,jpeg,… UE5_加载图片到UTexture __Desc使用方式源码 __Desc
__Time__: 2024-06-05 16:30
__Author__: Yblackd
__Desc__: UE5.2 加载本地图片 转 UTexture2D, 给材质 和 UMG 使用使用方式
新建继承BlueprintFunctionLibrary c 类复制下面源码修改类名实测加载 jpg,jpeg, png 都可以; 其他未测试
源码
// .h
UFUNCTION(BlueprintCallable, Category UtilityBPLibrary | Texture)
static bool LoadImageToTexture(const FString ImgPath, UTexture2D* Texture2D);
// .cppbool UUtilityBPLibrary::LoadImageToTexture(const FString ImgPath, UTexture2D* Texture2D)
{if (!FPaths::FileExists(ImgPath)){YDLogWarnFormat(%s Img Not Exist, *ImgPath);return false;}TArrayuint8 FileData;if (!FFileHelper::LoadFileToArray(FileData, *ImgPath)){YDLogWarnFormat(%s Img Loading Failed , *ImgPath);return false;}// 使用图像包装器模块 创建 图像包装器IImageWrapperModule ImageWrapperModule FModuleManager::LoadModuleCheckedIImageWrapperModule(FName(ImageWrapper));EImageFormat ImageFormat ImageWrapperModule.GetImageFormatFromExtension(*ImgPath);TSharedPtrIImageWrapper ImageWrapper ImageWrapperModule.CreateImageWrapper(ImageFormat);if (!ImageWrapper.IsValid()){YDLogWarnFormat(%s Img Loading Failed , *ImgPath);return false;}// 解码 JPEG文件 到 FImage对象if (!ImageWrapper-SetCompressed(FileData.GetData(), FileData.Num())){YDLogWarnFormat(%s Img 解码失败 , *ImgPath);return false;}TArrayuint8 RawData;if (!ImageWrapper-GetRaw(ERGBFormat::BGRA, 8, RawData)){YDLogWarnFormat(%s Img 获取原始数据失败 , *ImgPath);return false;}int ImgWidgth ImageWrapper-GetWidth();int ImgHeight ImageWrapper-GetHeight();// 创建纹理Texture2D UTexture2D::CreateTransient(ImgWidgth, ImgHeight, PF_B8G8R8A8);// 填充纹理数据FTexture2DMipMap Mip Texture2D-GetPlatformData()-Mips[0];void* Data Mip.BulkData.Lock(LOCK_READ_WRITE);FMemory::Memcpy(Data, RawData.GetData(), RawData.Num());Mip.BulkData.Unlock();// 设置纹理参数Texture2D-UpdateResource();return true;
}