Texture of sprite through drawn polygon

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

How can I make a drawn polygon (through code) be set as a texture of a TouchScreenButton (whether, normal or pressed)?

Please help.

:bust_in_silhouette: Reply From: Thomas Karcher

I never tried it, but it should be possible by drawing in a small viewport, capturing this viewport and converting the result to a texture:

Thanks for the tip. It works.

However, there is a problem. The entire viewport works as $testButton.normal = tex when I run the following code. In other words, the icon.png gets rendered, when I click anywhere in the output screen.
How do I make the small viewport that you have suggested? When I make a small viewport node as a child of the root viewport, it does not allow drawing of the polygon in the draw function. It does not recognise drawpolygon(points,colour) function in extends Viewport script.

extends Control

func _ready():
	var img = get_viewport().get_texture().get_data()

# Flip on the Y axis.
# You can also set "V Flip" to true if not on the root Viewport.
#	img.flip_y()
# Convert Image to ImageTexture.
	var tex = ImageTexture.new()
	$testButton.normal = tex
	$testButton.pressed = load("res://icon.png")
#	yield(VisualServer, "frame_post_draw")

func _draw():
	var points = PoolVector2Array()
	var colour = PoolColorArray()
	points = [Vector2(100,100), Vector2(200,100), Vector2(200,200),Vector2(100,200)]
	colour = [Color(0,0,1,1)]

Thanks in advance.

ashish | 2021-04-10 02:46