How to properly use DrawMaterialToRenderTarget in C++?

“Renders a quad with the material applied to the specified render target.”

Sound quite straight simple, let’s try it:

UTexture2D* MakeTexture( const FString& name, UWorld* world )
{
	UStaticMesh* plane = LoadObject<UStaticMesh>(nullptr, TEXT("/Engine/BasicShapes/Plane"));
	UTextureRenderTarget2D* target = LoadObject<UTextureRenderTarget2D>(nullptr, TEXT("/Game/Target"));
	UKismetRenderingLibrary::DrawMaterialToRenderTarget(world, target, plane->GetMaterial(0));
	return target->ConstructTexture2D(world, name, EObjectFlags::RF_NoFlags, CTF_DeferCompression);
}

The code above gives a blank texture instead of a quad with applied material.

I’ve the same problem. Still trying to figure it out.
When I use the node in blueprint with the material and render target created by c++ everything works as expected (the rendered texture seems a bit blurred, though).
But when I call the node’s function in c++ I get at most a render target with a uniform color.

One thin: it seems that you have to use an unlit material and connect your color to the “Emissive Color” output.

That said, it works for me with single color debug materials.
Somehow, it also works with a more complex material, but the render target is just filled with a single color. Could be a problem with UVs, somehow. But then I don’t understand why it works in blueprint…

1 Like

I have the exact same problem.
could you find a solution for it ?

I’m pretty sure that you have to wait until the material is done rendering to the render target before constructing a texture 2D or else it will be blank.