My current goal is to generate a simple image with background texture and text. The image can then be used in an ItemList as an icon for items.
I would like to find out how to:
- draw something using primitives such as drawrect or drawstring off-screen
- get the resulting image or texture
Drawing primitives are not available on Image nor TextureImage.
I've read the viewport-on-a-texture tutorial; it seems to be a bit of overkill to perform such a simple task.
In addition, it requires preparing a scene tree from the GUI for the rendering part. As my work is a utility function that can be called anywhere, I would like to avoid instanciating any nodes for it using the GUI.
All I want is:
Arguments (text, bg color) in ===> Image or ImageTexture out.
So far, I have tried tinkering with viewports and getting the resulting texture; nothing is visible.
Here is an excerpt of my code:
var vport = Viewport.new()
#this is the desired output image size
vport.set_size_override(true, Vector2(64, 32))
var cvitem = Control.new()
var font = load("res://fonts/res/narrative_text.tres")
cvitem.draw_string(font, Vector2(20, 20), "XXX", Color(0.0, 0.0, 1.0))
cvitem.draw_rect(Rect2(0, 0, 100, 100), Color(1.0, 0.0, 0.0), true)
var vport_img = vport.get_texture().get_data()
When displaying the vport_img, all I can see is transparent pixels.
Can I achieve that in Godot 3.1?