0
点赞
收藏
分享

微信扫一扫

虚幻引擎之动态贴花

伢赞 2022-02-10 阅读 109


UE4之动态贴花

虚幻引擎之动态贴花_epoll

目录



  1. 声明依赖
  2. 测试环境
  3. 事件1:从文件到材质【C++】
  4. 事件1:从文件到材质【蓝图】
  5. 事件2:从组件到投影【C++】
  6. 事件2:从组件到投影【蓝图】
  7. 效果


本文关于如何让用户从电脑中选择图片,自动制作成UE4贴花,并贴到地面上,整个理想的流程如下:

虚幻引擎之动态贴花_epoll_02


声明依赖

在manifest文件[name].build.cs中覆盖以下代码,声明项目中需要调用的功能。

PublicDependencyModuleNames.AddRange(new string[] { 
"Core",
"CoreUObject",
"Engine",
"InputCore" ,
"ImageWrapper",
"SlateCore",
"DesktopPlatform"
});

  • Core:核心标准库,包括数学库和容器类

  • CoreUObject:虚幻根对象,C++反射功能

  • Engine:引擎框架,包括actors和components

  • InputCore:输入设备驱动,包括键盘鼠标

  • ImageWrapper:图片相关的工具

  • DesktopPlatform:桌面UI界面




测试环境

虚幻引擎之动态贴花_js_03

搞一块地板用来贴贴花,一面墙用来辨识方向,设置基本的GameMode来操作玩家:WASD+QE+鼠标移动;鼠标左键投影贴画,鼠标右键选择图片。再创建一个widget提示用户这些操作。

虚幻引擎之动态贴花_js_04


事件1:从文件到材质【C++】


详细API接口:https://docs.unrealengine.com/en-US/API/Developer/DesktopPlatform/IDesktopPlatform/OpenFileDialog/index.html


// 截取自 getFile.cpp 文件


void UgetFile::getFileDialog( const FString& DefaultPath, const FString& FileTypes, TArray& OutFileNames, const bool multiple, const FString& DialogTitle)
{
if (GEngine)
{
if (GEngine->GameViewport)
{
void* ParentWindowHandle = GEngine->GameViewport->GetWindow()->GetNativeWindow()->GetOSWindowHandle();
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
if (DesktopPlatform)
{
uint32 SelectionFlag = multiple? 1:0;
DesktopPlatform->OpenFileDialog(ParentWindowHandle, DialogTitle, DefaultPath, FString(""), FileTypes, SelectionFlag, OutFileNames);
}
}
}
}

事件1:从文件到材质【蓝图】

虚幻引擎之动态贴花_编程语言_05


事件2:从组件到投影【C++】


详细API接口:https://docs.unrealengine.com/en-US/API/Runtime/ImageWrapper/IImageWrapper/index.html


// 截取自 getFile.cpp 文件


UTexture2D* UImageLoader::LoadTexture2D(const FString path, bool& IsValid, int32& OutWidth, int32& OutHeight)
{
UTexture2D* Texture = nullptr;
IsValid = false;
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*path))
{
return nullptr;
}
TArray CompressedData;
if (!FFileHelper::LoadFileToArray(CompressedData, *path))
{
return nullptr;
}
TSharedPtr ImageWrapper = GetImageWrapperByExtention(path);
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(CompressedData.GetData(), CompressedData.Num()))
{
TArray UncompressedRGBA;
if (ImageWrapper->GetRaw(ERGBFormat::RGBA, 8, UncompressedRGBA))
{
Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
if (Texture != nullptr)
{
IsValid = true;
OutWidth = ImageWrapper->GetWidth();
OutHeight = ImageWrapper->GetHeight();
void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedRGBA.GetData(), UncompressedRGBA.Num());
Texture->PlatformData->Mips[0].BulkData.Unlock();
Texture->UpdateResource();
}
}
}
return Texture;
}

事件2:从组件到投影【蓝图】

虚幻引擎之动态贴花_epoll_06



效果

游戏界面:

虚幻引擎之动态贴花_epoll_07

弹窗界面:

虚幻引擎之动态贴花_css_08

<完>



举报

相关推荐

0 条评论