FXAAShader.js 8.3 KB

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