Surface2D is a Godot 4 plugin adding polygonal surfaces to draw decals on.
-
Copy
addons/surface_2dfolder intoaddonsdirectory of your project. -
Enable the plugin under
Project > Project Settings... > Plugins.
-
Add a new
Surface2Dnode to the scene. -
Draw a shape of your surface by adding points in the scene view.
-
Set
cull_maskof the surface andvisibility_layerof the canvas item, which you want to draw, to a chosen rendering layer.
- Now when that canvas item intersects with the surface it should be drawn on it. If not, then you might want to refer to the tips section below.
This repo cotains a demo scene, which showcases some possible applications.
The default settings for some of Surface2D properties are stored under Project > Project Settings > Addons > Surface 2D > Defaults. Changing them requires engine restart, since the scripts have to be parsed again.
Polygons representing surfaces, as well as their bounding rectangles, can be made visible in-game for debugging purposes by checking corresponding options under Project > Tools > Surface2D Debugger. This works only when running the game from the editor and not in exported builds.
If a node you want to draw has a parent of CanvasItem type, then that parent must also have an appropriate visibility_layer set. In other words, visibility_layer propagates down the tree, similarly to how transforms do.
visibility_layer and cull_mask of the surface itself should NOT overlap (or the surface will try to draw itself on itself which normally causes a tsunami of errors).
It is a good practice to organize rendering layers by naming them accordingly under Project > Project Settings > Layer Names > 2D Render.
For understanding how mask culling and visibility layers work I recommend this excellent tutorial.
Under the hood the plugin uses SubViewport and Camera2D nodes. Subviewport has the same World2D as the root Viewport. The camera is transformed according to the transform of the Surface2D polygon itself. When it comes to performance, viewports can be quite heavy, but I haven't tested the limits of what this plugin might be capable of yet. One optimization technique, if needed, might involve making polygons smaller and scaling them up to desired size, which would reduce the number of rendered pixels.
This plugin is distributed under MIT License.

