Obtain materials and UE4 Editor Mesh Mesh thumbnail and drag it to the specified replacement materials

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/zhangxiaofan666/article/details/97643308

Task One: Get Material or Mesh thumbnails and rendered as a picture Texture2D

       We know that the picture is displayed in the UMG and UE4 Editor Import from disk into the picture eventually turned into Texture2D format, but thumbnail information is encapsulated in the class with the keyword Thumbnail

practice:

      To get a ObjectThumbnail, obtaining a variety of ways, the following code is only one, but also can take from ue4 memory, faster that way, and then extract the information from the Image ObjectThumbnail get the ObjectThumbnail with TArray <unint8> save, and then obtaining IImageWrapperModule module, create a JPEG format images, and TArray <unint8> turn information into a picture Color of TArray <unint8> information, finally called directly UKismetRenderingLibrary package static functions directly create a Texture2D

UTexture2D* UMyButton::GetTexture2D(UObject* MeshObject)
{
	//UAssetManager& AssetManager = UAssetManager::Get();
	//FAssetData MeshObjectAssetData;
	//AssetManager.GetAssetDataForPath(MeshObject->GetPathName(), MeshObjectAssetData);


	//UAssetManager& AssetManager = UAssetManager::Get();
	//FAssetData MeshObjectAssetData;
	//AssetManager.GetAssetDataForPath(MeshObject->GetPathName(), MeshObjectAssetData);
	//UTexture2D* MyTexture2D = Cast<UTexture2D>(MeshObjectAssetData.GetAsset());
	
	int32 _imageRes = 128;
	FObjectThumbnail _objectThumnail;
	ThumbnailTools::RenderThumbnail(MeshObject, _imageRes, _imageRes, ThumbnailTools::EThumbnailTextureFlushMode::AlwaysFlush, NULL, &_objectThumnail);
	TArray<uint8> _myData = _objectThumnail.GetUncompressedImageData();

	TArray<FColor> _imageRawColor;
	for (int i = 0; i < _myData.Num(); i += 4)
	{
		_imageRawColor.Add(FColor(_myData[i + 2], _myData[i + 1], _myData[i], _myData[i + 3]));
	}
	//FString _fileName = OutputPath.ToString() + "/" + obj->GetName() + FString(".jpg");
	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
	ImageWrapper->SetRaw(_imageRawColor.GetData(), _imageRawColor.GetAllocatedSize(), _imageRes, _imageRes, ERGBFormat::BGRA, 8);
	const TArray<uint8>& _ImageData = ImageWrapper->GetCompressed(100);
	UTexture2D*  MyTexture2D = UKismetRenderingLibrary::ImportBufferAsTexture2D(GetWorld(), _ImageData);
	return MyTexture2D;
}

Note that, in their own test, it was found UKismetRenderingLibrary :: ImportBufferAsTexture2D provides this function UE4.22 version, 4.19 version of the package does not function, you can catch yourself at your own source code package functions

 

Two tasks: to achieve drag mesh material and switching material functions in the editor to customize

Said that under the idea, the material will be dragged out of nothing more than create a window, follow the pointer moves tick, the only place the pointer pointing to the mesh, mesh how to obtain this, there are a number of ways:

比如通过射线碰撞检测实现的,以当前相机位置为原点,光标在屏幕上的2D位置转化成射线方向上的一个点,遍历查询该射线的碰撞网格,返回距离最近的即可

最后将材质替换到mesh上即可

Guess you like

Origin blog.csdn.net/zhangxiaofan666/article/details/97643308