FXAAShader.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ( function () {
  2. /**
  3. * NVIDIA FXAA by Timothy Lottes
  4. * https://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf
  5. * - WebGL port by @supereggbert
  6. * http://www.glge.org/demos/fxaa/
  7. * Further improved by Daniel Sturk
  8. */
  9. const FXAAShader = {
  10. uniforms: {
  11. 'tDiffuse': {
  12. value: null
  13. },
  14. 'resolution': {
  15. value: new THREE.Vector2( 1 / 1024, 1 / 512 )
  16. }
  17. },
  18. vertexShader: /* glsl */`
  19. varying vec2 vUv;
  20. void main() {
  21. vUv = uv;
  22. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  23. }`,
  24. fragmentShader: `
  25. precision highp float;
  26. uniform sampler2D tDiffuse;
  27. uniform vec2 resolution;
  28. varying vec2 vUv;
  29. // FXAA 3.11 implementation by NVIDIA, ported to WebGL by Agost Biro ([email protected])
  30. //----------------------------------------------------------------------------------
  31. // File: es3-kepler\FXAA\assets\shaders/FXAA_DefaultES.frag
  32. // SDK Version: v3.00
  33. // Email: [email protected]
  34. // Site: http://developer.nvidia.com/
  35. //
  36. // Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
  37. //
  38. // Redistribution and use in source and binary forms, with or without
  39. // modification, are permitted provided that the following conditions
  40. // are met:
  41. // * Redistributions of source code must retain the above copyright
  42. // notice, this list of conditions and the following disclaimer.
  43. // * Redistributions in binary form must reproduce the above copyright
  44. // notice, this list of conditions and the following disclaimer in the
  45. // documentation and/or other materials provided with the distribution.
  46. // * Neither the name of NVIDIA CORPORATION nor the names of its
  47. // contributors may be used to endorse or promote products derived
  48. // from this software without specific prior written permission.
  49. //
  50. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  51. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  53. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  58. // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. //
  62. //----------------------------------------------------------------------------------
  63. #ifndef FXAA_DISCARD
  64. //
  65. // Only valid for PC OpenGL currently.
  66. // Probably will not work when FXAA_GREEN_AS_LUMA = 1.
  67. //
  68. // 1 = Use discard on pixels which don't need AA.
  69. // For APIs which enable concurrent TEX+ROP from same surface.
  70. // 0 = Return unchanged color on pixels which don't need AA.
  71. //
  72. #define FXAA_DISCARD 0
  73. #endif
  74. /*--------------------------------------------------------------------------*/
  75. #define FxaaTexTop(t, p) texture2D(t, p, -100.0)
  76. #define FxaaTexOff(t, p, o, r) texture2D(t, p + (o * r), -100.0)
  77. /*--------------------------------------------------------------------------*/
  78. #define NUM_SAMPLES 5
  79. // assumes colors have premultipliedAlpha, so that the calculated color contrast is scaled by alpha
  80. float contrast( vec4 a, vec4 b ) {
  81. vec4 diff = abs( a - b );
  82. return max( max( max( diff.r, diff.g ), diff.b ), diff.a );
  83. }
  84. /*============================================================================
  85. FXAA3 QUALITY - PC
  86. ============================================================================*/
  87. /*--------------------------------------------------------------------------*/
  88. vec4 FxaaPixelShader(
  89. vec2 posM,
  90. sampler2D tex,
  91. vec2 fxaaQualityRcpFrame,
  92. float fxaaQualityEdgeThreshold,
  93. float fxaaQualityinvEdgeThreshold
  94. ) {
  95. vec4 rgbaM = FxaaTexTop(tex, posM);
  96. vec4 rgbaS = FxaaTexOff(tex, posM, vec2( 0.0, 1.0), fxaaQualityRcpFrame.xy);
  97. vec4 rgbaE = FxaaTexOff(tex, posM, vec2( 1.0, 0.0), fxaaQualityRcpFrame.xy);
  98. vec4 rgbaN = FxaaTexOff(tex, posM, vec2( 0.0,-1.0), fxaaQualityRcpFrame.xy);
  99. vec4 rgbaW = FxaaTexOff(tex, posM, vec2(-1.0, 0.0), fxaaQualityRcpFrame.xy);
  100. // . S .
  101. // W M E
  102. // . N .
  103. bool earlyExit = max( max( max(
  104. contrast( rgbaM, rgbaN ),
  105. contrast( rgbaM, rgbaS ) ),
  106. contrast( rgbaM, rgbaE ) ),
  107. contrast( rgbaM, rgbaW ) )
  108. < fxaaQualityEdgeThreshold;
  109. // . 0 .
  110. // 0 0 0
  111. // . 0 .
  112. #if (FXAA_DISCARD == 1)
  113. if(earlyExit) FxaaDiscard;
  114. #else
  115. if(earlyExit) return rgbaM;
  116. #endif
  117. float contrastN = contrast( rgbaM, rgbaN );
  118. float contrastS = contrast( rgbaM, rgbaS );
  119. float contrastE = contrast( rgbaM, rgbaE );
  120. float contrastW = contrast( rgbaM, rgbaW );
  121. float relativeVContrast = ( contrastN + contrastS ) - ( contrastE + contrastW );
  122. relativeVContrast *= fxaaQualityinvEdgeThreshold;
  123. bool horzSpan = relativeVContrast > 0.;
  124. // . 1 .
  125. // 0 0 0
  126. // . 1 .
  127. // 45 deg edge detection and corners of objects, aka V/H contrast is too similar
  128. if( abs( relativeVContrast ) < .3 ) {
  129. // locate the edge
  130. vec2 dirToEdge;
  131. dirToEdge.x = contrastE > contrastW ? 1. : -1.;
  132. dirToEdge.y = contrastS > contrastN ? 1. : -1.;
  133. // . 2 . . 1 .
  134. // 1 0 2 ~= 0 0 1
  135. // . 1 . . 0 .
  136. // tap 2 pixels and see which ones are "outside" the edge, to
  137. // determine if the edge is vertical or horizontal
  138. vec4 rgbaAlongH = FxaaTexOff(tex, posM, vec2( dirToEdge.x, -dirToEdge.y ), fxaaQualityRcpFrame.xy);
  139. float matchAlongH = contrast( rgbaM, rgbaAlongH );
  140. // . 1 .
  141. // 0 0 1
  142. // . 0 H
  143. vec4 rgbaAlongV = FxaaTexOff(tex, posM, vec2( -dirToEdge.x, dirToEdge.y ), fxaaQualityRcpFrame.xy);
  144. float matchAlongV = contrast( rgbaM, rgbaAlongV );
  145. // V 1 .
  146. // 0 0 1
  147. // . 0 .
  148. relativeVContrast = matchAlongV - matchAlongH;
  149. relativeVContrast *= fxaaQualityinvEdgeThreshold;
  150. if( abs( relativeVContrast ) < .3 ) { // 45 deg edge
  151. // 1 1 .
  152. // 0 0 1
  153. // . 0 1
  154. // do a simple blur
  155. return mix(
  156. rgbaM,
  157. (rgbaN + rgbaS + rgbaE + rgbaW) * .25,
  158. .4
  159. );
  160. }
  161. horzSpan = relativeVContrast > 0.;
  162. }
  163. if(!horzSpan) rgbaN = rgbaW;
  164. if(!horzSpan) rgbaS = rgbaE;
  165. // . 0 . 1
  166. // 1 0 1 -> 0
  167. // . 0 . 1
  168. bool pairN = contrast( rgbaM, rgbaN ) > contrast( rgbaM, rgbaS );
  169. if(!pairN) rgbaN = rgbaS;
  170. vec2 offNP;
  171. offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x;
  172. offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y;
  173. bool doneN = false;
  174. bool doneP = false;
  175. float nDist = 0.;
  176. float pDist = 0.;
  177. vec2 posN = posM;
  178. vec2 posP = posM;
  179. int iterationsUsed = 0;
  180. int iterationsUsedN = 0;
  181. int iterationsUsedP = 0;
  182. for( int i = 0; i < NUM_SAMPLES; i++ ) {
  183. iterationsUsed = i;
  184. float increment = float(i + 1);
  185. if(!doneN) {
  186. nDist += increment;
  187. posN = posM + offNP * nDist;
  188. vec4 rgbaEndN = FxaaTexTop(tex, posN.xy);
  189. doneN = contrast( rgbaEndN, rgbaM ) > contrast( rgbaEndN, rgbaN );
  190. iterationsUsedN = i;
  191. }
  192. if(!doneP) {
  193. pDist += increment;
  194. posP = posM - offNP * pDist;
  195. vec4 rgbaEndP = FxaaTexTop(tex, posP.xy);
  196. doneP = contrast( rgbaEndP, rgbaM ) > contrast( rgbaEndP, rgbaN );
  197. iterationsUsedP = i;
  198. }
  199. if(doneN || doneP) break;
  200. }
  201. if ( !doneP && !doneN ) return rgbaM; // failed to find end of edge
  202. float dist = min(
  203. doneN ? float( iterationsUsedN ) / float( NUM_SAMPLES - 1 ) : 1.,
  204. doneP ? float( iterationsUsedP ) / float( NUM_SAMPLES - 1 ) : 1.
  205. );
  206. // hacky way of reduces blurriness of mostly diagonal edges
  207. // but reduces AA quality
  208. dist = pow(dist, .5);
  209. dist = 1. - dist;
  210. return mix(
  211. rgbaM,
  212. rgbaN,
  213. dist * .5
  214. );
  215. }
  216. void main() {
  217. const float edgeDetectionQuality = .2;
  218. const float invEdgeDetectionQuality = 1. / edgeDetectionQuality;
  219. gl_FragColor = FxaaPixelShader(
  220. vUv,
  221. tDiffuse,
  222. resolution,
  223. edgeDetectionQuality, // [0,1] contrast needed, otherwise early discard
  224. invEdgeDetectionQuality
  225. );
  226. }
  227. `
  228. };
  229. THREE.FXAAShader = FXAAShader;
  230. } )();