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