FXAAShader.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author davidedc / http://www.sketchpatch.net/
  4. *
  5. * NVIDIA FXAA by Timothy Lottes
  6. * http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
  7. * - WebGL port by @supereggbert
  8. * http://www.glge.org/demos/fxaa/
  9. */
  10. THREE.FXAAShader = {
  11. uniforms: {
  12. "tDiffuse": { value: null },
  13. "resolution": { value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
  14. },
  15. vertexShader: [
  16. "void main() {",
  17. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  18. "}"
  19. ].join( "\n" ),
  20. fragmentShader: [
  21. "uniform sampler2D tDiffuse;",
  22. "uniform vec2 resolution;",
  23. "#define FXAA_REDUCE_MIN (1.0/128.0)",
  24. "#define FXAA_REDUCE_MUL (1.0/8.0)",
  25. "#define FXAA_SPAN_MAX 8.0",
  26. "void main() {",
  27. "vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",
  28. "vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",
  29. "vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",
  30. "vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",
  31. "vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );",
  32. "vec3 rgbM = rgbaM.xyz;",
  33. "vec3 luma = vec3( 0.299, 0.587, 0.114 );",
  34. "float lumaNW = dot( rgbNW, luma );",
  35. "float lumaNE = dot( rgbNE, luma );",
  36. "float lumaSW = dot( rgbSW, luma );",
  37. "float lumaSE = dot( rgbSE, luma );",
  38. "float lumaM = dot( rgbM, luma );",
  39. "float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",
  40. "float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",
  41. "vec2 dir;",
  42. "dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",
  43. "dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",
  44. "float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",
  45. "float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",
  46. "dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",
  47. "max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",
  48. "dir * rcpDirMin)) * resolution;",
  49. "vec4 rgbA = (1.0/2.0) * (",
  50. "texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (1.0/3.0 - 0.5)) +",
  51. "texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (2.0/3.0 - 0.5)));",
  52. "vec4 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (",
  53. "texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (0.0/3.0 - 0.5)) +",
  54. "texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (3.0/3.0 - 0.5)));",
  55. "float lumaB = dot(rgbB, vec4(luma, 0.0));",
  56. "if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",
  57. "gl_FragColor = rgbA;",
  58. "} else {",
  59. "gl_FragColor = rgbB;",
  60. "}",
  61. "}"
  62. ].join( "\n" )
  63. };