Draw Text on a texture

Is there a way to draw text (using specific Font) on a Texture and/or Material (like Canvas->DrawText() function draws in on screen)? For example, I have some FString (or similar) properties for an Actor (license plate or racing number on a car, player number in sports etc.), and I want them to appear on the actor’s mesh (for example, using a decal with dynamic material instance or something similar). How can this be achieved in UE4?

Did you found solution? I am looking for similiar/same thing i need draw simple shapes on UTexture2D.

Positioning a UTextRenderComponent is probably the easiest way to do what you are asking.

@kexik @psychogony I’m now using both UTextRenderComponent and the GUI widgets (drawn on a mesh), however the latter isn’t the best option for performance AFAIK.

You’ll need a render target:

RenderTexture = PCIP.CreateDefaultSubobject<UTextureRenderTarget2D>(this, TEXT("RenderTexture"));

You’ll want to initialize it:

RenderTexture->ClearColor = FLinearColor::Black;
RenderTexture->InitAutoFormat(iWidth, iHeight);
RenderTexture->UpdateResourceImmediate();

And then you want to do some drawing to it:

FCanvas canvas(RenderTexture->GameThread_GetRenderTargetResource(), NULL, 0.0f, 0.0f, 0.0f, GMaxRHIFeatureLevel);
renderFn(canvas);
canvas.Flush_GameThread(true);

Look here for renderable items: Engine\Source\Runtime\Engine\Public\CanvasItem.h - FCanvasTextItem is the simple text renderer.

The RenderTexture can be used as a UTexture converting it to a UTexture2D requires further work (and I have yet to get it working on iOS) but a UTexture works for more normal rendering cases.

I don’t have a good solution for clipping and haven’t looked at alpha much (currently solution I have a second alpha texture that I render the text to, ugly but not been a pressing issue to fix yet).

1 Like

Thank you! Will try some time later.