Is there a way to Render UMG to a texture

Hi,
I want to know if it is possible,if yes how, to Render a UMG Widget to a texture and apply it to a mesh.
I want to make a SAO style Healthbar that the opponent will see.

You could use the (experimental) 3D Widget Component instead of a Texture. You wont be able to wrap it around the whole model, but if you only want a UI rendered on a flat surface it works just fine.

Here is a tutorial about this:
https://docs.unrealengine.com/latest/INT/Engine/UMG/HowTo/Create3DWidgets/

1 Like

The only way I have figured out how to do it is to set up a SceneCaptureComponent2D to capture a 3D Widget Component and render it to a render target. However, this will not allow the same level of control over the transparency.

I would recommend checking out this if you know anything about programming in C++

Yeah but you’ll need to create a C++ class to access the needed functions…

Dependances in Build.cs file:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" , "UMG", "Slate", "SlateCore", "RenderCore"});

PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore", "RenderCore" });

H File:

/**
* Renders a widget to a Render Texture 2D with the given draw size.
*
* @param widget      The widget to converted to a Render Texture 2D.
* @param drawSize    The size to render the Widget to. Also will be the texture size.
* @return            The texture render target 2D containing the rendered widget.
*/
UFUNCTION(BlueprintCallable, Category = RenderTexture)
class UTextureRenderTarget2D* WidgetToTexture(class UUserWidget*const widget, const FVector2D &drawSize);

/* UPROPERTY initialized so UE4 will handle its garbage collection correctly... */
UPROPERTY(BlueprintReadWrite, Category = RenderTexture)
class UTextureRenderTarget2D* TextureRenderTarget;

CPP File:

UTextureRenderTarget2D* UComputerWidget::WidgetToTexture(UUserWidget *const widget, const FVector2D &drawSize)
{
	// As long as the slate application is initialized and the widget passed in is not null continue...
	if (FSlateApplication::IsInitialized() && widget != nullptr)
	{
		// Get the slate widget as a smart pointer. Return if null.
		TSharedPtr<SWidget> SlateWidget(widget->TakeWidget());
		if (!SlateWidget) return nullptr;
		// Create a new widget renderer to render the widget to a texture render target 2D.
		FWidgetRenderer* WidgetRenderer = new FWidgetRenderer(true);
		if (!WidgetRenderer) return nullptr;
		// Update/Create the render target 2D.
		TextureRenderTarget = WidgetRenderer->DrawWidget(SlateWidget.ToSharedRef(), drawSize);
		// Return the updated render target 2D.
		return TextureRenderTarget;
	}
	else return nullptr;
}

After all of this has been implemented your going to have to cast the UTextureRenderTarget2D into a UTexture so it can be passed as a texture parameter to the material instance (Only in C++), although you could just add this at the end of the function and just have it return the UTexture to make things easier.

3 Likes

thanks u very much , very nice

Guys, I have just found in 4.21.2 blueprint function GetRenderTarget of WidgetComponent:

3 Likes

This solution leaks memory like nothing else. You need to create the texture like this

TextureRenderTarget = FWidgetRenderer::CreateTargetFor(DrawSize, TF_Bilinear, false);

and then draw with this function:

WidgetRenderer->DrawWidget(TextureRenderTarget, SlateWidget.ToSharedRef(), DrawSize, 0, false);

I thinnk leaks memory may have another following problem:
FWidgetRenderer* WidgetRenderer = new FWidgetRenderer(true);

please take it out from The Tick Function

Bit late to te party but here you go. This plugin does exactly that.
RenderWidgetToTarget-master.zip (9.0 MB)

Source: GitHub - pafuhana1213/RenderWidgetToTarget

3 Likes

Hi, I have some little additions for the above. If you want a texture render target 2d ,you can get it through BP Node that “Create Render Target 2D”. I tried create it by c++,but it doesn’t work. The function is “FWidgetRenderer::CreateTargetFor”,maybe need resource or something ,I couldn’t found it.
Zarrar2802,thank you plugin.It’s useful.