GoDot – Shader – 2D – Outline

4x Blend

shader_type canvas_item;

uniform float size = 1;
uniform vec4 color: hint_color;

void fragment() 
{
   vec2 texture_size = TEXTURE_PIXEL_SIZE;
   vec4 sprite = texture(TEXTURE, UV);
   float maxAlpha = sprite.a;
   float minAlpha = sprite.a;

   float pixel_left = texture(TEXTURE, UV + vec2(0.0, -size) * texture_size).a;
   float pixel_right = texture(TEXTURE, UV + vec2(0.0, size) * texture_size).a;
   float pixel_top = texture(TEXTURE, UV + vec2(-size, 0.0) * texture_size).a;
   float pixel_bottom = texture(TEXTURE, UV + vec2(size, 0.0) * texture_size).a;

   maxAlpha = max(pixel_left, maxAlpha);
   maxAlpha = max(pixel_right, maxAlpha);
   maxAlpha = max(pixel_top, maxAlpha);
   maxAlpha = max(pixel_bottom, maxAlpha);
   minAlpha = min(pixel_left, minAlpha);
   minAlpha = min(pixel_right, minAlpha);
   minAlpha = min(pixel_top, minAlpha);
   minAlpha = min(pixel_bottom, minAlpha);

   COLOR = mix(sprite, color, maxAlpha - minAlpha);
 }

8x Blend

shader_type canvas_item;
uniform float size = 1;
uniform vec4 color: hint_color;

void fragment() 
{
   vec2 texture_size = TEXTURE_PIXEL_SIZE;
   vec4 sprite = texture(TEXTURE, UV);
   float maxAlpha = sprite.a;
   float minAlpha = sprite.a;

   float pixel_left = texture(TEXTURE, UV + vec2(0.0, -size) * texture_size).a;
   float pixel_right = texture(TEXTURE, UV + vec2(0.0, size) * texture_size).a;
   float pixel_top = texture(TEXTURE, UV + vec2(-size, 0.0) * texture_size).a;
   float pixel_bottom = texture(TEXTURE, UV + vec2(size, 0.0) * texture_size).a;
   float pixel_top_left = texture(TEXTURE, UV + vec2(-size, -size) * texture_size).a;
   float pixel_top_right = texture(TEXTURE, UV + vec2(size, -size) * texture_size).a;
   float pixel_bottom_left = texture(TEXTURE, UV + vec2(-size, size) * texture_size).a;
   float pixel_bottom_right = texture(TEXTURE, UV + vec2(size, size) * texture_size).a;

   maxAlpha = max(pixel_left, maxAlpha);
   maxAlpha = max(pixel_right, maxAlpha);
   maxAlpha = max(pixel_top, maxAlpha);
   maxAlpha = max(pixel_bottom, maxAlpha);
   maxAlpha = max(pixel_top_left, maxAlpha);
   maxAlpha = max(pixel_top_right, maxAlpha);
   maxAlpha = max(pixel_bottom_right, maxAlpha);
   maxAlpha = max(pixel_bottom_left, maxAlpha);
   minAlpha = min(pixel_left, minAlpha);
   minAlpha = min(pixel_right, minAlpha);
   minAlpha = min(pixel_top, minAlpha);
   minAlpha = min(pixel_bottom, minAlpha);
   minAlpha = min(pixel_top_left, minAlpha);
   minAlpha = min(pixel_top_right, minAlpha);
   minAlpha = min(pixel_bottom_right, minAlpha);
   minAlpha = min(pixel_bottom_left, minAlpha);

   COLOR = mix(sprite, color, maxAlpha - minAlpha);
}
0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments