This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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

Please help.

Godot version 3.2.2
related to an answer for: Drawing a polygon 2d
in Engine by (101 points)

1 Answer

0 votes

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:

https://docs.godotengine.org/en/stable/tutorials/viewports/viewports.html#capture

by (1,762 points)

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()
    tex.create_from_image(img)
    $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)]
    draw_polygon(points,colour)

Thanks in advance.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.