Your newSprite
is empty because draw_colored_polygon()
returns nothing.
I've searched around ImageTexture
, BitMap
and such but I can't find an out of the box solution to convert a Polygon2D
to a Texture
. But I think you can do it yourself if you need to.
I see two approaches:
- one is creating a BitMap
yourself, then create a Texture with that bitmap and use it with your button.
- The other is to draw a Polygon2D
and use a Viewport
to render the texture. You then use a ViewportTexture
in your button.
For both method, you'll need to calculate the minimum bounding box to contain your Polygon2D
. This is because you either have to create a BitMap
, or set the size of the Viewport
. Finding the minimum bounding box is a known problem: https://en.wikipedia.org/wiki/Minimum_bounding_box_algorithms
I won't code it for you be here's some pseudo code of how I would approach creating a BitMap
. ( Doc for it: https://docs.godotengine.org/en/stable/classes/class_bitmap.html )
- Get points from CollisionShape (this is your polygon)
- Calculate the minimum bounding box to contain the points
- Create a
BitMap
the size of this rectangle
- Loop on all the pixels of the bitmap
a. If the point is inside your polygon, set the pixel to your desired color
b. If the point is outside, set the pixel transparent
- Create a
Texture
from the BitMap
probably using an Image
While it's a bit complicated, I don't think it's impossible. If anyone has a better solution, please contribute!