blend.frag.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 blend_frag = `
  10. precision highp float;
  11. precision highp int;
  12. precision highp sampler2D;
  13. uniform sampler2D tWeights;
  14. uniform sampler2D tColor;
  15. uniform vec2 uTexSizeInv;
  16. varying vec2 vUv;
  17. varying vec4 vOffset[2];
  18. vec4 SMAANeighborhoodBlendingPS(vec2 texCoord, vec4 offset[2], sampler2D colorTex, sampler2D blendTex) {
  19. // Fetch the blending weights for current pixel:
  20. vec4 a;
  21. a.xz = texture2D(blendTex, texCoord).xz;
  22. a.y = texture2D(blendTex, offset[1].zw).g;
  23. a.w = texture2D(blendTex, offset[1].xy).a;
  24. // Is there any blending weight with a value greater than 0.0?
  25. if (dot(a, vec4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
  26. return texture2D(colorTex, texCoord, 0.0);
  27. } else {
  28. // Up to 4 lines can be crossing a pixel (one through each edge). We
  29. // favor blending by choosing the line with the maximum weight for each
  30. // direction:
  31. vec2 offset;
  32. offset.x = a.a > a.b ? a.a : -a.b; // left vs. right
  33. offset.y = a.g > a.r ? -a.g : a.r; // top vs. bottom // WebGL port note: Changed signs
  34. // Then we go in the direction that has the maximum weight:
  35. if (abs(offset.x) > abs(offset.y)) { // horizontal vs. vertical
  36. offset.y = 0.0;
  37. } else {
  38. offset.x = 0.0;
  39. }
  40. // Fetch the opposite color and lerp by hand:
  41. vec4 C = texture2D(colorTex, texCoord, 0.0);
  42. texCoord += sign(offset) * uTexSizeInv;
  43. vec4 Cop = texture2D(colorTex, texCoord, 0.0);
  44. float s = abs(offset.x) > abs(offset.y) ? abs(offset.x) : abs(offset.y);
  45. // WebGL port note: Added gamma correction
  46. C.xyz = pow(C.xyz, vec3(2.2));
  47. Cop.xyz = pow(Cop.xyz, vec3(2.2));
  48. vec4 mixed = mix(C, Cop, s);
  49. mixed.xyz = pow(mixed.xyz, vec3(1.0 / 2.2));
  50. return mixed;
  51. }
  52. }
  53. void main() {
  54. gl_FragColor = SMAANeighborhoodBlendingPS(vUv, vOffset, tColor, tWeights);
  55. }
  56. `;