Custom drawing using CanvasItem in gdscript
I am implementing simple game and I am wondering about what solution to choose / is possible.
The task is, to show many different shapes like, rectangles, circles, triangles etc. on screen. Shapes are colored in random color selected by random number generator.
First solution is to prepare many shapes as images and use shaders to color them on the fly (in _on_draw function), or just draw shapes filled in color on the fly.
I like the second one solution as it is more flexible, but I can’t find solution how to do.
My (not working) approach is:
var viewport = VisualServer.viewport_create(); var canvas_item = VisualServer.canvas_item_create(); VisualServer.canvas_item_set_custom_rect(canvas_item, true, Rect2(0,0,50,50)); VisualServer.viewport_set_size( viewport, 50, 50); VisualServer.viewport_attach_canvas(viewport, canvas_item); #this line does not work – shown by debugger VisualServer.draw_circle(canvas_item, Vector2(25,25), 10, Color(1,0,0)); #circle shoud be drawn on canvas var final_texture_rid = VisualServer.viewport_get_texture(viewport); #acquisition background as texture var img_data = VisualServer.texture_get_data(final_texture_rid); #acquisition image data from texture var image_texture = ImageTexture.new(); image_texture.create_from_image(img_data); #texture creation using image data var background = get_node(“background”); var background_canvas_item = background.get_canvas_item(); image_texture.draw( background_canvas_item, Vector2(100, 100)); #showing texture on background starting at point 100, 100 or self.draw_texture(image_texture, Vector2(100, 100)); #self is background – same as line above
I have looked at godot source code on github and it looks like canvas_item created as:
var canvas_item = VisualServer.canvas_item_create();
is not valid, but it is only my feeling, maybe I am wrong.
Can somebody advice how to implement it correctly?
Thanks in advance !