outline.swsl 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Taken from the godot-demo-projects repo.
  2. // GODOT ENGINE
  3. // http://www.godotengine.org
  4. //
  5. // ************************************************************************
  6. //
  7. // Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //************************************************************************
  29. uniform highp float outline_width; // = 2.0;
  30. uniform highp vec4 outline_color; // =vec4(1.0,0.0,0.0,0.33);
  31. uniform bool outline_fullbright; // =false;
  32. uniform highp float light_boost; // = 4.0;
  33. uniform highp float light_gamma; // = 1.0;
  34. uniform highp float light_whitepoint; // = 1.0;
  35. void fragment() {
  36. highp vec4 col = zTexture(UV);
  37. highp vec2 ps = TEXTURE_PIXEL_SIZE;
  38. highp float a;
  39. highp float maxa = col.a;
  40. highp float mina = col.a;
  41. // note: these bypass zTexture because only alpha is queried.
  42. a = texture2D(TEXTURE, UV + vec2(0.0, -outline_width)*ps).a;
  43. maxa = max(a, maxa);
  44. mina = min(a, mina);
  45. a = texture2D(TEXTURE, UV + vec2(-outline_width, -outline_width)*ps).a;
  46. maxa = max(a, maxa);
  47. mina = min(a, mina);
  48. a = texture2D(TEXTURE, UV + vec2(0.0, outline_width)*ps).a;
  49. maxa = max(a, maxa);
  50. mina = min(a, mina);
  51. a = texture2D(TEXTURE, UV + vec2(outline_width, -outline_width)*ps).a;
  52. maxa = max(a, maxa);
  53. mina = min(a, mina);
  54. a = texture2D(TEXTURE, UV + vec2(-outline_width,0.0)*ps).a;
  55. maxa = max(a, maxa);
  56. mina = min(a, mina);
  57. a = texture2D(TEXTURE, UV + vec2(-outline_width, outline_width)*ps).a;
  58. maxa = max(a, maxa);
  59. mina = min(a, mina);
  60. a = texture2D(TEXTURE, UV + vec2(outline_width, 0.0)*ps).a;
  61. maxa = max(a, maxa);
  62. mina = min(a, mina);
  63. a = texture2D(TEXTURE, UV + vec2(outline_width, outline_width)*ps).a;
  64. maxa = max(a, maxa);
  65. mina = min(a, mina);
  66. lowp float sampledLight = outline_fullbright ? 1.0 : clamp( (pow( zGrayscale_BT601(lightSample.rgb) * light_whitepoint, light_gamma) / light_whitepoint ) * light_boost, 0.0, 1.0);
  67. COLOR = mix(col, outline_color * vec4(vec3(1.0), sampledLight), maxa - col.a);
  68. lightSample = vec3(1.0);
  69. }