edges.frag.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Slightly adapted from https://github.com/mrdoob/three.js
  3. * MIT License Copyright (c) 2010-2020 three.js authors
  4. *
  5. * WebGL port of Subpixel Morphological Antialiasing (SMAA) v2.8
  6. * Preset: SMAA 1x Medium (with color edge detection)
  7. * https://github.com/iryoku/smaa/releases/tag/v2.8
  8. */
  9. export const edges_frag = `
  10. precision highp float;
  11. precision highp int;
  12. precision highp sampler2D;
  13. uniform sampler2D tColor;
  14. uniform vec2 uTexSizeInv;
  15. varying vec2 vUv;
  16. varying vec4 vOffset[3];
  17. vec4 SMAAColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex) {
  18. vec2 threshold = vec2(dEdgeThreshold, dEdgeThreshold);
  19. // Calculate color deltas:
  20. vec4 delta;
  21. vec3 C = texture2D(colorTex, texcoord).rgb;
  22. vec3 Cleft = texture2D(colorTex, offset[0].xy).rgb;
  23. vec3 t = abs(C - Cleft);
  24. delta.x = max(max(t.r, t.g), t.b);
  25. vec3 Ctop = texture2D(colorTex, offset[0].zw).rgb;
  26. t = abs(C - Ctop);
  27. delta.y = max(max(t.r, t.g), t.b);
  28. // We do the usual threshold:
  29. vec2 edges = step(threshold, delta.xy);
  30. // Then discard if there is no edge:
  31. if (dot(edges, vec2(1.0, 1.0 )) == 0.0)
  32. discard;
  33. // Calculate right and bottom deltas:
  34. vec3 Cright = texture2D(colorTex, offset[1].xy).rgb;
  35. t = abs( C - Cright );
  36. delta.z = max(max(t.r, t.g), t.b);
  37. vec3 Cbottom = texture2D(colorTex, offset[1].zw).rgb;
  38. t = abs(C - Cbottom);
  39. delta.w = max(max(t.r, t.g), t.b);
  40. // Calculate the maximum delta in the direct neighborhood:
  41. float maxDelta = max(max(max(delta.x, delta.y), delta.z), delta.w );
  42. // Calculate left-left and top-top deltas:
  43. vec3 Cleftleft = texture2D(colorTex, offset[2].xy).rgb;
  44. t = abs( C - Cleftleft );
  45. delta.z = max(max(t.r, t.g), t.b);
  46. vec3 Ctoptop = texture2D(colorTex, offset[2].zw).rgb;
  47. t = abs(C - Ctoptop);
  48. delta.w = max(max(t.r, t.g), t.b);
  49. // Calculate the final maximum delta:
  50. maxDelta = max(max(maxDelta, delta.z), delta.w);
  51. // Local contrast adaptation in action:
  52. edges.xy *= step(0.5 * maxDelta, delta.xy);
  53. return vec4(edges, 0.0, 0.0);
  54. }
  55. void main() {
  56. gl_FragColor = SMAAColorEdgeDetectionPS(vUv, vOffset, tColor);
  57. }
  58. `;