Creating 2D color rectangles that are not part of the UI

Version

4.6.3

Question

I’ve been using ColorRect to create colored rectangles that are part of the world, not the UI, but it always seemed wrong to me since Control nodes have many other options that don’t make sense for this use case. I’ve been looking for nodes that do that but inheriting from Node2D and… there is none?

I know you can use Polygon2D, but that’s not as convenient as just dragging handles from the corners of a shape.

It also seems weird to me that you can’t edit a RectangleShape2D or similar classes outside a CollisionShape2D from the editor. You can change their values in the inspector, but you won’t see the lines and handles in the editor. Wouldn’t it be super convenient to have this kind of behaviour to create shapes for other purposes other than physics?

My possible solution?

I’ve developed an addon that adds a RectangleZone2D class, which is a Node2D with a RectangleShape2D, it works exactly the same as CollisionShape2D but without all the physics behaviour. You can set a fill color if you want to use it as a colored rectangle during runtime, or make it only visible in the editor if you want to use it for other purposes.

During runtime it uses the _draw() function, and in the editor it uses _forward_canvas_draw_over_viewport() and _forward_canvas_gui_input() to show the lines/handles and to be able to edit it by dragging the handles.

It didn’t cost much, just a couple of sessions of coding. But I’m shocked that this just doesn’t exists as a base class.

What’s your question? Since you posted in the Help category but it seems like you found a problem and came up with a solution as is. This might be better suited for General or even Resources > Tips & Tricks

Also when I need to use something like ColorRect in 2D and not as a control node, I generally just use a Sprite2D with a GradientTexture2D and re-color it the way I need it.

I posted in help because I’m not sure if I’m overenineering or there is a simpler choice, like the one you mentioned.

Sprite2D with GradientTexture2D is the one I usually go with, and if you don’t want this tile to receive shadows or be affected by any light at all, you can go to it’s properties, Material, create a new CanvasItemMaterial, and set it’s Light Mode to be “Unshaded”.

Seems like an easy workaround, thanks!

Do you also have a workaround for when you want to set a rectangle for logic purposes instead of drawing in the scene? For example, I have a scene where I set several particle emitters, and I don’t want to configure their visibility zone and emitting zone individually, because it’s the same for all of them. I would like to have a rectangle that I can drag in the editor and the script will set their zones automatically.

Using gradient texture would do the trick, but I don’t really need to draw it on screen.

Those are set in the particle process material which is a resource that can be shared between multiple nodes.

If I may be so free to hijack your question a little. Maybe I could interest you in Scalable Vector Shapes 2D?

With it you can also sculpt Bézier curve based colliders and cut holes in them, besides the use case you mentioned. Maybe it’s a little overkill for what you need, then again, maybe it’s not. I made it so you can get rid of the plugin (if you want) before going to production, unless you use its animating curve capabilities.

Yeah, but that would make all the other properties to be shared too, and I don’t want that. Also, I think they don’t have draggable handles either

Cool plugin! I’ll take a look at it because it seems to resolve that problem, yes.

Coming back to the question, then, the answer is that there is no way to have the RectangleShape2D functionality outside a CollisionShape2D node at all. I’m still shocked, to be honest.

Try and see it from the perspective of a core developer / maintainer, though: It’s a massive wishlist that needs to be managed and triaged.

Also, for me (or you) as a hobbyist it’s a lot easier and faster to build all this in gdscript as opposed to c/c++ without having to appease as many game developers as possible.

Use ReferenceRect node. It is control but it doesn’t have any visual representation. It’s basically just an editable rect. You can also put all of those nodes in a group and delete them at startup once you get the information from them.

Yep, totally. It’s just that having draggable handles is an already existing function for many types of nodes, so it’s kind of weird that I have to implement my own code to use it.

Anyway, thank you all for the responses. Closing the thread :+1:t2: