glsl-fxaa
A WebGL implementation of Fast Approximate Anti-Aliasing (FXAA v2). This is a screen-space technique. The code was originally from Geeks3D.com and cleaned up by Armin Ronacher for WebGL.
FXAA is particularly useful in WebGL since most browsers do not currently support MSAA, and even those that do (e.g. Chrome) will not support it outside of the main frame buffer (which is common when doing post-processing effects like color grading).
Usage
vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution)
Returns the anti-aliased color from your frame texture.
Inside GLSL fragment shader:
# fxaa = require(glsl-fxaa) uniform vec2 resolution; void
optimizing
If you plan on using FXAA instead of native anti-aliasing (i.e. for post-processed 3D scenes), disabling native AA when creating your WebGL context should give you a performance boost in some browsers.
This FXAA shader uses 9 dependent texture reads. For various mobile GPUs (particularly iOS), we can optimize the shader by making 5 of the texture2D calls non-dependent. To do this, the coordinates have to be computed in the vertex shader and passed along:
vert shader:
varying vec2 v_rgbNW;varying vec2 v_rgbNE;varying vec2 v_rgbSW;varying vec2 v_rgbSE;varying vec2 v_rgbM; uniform vec2 resolution; # texcoords = require(glsl-fxaa/texcoords.glsl) void
frag shader:
varying vec2 v_rgbNW;varying vec2 v_rgbNE;varying vec2 v_rgbSW;varying vec2 v_rgbSE;varying vec2 v_rgbM; uniform vec2 resolution; # fxaa = require(glsl-fxaa/fxaa.glsl) void
In most cases, you should just use the simplest index.glsl
use case demonstrated earlier.
demo
See the demo folder. To run:
# clone repo git clone https://github.com/mattdesl/glsl-fxaa.git # install deps npm install # run local host npm start
Now open localhost:9966
to test. Use npm run build
to build a bundle.
License
MIT, see LICENSE.md for details.