FXAAShader.js 8.3 KB

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