Skip to content

Instantly share code, notes, and snippets.

@hcarty
Created July 6, 2024 15:20
Show Gist options
  • Save hcarty/565469bc2f1d700dd3ffbe2f1ec6a0f1 to your computer and use it in GitHub Desktop.
Save hcarty/565469bc2f1d700dd3ffbe2f1ec6a0f1 to your computer and use it in GitHub Desktop.
orx config for an inspector outline shader, sized appropriately for the orx logo in a fresh init project
[Inspector]
Shader = Outline
[Outline]
UseCustomParam = true
ParamList = texture # time # highlight
time = time 3.1415926
highlight = (1, 1, 1)
Code = "
uniform float width = 5.0;
uniform int pixel_size = 500;
uniform float width_speed = 1.0;
void main()
{
// Outline color
vec4 outline_color = vec4(highlight, 1.0);
// Texture bounds
vec2 texture_min = vec2(texture_left, texture_top);
vec2 texture_max = vec2(texture_right, texture_bottom);
vec2 texture_size = texture_max - texture_min;
// Position and texture color
vec2 pixel_pos = gl_TexCoord[0].xy;
vec4 pixel_color = texture2D(texture, pixel_pos);
float _width = width; // + (sin(time*width_speed) + 1.0) * 5.0;
// Unit of sampling in each direction
vec2 unit = vec2(1.0/float(pixel_size)) * texture_size;
// Balance between the outline color and texture color
float balance = 0.0;
if (pixel_color.a != 0.0) {
// Sample in a box around the current texture element
for (float x = -ceil(_width); x <= ceil(_width); x++) {
for (float y = -ceil(_width); y <= ceil(_width); y++) {
vec2 offset_pos = pixel_pos + vec2(x*unit.x, y*unit.y);
vec4 offset_color = vec4(0.0);
if (offset_pos.x >= texture_min.x && offset_pos.x <= texture_max.x &&
offset_pos.y >= texture_min.y && offset_pos.y <= texture_max.y) {
offset_color = texture2D(texture, offset_pos);
}
if (offset_color.a != 0.0 && gl_Color.a != 0.0 || (x==0.0 && y==0.0)) {
continue;
}
balance += outline_color.a / (pow(x,2)+pow(y,2)) * (1.0-pow(2.0, -_width));
if (balance > 1.0 || balance < 0.0) {
balance = 1.0;
}
}
}
}
// Use the following line to have the outline color blend at the edges
// gl_FragColor = mix(pixel_color * gl_Color, pixel_color * gl_Color * outline_color, balance);
// Use the following line to have the outline color only at the edges
gl_FragColor = mix(pixel_color * gl_Color, outline_color, balance);
}
"
@hcarty
Copy link
Author

hcarty commented Jul 6, 2024

This was originally based on https://godotshaders.com/shader/smooth-outline-2d/, with modifications to work with orx and change the outline behavior to render inside of an object's extent rather than outside of an object's extent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment