outline.gdshader 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. shader_type canvas_item;
  2. uniform vec4 color : source_color = vec4(1.0);
  3. uniform float width : hint_range(0, 100) = 1.0;
  4. uniform int pattern : hint_range(0, 2) = 0; // diamond, circle, square
  5. uniform bool inside = false;
  6. uniform bool add_margins = true; // only useful when inside is false
  7. void vertex() {
  8. if (add_margins) {
  9. VERTEX += (UV * 2.0 - 1.0) * width;
  10. }
  11. }
  12. bool hasContraryNeighbour(vec2 uv, vec2 texture_pixel_size, sampler2D texture) {
  13. for (float i = -ceil(width); i <= ceil(width); i++) {
  14. float x = abs(i) > width ? width * sign(i) : i;
  15. float offset;
  16. if (pattern == 0) {
  17. offset = width - abs(x);
  18. } else if (pattern == 1) {
  19. offset = floor(sqrt(pow(width + 0.5, 2) - x * x));
  20. } else if (pattern == 2) {
  21. offset = width;
  22. }
  23. for (float j = -ceil(offset); j <= ceil(offset); j++) {
  24. float y = abs(j) > offset ? offset * sign(j) : j;
  25. vec2 xy = uv + texture_pixel_size * vec2(x, y);
  26. if ((xy != clamp(xy, vec2(0.0), vec2(1.0)) || texture(texture, xy).a == 0.0) == inside) {
  27. return true;
  28. }
  29. }
  30. }
  31. return false;
  32. }
  33. void fragment() {
  34. vec2 uv = UV;
  35. if (add_margins) {
  36. vec2 texture_pixel_size = vec2(1.0) / (vec2(1.0) / TEXTURE_PIXEL_SIZE + vec2(width * 2.0));
  37. uv = (uv - texture_pixel_size * width) * TEXTURE_PIXEL_SIZE / texture_pixel_size;
  38. if (uv != clamp(uv, vec2(0.0), vec2(1.0))) {
  39. COLOR.a = 0.0;
  40. } else {
  41. COLOR = texture(TEXTURE, uv);
  42. }
  43. } else {
  44. COLOR = texture(TEXTURE, uv);
  45. }
  46. if ((COLOR.a > 0.0) == inside && hasContraryNeighbour(uv, TEXTURE_PIXEL_SIZE, TEXTURE)) {
  47. COLOR.rgb = inside ? mix(COLOR.rgb, color.rgb, color.a) : color.rgb;
  48. COLOR.a += (1.0 - COLOR.a) * color.a;
  49. }
  50. }