0 votes


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?

in Engine by (12 points)

Do you reached any solution?

1 Answer

0 votes

I don't know if you still need it. Here is what I got. But I am not sure if it is right and what you want cuz I am still new to Godot. It saves an png with XXX on it.

extends Node2D

var vport = Viewport.new()
var vport_img

func _ready():
    vport.size = Vector2(64, 64)
    # or Viewport.UPDATE_ONCE
    vport.render_target_update_mode = Viewport.UPDATE_ALWAYS 
    var cvitem = Control.new()
    cvitem.rect_min_size =  Vector2(64, 64)

func _on_Button_pressed():
    vport_img = vport.get_texture().get_data()

the font is a DynamicFont

extends Control

var font = load("res://font.tres")

func _draw():
    draw_string(font, Vector2(20, 20), "XXX", Color(0.0, 0.0, 1.0, 1.0))
by (59 points)
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 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 webmaster@godotengine.org with your username.