Hi, I'm new to Godot and game development in general and I have a question. In my "game" I need to draw colored shapes such as rectangle. For now I use a sprite node and add a texture of my colored shape to it. I find this a little too heavy, is there any way to just draw colored shapes without loading a texture ? Thank you for your help !
How to draw a simple colored rectangle
You can use the _draw function of CanvasItem to do this. Use either Node2D or Control, whatever is better in your case.
Here is an example:
extends CanvasItem
var rect = [Rect2(Vector2(50,50),Vector2(200,200)),Rect2(Vector2(150,200),Vector2(200,200))]
var color = [Color(1.0,0.0,0.0),Color(0.0,0.0,1.0)]
func _draw():
for i in range(rect.size()):
draw_rect(rect[i],color[i])
func _ready():
update()
It's also possible to draw some other things like circles, textures and lines.
If I recall correctly, there is also the ColoredRect
node, which draws a rectangle with a color property to change it’s color in the editor.
5 years later
TwistedTwigleg Yeah, that works perfectly. Thanks.