SMAAShader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. ( function () {
  2. /**
  3. * WebGL port of Subpixel Morphological Antialiasing (SMAA) v2.8
  4. * Preset: SMAA 1x Medium (with color edge detection)
  5. * https://github.com/iryoku/smaa/releases/tag/v2.8
  6. */
  7. const SMAAEdgesShader = {
  8. defines: {
  9. 'SMAA_THRESHOLD': '0.1'
  10. },
  11. uniforms: {
  12. 'tDiffuse': {
  13. value: null
  14. },
  15. 'resolution': {
  16. value: new THREE.Vector2( 1 / 1024, 1 / 512 )
  17. }
  18. },
  19. vertexShader:
  20. /* glsl */
  21. `
  22. uniform vec2 resolution;
  23. varying vec2 vUv;
  24. varying vec4 vOffset[ 3 ];
  25. void SMAAEdgeDetectionVS( vec2 texcoord ) {
  26. vOffset[ 0 ] = texcoord.xyxy + resolution.xyxy * vec4( -1.0, 0.0, 0.0, 1.0 ); // WebGL port note: Changed sign in W component
  27. vOffset[ 1 ] = texcoord.xyxy + resolution.xyxy * vec4( 1.0, 0.0, 0.0, -1.0 ); // WebGL port note: Changed sign in W component
  28. vOffset[ 2 ] = texcoord.xyxy + resolution.xyxy * vec4( -2.0, 0.0, 0.0, 2.0 ); // WebGL port note: Changed sign in W component
  29. }
  30. void main() {
  31. vUv = uv;
  32. SMAAEdgeDetectionVS( vUv );
  33. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  34. }`,
  35. fragmentShader:
  36. /* glsl */
  37. `
  38. uniform sampler2D tDiffuse;
  39. varying vec2 vUv;
  40. varying vec4 vOffset[ 3 ];
  41. vec4 SMAAColorEdgeDetectionPS( vec2 texcoord, vec4 offset[3], sampler2D colorTex ) {
  42. vec2 threshold = vec2( SMAA_THRESHOLD, SMAA_THRESHOLD );
  43. // Calculate color deltas:
  44. vec4 delta;
  45. vec3 C = texture2D( colorTex, texcoord ).rgb;
  46. vec3 Cleft = texture2D( colorTex, offset[0].xy ).rgb;
  47. vec3 t = abs( C - Cleft );
  48. delta.x = max( max( t.r, t.g ), t.b );
  49. vec3 Ctop = texture2D( colorTex, offset[0].zw ).rgb;
  50. t = abs( C - Ctop );
  51. delta.y = max( max( t.r, t.g ), t.b );
  52. // We do the usual threshold:
  53. vec2 edges = step( threshold, delta.xy );
  54. // Then discard if there is no edge:
  55. if ( dot( edges, vec2( 1.0, 1.0 ) ) == 0.0 )
  56. discard;
  57. // Calculate right and bottom deltas:
  58. vec3 Cright = texture2D( colorTex, offset[1].xy ).rgb;
  59. t = abs( C - Cright );
  60. delta.z = max( max( t.r, t.g ), t.b );
  61. vec3 Cbottom = texture2D( colorTex, offset[1].zw ).rgb;
  62. t = abs( C - Cbottom );
  63. delta.w = max( max( t.r, t.g ), t.b );
  64. // Calculate the maximum delta in the direct neighborhood:
  65. float maxDelta = max( max( max( delta.x, delta.y ), delta.z ), delta.w );
  66. // Calculate left-left and top-top deltas:
  67. vec3 Cleftleft = texture2D( colorTex, offset[2].xy ).rgb;
  68. t = abs( C - Cleftleft );
  69. delta.z = max( max( t.r, t.g ), t.b );
  70. vec3 Ctoptop = texture2D( colorTex, offset[2].zw ).rgb;
  71. t = abs( C - Ctoptop );
  72. delta.w = max( max( t.r, t.g ), t.b );
  73. // Calculate the final maximum delta:
  74. maxDelta = max( max( maxDelta, delta.z ), delta.w );
  75. // Local contrast adaptation in action:
  76. edges.xy *= step( 0.5 * maxDelta, delta.xy );
  77. return vec4( edges, 0.0, 0.0 );
  78. }
  79. void main() {
  80. gl_FragColor = SMAAColorEdgeDetectionPS( vUv, vOffset, tDiffuse );
  81. }`
  82. };
  83. const SMAAWeightsShader = {
  84. defines: {
  85. 'SMAA_MAX_SEARCH_STEPS': '8',
  86. 'SMAA_AREATEX_MAX_DISTANCE': '16',
  87. 'SMAA_AREATEX_PIXEL_SIZE': '( 1.0 / vec2( 160.0, 560.0 ) )',
  88. 'SMAA_AREATEX_SUBTEX_SIZE': '( 1.0 / 7.0 )'
  89. },
  90. uniforms: {
  91. 'tDiffuse': {
  92. value: null
  93. },
  94. 'tArea': {
  95. value: null
  96. },
  97. 'tSearch': {
  98. value: null
  99. },
  100. 'resolution': {
  101. value: new THREE.Vector2( 1 / 1024, 1 / 512 )
  102. }
  103. },
  104. vertexShader:
  105. /* glsl */
  106. `
  107. uniform vec2 resolution;
  108. varying vec2 vUv;
  109. varying vec4 vOffset[ 3 ];
  110. varying vec2 vPixcoord;
  111. void SMAABlendingWeightCalculationVS( vec2 texcoord ) {
  112. vPixcoord = texcoord / resolution;
  113. // We will use these offsets for the searches later on (see @PSEUDO_GATHER4):
  114. vOffset[ 0 ] = texcoord.xyxy + resolution.xyxy * vec4( -0.25, 0.125, 1.25, 0.125 ); // WebGL port note: Changed sign in Y and W components
  115. vOffset[ 1 ] = texcoord.xyxy + resolution.xyxy * vec4( -0.125, 0.25, -0.125, -1.25 ); // WebGL port note: Changed sign in Y and W components
  116. // And these for the searches, they indicate the ends of the loops:
  117. vOffset[ 2 ] = vec4( vOffset[ 0 ].xz, vOffset[ 1 ].yw ) + vec4( -2.0, 2.0, -2.0, 2.0 ) * resolution.xxyy * float( SMAA_MAX_SEARCH_STEPS );
  118. }
  119. void main() {
  120. vUv = uv;
  121. SMAABlendingWeightCalculationVS( vUv );
  122. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  123. }`,
  124. fragmentShader:
  125. /* glsl */
  126. `
  127. #define SMAASampleLevelZeroOffset( tex, coord, offset ) texture2D( tex, coord + float( offset ) * resolution, 0.0 )
  128. uniform sampler2D tDiffuse;
  129. uniform sampler2D tArea;
  130. uniform sampler2D tSearch;
  131. uniform vec2 resolution;
  132. varying vec2 vUv;
  133. varying vec4 vOffset[3];
  134. varying vec2 vPixcoord;
  135. #if __VERSION__ == 100
  136. vec2 round( vec2 x ) {
  137. return sign( x ) * floor( abs( x ) + 0.5 );
  138. }
  139. #endif
  140. float SMAASearchLength( sampler2D searchTex, vec2 e, float bias, float scale ) {
  141. // Not required if searchTex accesses are set to point:
  142. // float2 SEARCH_TEX_PIXEL_SIZE = 1.0 / float2(66.0, 33.0);
  143. // e = float2(bias, 0.0) + 0.5 * SEARCH_TEX_PIXEL_SIZE +
  144. // e * float2(scale, 1.0) * float2(64.0, 32.0) * SEARCH_TEX_PIXEL_SIZE;
  145. e.r = bias + e.r * scale;
  146. return 255.0 * texture2D( searchTex, e, 0.0 ).r;
  147. }
  148. float SMAASearchXLeft( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  149. /**
  150. * @PSEUDO_GATHER4
  151. * This texcoord has been offset by (-0.25, -0.125) in the vertex shader to
  152. * sample between edge, thus fetching four edges in a row.
  153. * Sampling with different offsets in each direction allows to disambiguate
  154. * which edges are active from the four fetched ones.
  155. */
  156. vec2 e = vec2( 0.0, 1.0 );
  157. for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) { // WebGL port note: Changed while to for
  158. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  159. texcoord -= vec2( 2.0, 0.0 ) * resolution;
  160. if ( ! ( texcoord.x > end && e.g > 0.8281 && e.r == 0.0 ) ) break;
  161. }
  162. // We correct the previous (-0.25, -0.125) offset we applied:
  163. texcoord.x += 0.25 * resolution.x;
  164. // The searches are bias by 1, so adjust the coords accordingly:
  165. texcoord.x += resolution.x;
  166. // Disambiguate the length added by the last step:
  167. texcoord.x += 2.0 * resolution.x; // Undo last step
  168. texcoord.x -= resolution.x * SMAASearchLength(searchTex, e, 0.0, 0.5);
  169. return texcoord.x;
  170. }
  171. float SMAASearchXRight( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  172. vec2 e = vec2( 0.0, 1.0 );
  173. for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) { // WebGL port note: Changed while to for
  174. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  175. texcoord += vec2( 2.0, 0.0 ) * resolution;
  176. if ( ! ( texcoord.x < end && e.g > 0.8281 && e.r == 0.0 ) ) break;
  177. }
  178. texcoord.x -= 0.25 * resolution.x;
  179. texcoord.x -= resolution.x;
  180. texcoord.x -= 2.0 * resolution.x;
  181. texcoord.x += resolution.x * SMAASearchLength( searchTex, e, 0.5, 0.5 );
  182. return texcoord.x;
  183. }
  184. float SMAASearchYUp( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  185. vec2 e = vec2( 1.0, 0.0 );
  186. for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) { // WebGL port note: Changed while to for
  187. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  188. texcoord += vec2( 0.0, 2.0 ) * resolution; // WebGL port note: Changed sign
  189. if ( ! ( texcoord.y > end && e.r > 0.8281 && e.g == 0.0 ) ) break;
  190. }
  191. texcoord.y -= 0.25 * resolution.y; // WebGL port note: Changed sign
  192. texcoord.y -= resolution.y; // WebGL port note: Changed sign
  193. texcoord.y -= 2.0 * resolution.y; // WebGL port note: Changed sign
  194. texcoord.y += resolution.y * SMAASearchLength( searchTex, e.gr, 0.0, 0.5 ); // WebGL port note: Changed sign
  195. return texcoord.y;
  196. }
  197. float SMAASearchYDown( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  198. vec2 e = vec2( 1.0, 0.0 );
  199. for ( int i = 0; i < SMAA_MAX_SEARCH_STEPS; i ++ ) { // WebGL port note: Changed while to for
  200. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  201. texcoord -= vec2( 0.0, 2.0 ) * resolution; // WebGL port note: Changed sign
  202. if ( ! ( texcoord.y < end && e.r > 0.8281 && e.g == 0.0 ) ) break;
  203. }
  204. texcoord.y += 0.25 * resolution.y; // WebGL port note: Changed sign
  205. texcoord.y += resolution.y; // WebGL port note: Changed sign
  206. texcoord.y += 2.0 * resolution.y; // WebGL port note: Changed sign
  207. texcoord.y -= resolution.y * SMAASearchLength( searchTex, e.gr, 0.5, 0.5 ); // WebGL port note: Changed sign
  208. return texcoord.y;
  209. }
  210. vec2 SMAAArea( sampler2D areaTex, vec2 dist, float e1, float e2, float offset ) {
  211. // Rounding prevents precision errors of bilinear filtering:
  212. vec2 texcoord = float( SMAA_AREATEX_MAX_DISTANCE ) * round( 4.0 * vec2( e1, e2 ) ) + dist;
  213. // We do a scale and bias for mapping to texel space:
  214. texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + ( 0.5 * SMAA_AREATEX_PIXEL_SIZE );
  215. // Move to proper place, according to the subpixel offset:
  216. texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
  217. return texture2D( areaTex, texcoord, 0.0 ).rg;
  218. }
  219. vec4 SMAABlendingWeightCalculationPS( vec2 texcoord, vec2 pixcoord, vec4 offset[ 3 ], sampler2D edgesTex, sampler2D areaTex, sampler2D searchTex, ivec4 subsampleIndices ) {
  220. vec4 weights = vec4( 0.0, 0.0, 0.0, 0.0 );
  221. vec2 e = texture2D( edgesTex, texcoord ).rg;
  222. if ( e.g > 0.0 ) { // Edge at north
  223. vec2 d;
  224. // Find the distance to the left:
  225. vec2 coords;
  226. coords.x = SMAASearchXLeft( edgesTex, searchTex, offset[ 0 ].xy, offset[ 2 ].x );
  227. coords.y = offset[ 1 ].y; // offset[1].y = texcoord.y - 0.25 * resolution.y (@CROSSING_OFFSET)
  228. d.x = coords.x;
  229. // Now fetch the left crossing edges, two at a time using bilinear
  230. // filtering. Sampling at -0.25 (see @CROSSING_OFFSET) enables to
  231. // discern what value each edge has:
  232. float e1 = texture2D( edgesTex, coords, 0.0 ).r;
  233. // Find the distance to the right:
  234. coords.x = SMAASearchXRight( edgesTex, searchTex, offset[ 0 ].zw, offset[ 2 ].y );
  235. d.y = coords.x;
  236. // We want the distances to be in pixel units (doing this here allow to
  237. // better interleave arithmetic and memory accesses):
  238. d = d / resolution.x - pixcoord.x;
  239. // SMAAArea below needs a sqrt, as the areas texture is compressed
  240. // quadratically:
  241. vec2 sqrt_d = sqrt( abs( d ) );
  242. // Fetch the right crossing edges:
  243. coords.y -= 1.0 * resolution.y; // WebGL port note: Added
  244. float e2 = SMAASampleLevelZeroOffset( edgesTex, coords, ivec2( 1, 0 ) ).r;
  245. // Ok, we know how this pattern looks like, now it is time for getting
  246. // the actual area:
  247. weights.rg = SMAAArea( areaTex, sqrt_d, e1, e2, float( subsampleIndices.y ) );
  248. }
  249. if ( e.r > 0.0 ) { // Edge at west
  250. vec2 d;
  251. // Find the distance to the top:
  252. vec2 coords;
  253. coords.y = SMAASearchYUp( edgesTex, searchTex, offset[ 1 ].xy, offset[ 2 ].z );
  254. coords.x = offset[ 0 ].x; // offset[1].x = texcoord.x - 0.25 * resolution.x;
  255. d.x = coords.y;
  256. // Fetch the top crossing edges:
  257. float e1 = texture2D( edgesTex, coords, 0.0 ).g;
  258. // Find the distance to the bottom:
  259. coords.y = SMAASearchYDown( edgesTex, searchTex, offset[ 1 ].zw, offset[ 2 ].w );
  260. d.y = coords.y;
  261. // We want the distances to be in pixel units:
  262. d = d / resolution.y - pixcoord.y;
  263. // SMAAArea below needs a sqrt, as the areas texture is compressed
  264. // quadratically:
  265. vec2 sqrt_d = sqrt( abs( d ) );
  266. // Fetch the bottom crossing edges:
  267. coords.y -= 1.0 * resolution.y; // WebGL port note: Added
  268. float e2 = SMAASampleLevelZeroOffset( edgesTex, coords, ivec2( 0, 1 ) ).g;
  269. // Get the area for this direction:
  270. weights.ba = SMAAArea( areaTex, sqrt_d, e1, e2, float( subsampleIndices.x ) );
  271. }
  272. return weights;
  273. }
  274. void main() {
  275. gl_FragColor = SMAABlendingWeightCalculationPS( vUv, vPixcoord, vOffset, tDiffuse, tArea, tSearch, ivec4( 0.0 ) );
  276. }`
  277. };
  278. const SMAABlendShader = {
  279. uniforms: {
  280. 'tDiffuse': {
  281. value: null
  282. },
  283. 'tColor': {
  284. value: null
  285. },
  286. 'resolution': {
  287. value: new THREE.Vector2( 1 / 1024, 1 / 512 )
  288. }
  289. },
  290. vertexShader:
  291. /* glsl */
  292. `
  293. uniform vec2 resolution;
  294. varying vec2 vUv;
  295. varying vec4 vOffset[ 2 ];
  296. void SMAANeighborhoodBlendingVS( vec2 texcoord ) {
  297. vOffset[ 0 ] = texcoord.xyxy + resolution.xyxy * vec4( -1.0, 0.0, 0.0, 1.0 ); // WebGL port note: Changed sign in W component
  298. vOffset[ 1 ] = texcoord.xyxy + resolution.xyxy * vec4( 1.0, 0.0, 0.0, -1.0 ); // WebGL port note: Changed sign in W component
  299. }
  300. void main() {
  301. vUv = uv;
  302. SMAANeighborhoodBlendingVS( vUv );
  303. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  304. }`,
  305. fragmentShader:
  306. /* glsl */
  307. `
  308. uniform sampler2D tDiffuse;
  309. uniform sampler2D tColor;
  310. uniform vec2 resolution;
  311. varying vec2 vUv;
  312. varying vec4 vOffset[ 2 ];
  313. vec4 SMAANeighborhoodBlendingPS( vec2 texcoord, vec4 offset[ 2 ], sampler2D colorTex, sampler2D blendTex ) {
  314. // Fetch the blending weights for current pixel:
  315. vec4 a;
  316. a.xz = texture2D( blendTex, texcoord ).xz;
  317. a.y = texture2D( blendTex, offset[ 1 ].zw ).g;
  318. a.w = texture2D( blendTex, offset[ 1 ].xy ).a;
  319. // Is there any blending weight with a value greater than 0.0?
  320. if ( dot(a, vec4( 1.0, 1.0, 1.0, 1.0 )) < 1e-5 ) {
  321. return texture2D( colorTex, texcoord, 0.0 );
  322. } else {
  323. // Up to 4 lines can be crossing a pixel (one through each edge). We
  324. // favor blending by choosing the line with the maximum weight for each
  325. // direction:
  326. vec2 offset;
  327. offset.x = a.a > a.b ? a.a : -a.b; // left vs. right
  328. offset.y = a.g > a.r ? -a.g : a.r; // top vs. bottom // WebGL port note: Changed signs
  329. // Then we go in the direction that has the maximum weight:
  330. if ( abs( offset.x ) > abs( offset.y )) { // horizontal vs. vertical
  331. offset.y = 0.0;
  332. } else {
  333. offset.x = 0.0;
  334. }
  335. // Fetch the opposite color and lerp by hand:
  336. vec4 C = texture2D( colorTex, texcoord, 0.0 );
  337. texcoord += sign( offset ) * resolution;
  338. vec4 Cop = texture2D( colorTex, texcoord, 0.0 );
  339. float s = abs( offset.x ) > abs( offset.y ) ? abs( offset.x ) : abs( offset.y );
  340. // WebGL port note: Added gamma correction
  341. C.xyz = pow(C.xyz, vec3(2.2));
  342. Cop.xyz = pow(Cop.xyz, vec3(2.2));
  343. vec4 mixed = mix(C, Cop, s);
  344. mixed.xyz = pow(mixed.xyz, vec3(1.0 / 2.2));
  345. return mixed;
  346. }
  347. }
  348. void main() {
  349. gl_FragColor = SMAANeighborhoodBlendingPS( vUv, vOffset, tColor, tDiffuse );
  350. }`
  351. };
  352. THREE.SMAABlendShader = SMAABlendShader;
  353. THREE.SMAAEdgesShader = SMAAEdgesShader;
  354. THREE.SMAAWeightsShader = SMAAWeightsShader;
  355. } )();