Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MPiccinato/a0e680f71f646c609ef9ae573bb6faaf to your computer and use it in GitHub Desktop.
Save MPiccinato/a0e680f71f646c609ef9ae573bb6faaf to your computer and use it in GitHub Desktop.
Outlines a 2D Sprite Sheet to take into account each frame and not consider pixels outside of the current frame. Original - https://github.com/GDQuest/godot-shaders/blob/master/godot/Shaders/outline2D_outer.shader
shader_type canvas_item;
uniform vec4 line_color : source_color = vec4(1.0);
uniform float line_thickness : hint_range(0, 10) = 1.0;
uniform vec2 sheet_size = vec2(1, 1);
const vec2 OFFSETS[8] = {
vec2(-1, -1), vec2(-1, 0), vec2(-1, 1), vec2(0, -1), vec2(0, 1),
vec2(1, -1), vec2(1, 0), vec2(1, 1)
};
void fragment() {
vec2 size = TEXTURE_PIXEL_SIZE * line_thickness;
float outline = 0.0;
// Each Frames size based on the Rows and Columns
vec2 frame_size = vec2(1.0 / sheet_size.x, 1.0 / sheet_size.y);
// Upper left UV corner of the current frame
vec2 frame_uv = floor(UV / frame_size) * frame_size;
for (int i = 0; i < OFFSETS.length(); i++) {
vec2 offset = OFFSETS[i] * size;
vec2 sample_uv = mix(UV, UV + offset, step(frame_uv, UV + offset));
outline += texture(TEXTURE, sample_uv).a;
}
outline = min(outline, 1.0);
vec4 color = texture(TEXTURE, UV);
COLOR = mix(color, line_color, outline - color.a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment