UI Shader (2D)

In this shader code:

 

The border_width uniform controls the width of the border.

The corner_radius uniform sets the radius of the rounded corners.

The border_color uniform specifies the color of the border.

To use this shader in Godot:

 

Create a new CanvasItem shader in the Godot editor.

Copy and paste the shader code into the shader editor.

Assign the shader to the material of the 2D node you want to apply the border to.

Adjust the border_width, corner_radius, and border_color parameters in the shader properties to customize the appearance of the border.

This shader will create a flat border UI box with rounded corners on a 2D canvas item, allowing you to easily customize the width, corner radius, and color of the border.

Shader code
shader_type canvas_item;

uniform float border_width : hint_range(0.0, 100.0) = 5.0;
uniform float corner_radius : hint_range(0.0, 50.0) = 10.0;
uniform vec4 border_color : hint_color = vec4(1.0, 1.0, 1.0, 1.0);

void fragment() {
    vec2 size = vec2(1.0, 1.0) - 2.0 * border_width / SCREEN_PIXEL_SIZE;
    vec2 uv = UV * size;

    float dist = length(max(abs(uv) - size + corner_radius, 0.0));
    float border = smoothstep(0.0, border_width / SCREEN_PIXEL_SIZE, dist);

    COLOR = mix(COLOR, border_color, border);
}
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from MilkyDevs

Highlighter Shader (3D)

LevelUp Shader (3D)

Related shaders

far distance water shader (sea shader)

Realistic CRT shader

Slice – 2D pixel perfect slice shader

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
s B
s B
3 months ago

Would like to see this in effect, but I’m using Godot 4.3 and it says border_color was an issue, and I googled to find out that the solution is that it should use source_color instead. But there’s another error saying error(12): Invalid arguments for the built-in function: “smoothstep(float,vec2,float)”. Not experienced enough with shaders to know what this is talking about. Maybe an update needed?