Drawing on a Texture

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kutoman

I’m relatively new to Godot. I couldn’t figure out how to draw a node on a texture.
For example, in my case, I try to draw a Polygon2Ds polygon on ImageTexture:

var tex = ImageTexture.new()
var poly: Polygon2D = $Polygon2D

# how to draw poly.polygon on tex ?

I know of draw_polygon, but that method draws on the game screen directly.
I need the texture in order to pass it to another node using textures.

:bust_in_silhouette: Reply From: DigitalDrako

I think you want to make sure your polygon is a child of the texture. That way it shows up on top of the texture and it’s connected to the texture

How can I add a polygon to a texture as a child? As far as I can see the Texture class doesn’t have any method to add a Node or alike. Or do you mean the texture’s node?
But actually it’s crucial that the polygon is actually drawn in the texture itself. To be more precise I need to use that texture as a mask texture having the shape of that polygon.

kutoman | 2020-08-17 09:27

Hmm, I don’t have a lot of experience with polygons. Tell me if one of these helps:

DigitalDrako | 2020-08-17 09:31

I had already checked the latter but the first one seems promissing. I’ve also found out that there is the possibility to capture textures by Viewports, as it is also pointed out in one of the answers from the link you’ve shared: Using Viewports — Godot Engine (stable) documentation in English

thanks for your help!

kutoman | 2020-08-17 10:19

:bust_in_silhouette: Reply From: kutoman

as @DigitalDrako led me to the right direction. This can be achieved by capturing textures via Viewports: