FXAAShader.js 8.3 KB

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