opengl_fragment.glsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #define rendered texture0
  2. uniform sampler2D rendered;
  3. uniform vec2 texelSize0;
  4. varying vec2 sampleNW;
  5. varying vec2 sampleNE;
  6. varying vec2 sampleSW;
  7. varying vec2 sampleSE;
  8. #ifdef GL_ES
  9. varying mediump vec2 varTexCoord;
  10. #else
  11. centroid varying vec2 varTexCoord;
  12. #endif
  13. /**
  14. Basic FXAA implementation based on the code on geeks3d.com with the
  15. modification that the texture2DLod stuff was removed since it's
  16. unsupported by WebGL.
  17. --
  18. From:
  19. https://github.com/mitsuhiko/webgl-meincraft
  20. Copyright (c) 2011 by Armin Ronacher.
  21. Some rights reserved.
  22. Redistribution and use in source and binary forms, with or without
  23. modification, are permitted provided that the following conditions are
  24. met:
  25. * Redistributions of source code must retain the above copyright
  26. notice, this list of conditions and the following disclaimer.
  27. * Redistributions in binary form must reproduce the above
  28. copyright notice, this list of conditions and the following
  29. disclaimer in the documentation and/or other materials provided
  30. with the distribution.
  31. * The names of the contributors may not be used to endorse or
  32. promote products derived from this software without specific
  33. prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. #ifndef FXAA_REDUCE_MIN
  47. #define FXAA_REDUCE_MIN (1.0/ 128.0)
  48. #endif
  49. #ifndef FXAA_REDUCE_MUL
  50. #define FXAA_REDUCE_MUL (1.0 / 8.0)
  51. #endif
  52. #ifndef FXAA_SPAN_MAX
  53. #define FXAA_SPAN_MAX 8.0
  54. #endif
  55. //optimized version for mobile, where dependent
  56. //texture reads can be a bottleneck
  57. vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 inverseVP,
  58. vec2 v_rgbNW, vec2 v_rgbNE,
  59. vec2 v_rgbSW, vec2 v_rgbSE,
  60. vec2 v_rgbM) {
  61. vec4 color;
  62. vec3 rgbNW = texture2D(tex, v_rgbNW).xyz;
  63. vec3 rgbNE = texture2D(tex, v_rgbNE).xyz;
  64. vec3 rgbSW = texture2D(tex, v_rgbSW).xyz;
  65. vec3 rgbSE = texture2D(tex, v_rgbSE).xyz;
  66. vec4 texColor = texture2D(tex, v_rgbM);
  67. vec3 rgbM = texColor.xyz;
  68. vec3 luma = vec3(0.299, 0.587, 0.114);
  69. float lumaNW = dot(rgbNW, luma);
  70. float lumaNE = dot(rgbNE, luma);
  71. float lumaSW = dot(rgbSW, luma);
  72. float lumaSE = dot(rgbSE, luma);
  73. float lumaM = dot(rgbM, luma);
  74. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  75. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  76. mediump vec2 dir;
  77. dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  78. dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  79. float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
  80. (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
  81. float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
  82. dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
  83. max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
  84. dir * rcpDirMin)) * inverseVP;
  85. vec3 rgbA = 0.5 * (
  86. texture2D(tex, fragCoord + dir * (1.0 / 3.0 - 0.5)).xyz +
  87. texture2D(tex, fragCoord + dir * (2.0 / 3.0 - 0.5)).xyz);
  88. vec3 rgbB = rgbA * 0.5 + 0.25 * (
  89. texture2D(tex, fragCoord + dir * -0.5).xyz +
  90. texture2D(tex, fragCoord + dir * 0.5).xyz);
  91. float lumaB = dot(rgbB, luma);
  92. if ((lumaB < lumaMin) || (lumaB > lumaMax))
  93. color = vec4(rgbA, 1.0);
  94. else
  95. color = vec4(rgbB, 1.0);
  96. return color;
  97. }
  98. void main(void)
  99. {
  100. vec2 uv = varTexCoord.st;
  101. gl_FragColor = fxaa(rendered, uv, texelSize0,
  102. sampleNW, sampleNE, sampleSW, sampleSE, uv);
  103. }