SMAAShader.js 15 KB

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