BlendWeights.glsl.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{glsl as e}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{ShaderBuilder as o}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DPassUniform as r}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{VertexAttribute as t}from"../views/3d/webgl-engine/lib/VertexAttribute.js";import{addResolutionUniform as s}from"../views/3d/webgl-engine/shaders/SMAAPassParameters.js";const c={maxSearchSteps:8,maxDistanceAreaTex:16};function d(){const d=new o,{attributes:x,varyings:a,vertex:i,fragment:n}=d;return x.add(t.POSITION,"vec2"),a.add("fTexCoord","vec2"),a.add("fOffset[3]","vec4"),a.add("fPixCoord","vec2"),s(i),i.code.add(e`
  6. void SMAABlendingWeightCalculationVS( vec2 texcoord ) {
  7. fPixCoord = texcoord * resolution.zw;
  8. fOffset[0] = texcoord.xyxy + resolution.xyxy * vec4( -0.25, 0.125, 1.25, 0.125 );
  9. fOffset[1] = texcoord.xyxy + resolution.xyxy * vec4( -0.125, 0.25, -0.125, -1.25 );
  10. fOffset[2] = vec4( fOffset[0].xz, fOffset[1].yw ) + vec4( -2.0, 2.0, -2.0, 2.0 ) * resolution.xxyy * float( ${e.int(c.maxSearchSteps)} );
  11. }
  12. void main() {
  13. fTexCoord = (position + 1.0 ) * 0.5;
  14. gl_Position = vec4(position, 0, 1);
  15. SMAABlendingWeightCalculationVS( fTexCoord );
  16. }
  17. `),n.uniforms.add(new r("tEdges",(e=>e.edges.colorTexture))),n.uniforms.add(new r("tArea",(e=>e.areaTexture))),n.uniforms.add(new r("tSearch",(e=>e.searchTexture))),n.uniforms.add(new r("tColor",(e=>e.colorTexture))),s(n),n.code.add(e`
  18. #define SMAA_AREATEX_PIXEL_SIZE ( 1.0 / vec2( 160.0, 560.0 ) )
  19. #define SMAA_AREATEX_SUBTEX_SIZE ( 1.0 / 7.0 )
  20. vec4 SMAASampleLevelZeroOffset(sampler2D texture, vec2 coord, vec2 offset) {
  21. return texture2D(texture, coord + offset.x * resolution.xy, 0.0);
  22. }
  23. vec2 round( vec2 x ) {
  24. return sign( x ) * floor( abs( x ) + 0.5 );
  25. }
  26. float SMAASearchLength( sampler2D searchTex, vec2 e, float bias, float scale ) {
  27. e.r = bias + e.r * scale;
  28. return 255.0 * texture2D( searchTex, e, 0.0 ).r;
  29. }
  30. float SMAASearchXLeft( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  31. vec2 e = vec2( 0.0, 1.0 );
  32. for ( int i = 0; i < ${e.int(c.maxSearchSteps)}; i ++ ) {
  33. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  34. texcoord -= vec2( 2.0, 0.0 ) * resolution.xy;
  35. if ( ! ( texcoord.x > end && e.g > 0.8281 && e.r == 0.0 ) ) break;
  36. }
  37. texcoord.x += 0.25 * resolution.x;
  38. texcoord.x += resolution.x;
  39. texcoord.x += 2.0 * resolution.x;
  40. texcoord.x -= resolution.x * SMAASearchLength(searchTex, e, 0.0, 0.5);
  41. return texcoord.x;
  42. }
  43. float SMAASearchXRight( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  44. vec2 e = vec2( 0.0, 1.0 );
  45. for ( int i = 0; i < ${e.int(c.maxSearchSteps)}; i ++ ) {
  46. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  47. texcoord += vec2( 2.0, 0.0 ) * resolution.xy;
  48. if ( ! ( texcoord.x < end && e.g > 0.8281 && e.r == 0.0 ) ) break;
  49. }
  50. texcoord.x -= 0.25 * resolution.x;
  51. texcoord.x -= resolution.x;
  52. texcoord.x -= 2.0 * resolution.x;
  53. texcoord.x += resolution.x * SMAASearchLength( searchTex, e, 0.5, 0.5 );
  54. return texcoord.x;
  55. }
  56. float SMAASearchYUp( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  57. vec2 e = vec2( 1.0, 0.0 );
  58. for ( int i = 0; i < ${e.int(c.maxSearchSteps)}; i ++ ) {
  59. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  60. texcoord += vec2( 0.0, 2.0 ) * resolution.xy;
  61. if ( ! ( texcoord.y > end && e.r > 0.8281 && e.g == 0.0 ) ) break;
  62. }
  63. texcoord.y -= 0.25 * resolution.y;
  64. texcoord.y -= resolution.y;
  65. texcoord.y -= 2.0 * resolution.y;
  66. texcoord.y += resolution.y * SMAASearchLength( searchTex, e.gr, 0.0, 0.5 );
  67. return texcoord.y;
  68. }
  69. float SMAASearchYDown( sampler2D edgesTex, sampler2D searchTex, vec2 texcoord, float end ) {
  70. vec2 e = vec2( 1.0, 0.0 );
  71. for ( int i = 0; i < ${e.int(c.maxSearchSteps)}; i ++ ) {
  72. e = texture2D( edgesTex, texcoord, 0.0 ).rg;
  73. texcoord -= vec2( 0.0, 2.0 ) * resolution.xy;
  74. if ( ! ( texcoord.y < end && e.r > 0.8281 && e.g == 0.0 ) ) break;
  75. }
  76. texcoord.y += 0.25 * resolution.y;
  77. texcoord.y += resolution.y;
  78. texcoord.y += 2.0 * resolution.y;
  79. texcoord.y -= resolution.y * SMAASearchLength( searchTex, e.gr, 0.5, 0.5 );
  80. return texcoord.y;
  81. }
  82. vec2 SMAAArea( sampler2D areaTex, vec2 dist, float e1, float e2, float offset ) {
  83. vec2 texcoord = float( ${e.int(c.maxDistanceAreaTex)} ) * round( 4.0 * vec2( e1, e2 ) ) + dist;
  84. texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + ( 0.5 * SMAA_AREATEX_PIXEL_SIZE );
  85. texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
  86. return texture2D( areaTex, texcoord, 0.0 ).rg;
  87. }
  88. vec4 SMAABlendingWeightCalculationPS( vec2 texcoord, vec2 pixcoord, vec4 offset[ 3 ], sampler2D edgesTex, sampler2D areaTex, sampler2D searchTex, ivec4 subsampleIndices ) {
  89. vec4 weights = vec4( 0.0, 0.0, 0.0, 0.0 );
  90. vec2 e = texture2D( edgesTex, texcoord ).rg;
  91. if ( e.g > 0.0 ) {
  92. vec2 d;
  93. vec2 coords;
  94. coords.x = SMAASearchXLeft( edgesTex, searchTex, offset[ 0 ].xy, offset[ 2 ].x );
  95. coords.y = offset[ 1 ].y;
  96. d.x = coords.x;
  97. float e1 = texture2D( edgesTex, coords, 0.0 ).r;
  98. coords.x = SMAASearchXRight( edgesTex, searchTex, offset[ 0 ].zw, offset[ 2 ].y );
  99. d.y = coords.x;
  100. d = d * resolution.z - pixcoord.x;
  101. vec2 sqrt_d = sqrt( abs( d ) );
  102. coords.y -= 1.0 * resolution.y;
  103. float e2 = SMAASampleLevelZeroOffset( edgesTex, coords, vec2( 1.0, 0.0 ) ).r;
  104. weights.rg = SMAAArea( areaTex, sqrt_d, e1, e2, float( subsampleIndices.y ) );
  105. }
  106. if ( e.r > 0.0 ) {
  107. vec2 d;
  108. vec2 coords;
  109. coords.y = SMAASearchYUp( edgesTex, searchTex, offset[ 1 ].xy, offset[ 2 ].z );
  110. coords.x = offset[ 0 ].x;
  111. d.x = coords.y;
  112. float e1 = texture2D( edgesTex, coords, 0.0 ).g;
  113. coords.y = SMAASearchYDown( edgesTex, searchTex, offset[ 1 ].zw, offset[ 2 ].w );
  114. d.y = coords.y;
  115. d = d * resolution.w - pixcoord.y;
  116. vec2 sqrt_d = sqrt( abs( d ) );
  117. coords.y -= 1.0 * resolution.y;
  118. float e2 = SMAASampleLevelZeroOffset( edgesTex, coords, vec2( 0.0, 1.0 ) ).g;
  119. weights.ba = SMAAArea( areaTex, sqrt_d, e1, e2, float( subsampleIndices.x ) );
  120. // for some reason the following lines are necessary to prevent
  121. // texture lookup precision issues on some Intel integrated graphics chips
  122. vec4 dbg = (offset[ 0 ]+offset[ 1 ]+offset[ 2 ] + coords.xyyx);
  123. weights.r += 0.00000001 * dot(vec4(0,1,0,1),dbg);
  124. }
  125. return weights;
  126. }
  127. void main() {
  128. gl_FragColor = SMAABlendingWeightCalculationPS( fTexCoord, fPixCoord, fOffset, tEdges, tArea, tSearch, ivec4( 0.0 ) );
  129. }
  130. `),d}const x=Object.freeze(Object.defineProperty({__proto__:null,build:d},Symbol.toStringTag,{value:"Module"}));export{x as B,d as b};