CanvasRenderTarget2D Exemple

Hello,
For a while i’ve been searching how to draw in a texture in realtime.
Apparently in 4.5 CanvasRenderTarget2D was added.
I tried to make it work but i seems to be missing something and cannot find any exemple on how to make it work. Anyone got a little guide or some bullet point on the step to follow to make it work ?

thank you

Anyone ever used this ?

I saw there are no BP tutorials out there so i made this one. I hope it helps.

Step 1.

Create a CanvasRenderTarget2D Blueprint

Add an event (Receive Update).
This event gets called when the canvas is required to update the render target.

This blueprint will draw a red line in the Top Left corner.

Step 2.

Now create a new material. Add a Texture2D Parameter. (Name it Target Texture)

Step 4.

Now, Create a New Material instance using our new material and set the Target Texture to our RenderTarget we first created. (See Step 1.)

Step 5.

Now lets create a new Actor and add a static mesh component to it. Set any mesh. (I used a box this time.)

Add a new Material Instance Dynamic variable and name it Material.

The Construction Script:

And here is how to create & update the render target.

The Update Resource will trigger the Receive Update event in the Canvas blueprint.

Result:

After:

As you can see there it is. The small Red line. Try tweaking with the Canvas and add more draw calls.

I hope this small tutorial was helpful. :slight_smile:

Thank you very much !
There is no tutorial anywere on this… i was trying to get it to work through HUD so i was doing it wrong :slight_smile:

I will flag this thread as answere soon, i just have one more little question about it.

If i want to use oneCanvasRenderTarget to do something like a drawing application, is it even possible ?
i noticed that if i use a simple flip gate, it erase the previous frame anyways to stop that ?

If you want to save your previous frame it would be the best to save it to an another texture. So you have to use 2 render targets.

  • Draw the saved render texture.
  • Draw anything you want on top of it.
  • Draw the final texture to a second render target. (Save it)

Without knowing what are you up to. i recommend to redraw your image every frame. I am not sure what do you need. :slight_smile:

Thanks for the tutorial. I created this standalone project to try out your instructions. Might be helpful to others.

Hello, is it possible to do this in C++? I tried reproduce itm but got lost in BP calls, I need to draw line (or simple shape, basicly UCanvas know to draw everything i need) on texture (or better create texture).

Hello, Azarus,

Thank you for your tutorial. I have a question: What is the use of the Render Target that you create at first? Is it necessary for memory or something similar? I see that the canvas is not writing to the render target texture, and it is not referenced further, only as an initial value for the material instance.

I ask because as far as I understand, the CanvasRenderTarget2D is itself a kind of render target, that gets initialized with its own size etc. And when you set the material texture parameter to the canvas, as far as I understand, it is overriding your original render target texture, and that is not used further?

Trying to understand this better. Thank you!

Yeah Diego is right you could skip the creation of that Render Target

Right, thanks for the info it was stupid indeed. :slight_smile: