EdgeDetect.glsl.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 t}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DPassUniform as o}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{VertexAttribute as a}from"../views/3d/webgl-engine/lib/VertexAttribute.js";import{addResolutionUniform as r}from"../views/3d/webgl-engine/shaders/SMAAPassParameters.js";const d={threshold:.05,localConstrastAdaption:2};function l(){const l=new t,{attributes:s,varyings:c,vertex:x,fragment:i}=l;return s.add(a.POSITION,"vec2"),r(x),c.add("fTexCoord","vec2"),c.add("fOffset[3]","vec4"),x.code.add(e`void EdgeDetectEdgeDetectionVS( vec2 texcoord ) {
  6. fOffset[0] = texcoord.xyxy + resolution.xyxy * vec4( -1.0, 0.0, 0.0, 1.0 );
  7. fOffset[1] = texcoord.xyxy + resolution.xyxy * vec4( 1.0, 0.0, 0.0, -1.0 );
  8. fOffset[2] = texcoord.xyxy + resolution.xyxy * vec4( -2.0, 0.0, 0.0, 2.0 );
  9. }
  10. void main() {
  11. fTexCoord = (position + 1.0) * 0.5;
  12. gl_Position = vec4(position, 0, 1);
  13. EdgeDetectEdgeDetectionVS(fTexCoord);
  14. }`),i.uniforms.add(new o("tColor",(e=>e.colorTexture))),i.code.add(e`
  15. vec4 EdgeDetectColorEdgeDetectionPS(vec2 texcoord, vec4 offset[3], sampler2D colorTex) {
  16. vec2 threshold = vec2( ${e.float(d.threshold)} );
  17. // Calculate color deltas:
  18. vec4 delta;
  19. vec3 C = texture2D( colorTex, texcoord ).rgb;
  20. vec3 Cleft = texture2D( colorTex, offset[0].xy ).rgb;
  21. vec3 t = abs( C - Cleft );
  22. delta.x = max( max( t.r, t.g ), t.b );
  23. vec3 Ctop = texture2D( colorTex, offset[0].zw ).rgb;
  24. t = abs( C - Ctop );
  25. delta.y = max( max( t.r, t.g ), t.b );
  26. // We do the usual threshold:
  27. vec2 edges = step( threshold, delta.xy );
  28. // Then discard if there is no edge:
  29. if ( dot( edges, vec2( 1.0, 1.0 ) ) == 0.0 )
  30. discard;
  31. // Calculate right and bottom deltas:
  32. vec3 Cright = texture2D( colorTex, offset[1].xy ).rgb;
  33. t = abs( C - Cright );
  34. delta.z = max( max( t.r, t.g ), t.b );
  35. vec3 Cbottom = texture2D( colorTex, offset[1].zw ).rgb;
  36. t = abs( C - Cbottom );
  37. delta.w = max( max( t.r, t.g ), t.b );
  38. // Calculate the maximum delta in the direct neighborhood:
  39. float maxDelta = max( max( max( delta.x, delta.y ), delta.z ), delta.w );
  40. // Calculate left-left and top-top deltas:
  41. vec3 Cleftleft = texture2D( colorTex, offset[2].xy ).rgb;
  42. t = abs( C - Cleftleft );
  43. delta.z = max( max( t.r, t.g ), t.b );
  44. vec3 Ctoptop = texture2D( colorTex, offset[2].zw ).rgb;
  45. t = abs( C - Ctoptop );
  46. delta.w = max( max( t.r, t.g ), t.b );
  47. // Calculate the final maximum delta:
  48. maxDelta = max( max( maxDelta, delta.z ), delta.w );
  49. // Local contrast adaptation in action:
  50. edges.xy *= step( maxDelta, float(${e.float(d.localConstrastAdaption)}) * delta.xy );
  51. return vec4( edges, 0.0, 0.0 );
  52. }
  53. void main() {
  54. gl_FragColor = EdgeDetectColorEdgeDetectionPS(fTexCoord, fOffset, tColor);
  55. }
  56. `),l}const s=Object.freeze(Object.defineProperty({__proto__:null,build:l},Symbol.toStringTag,{value:"Module"}));export{s as E,l as b};