RibbonLine.glsl.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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{unwrapOr as e}from"../core/maybe.js";import{addLinearDepth as i,addCalculateLinearDepth as t}from"../views/3d/webgl-engine/core/shaderLibrary/ForwardLinearDepth.glsl.js";import{ShaderOutput as o}from"../views/3d/webgl-engine/core/shaderLibrary/ShaderOutput.js";import{SliceDraw as r}from"../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl.js";import{ObjectAndLayerIdColor as n}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/ObjectAndLayerIdColor.glsl.js";import{RibbonVertexPosition as a}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/RibbonVertexPosition.glsl.js";import{OutputDepth as s}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputDepth.glsl.js";import{LineStipple as l,computePixelSize as d}from"../views/3d/webgl-engine/core/shaderLibrary/shading/LineStipple.glsl.js";import{MarkerSizing as p}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MarkerSizing.glsl.js";import{multipassTerrainTest as c}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MultipassTerrainTest.glsl.js";import{PiUtils as g}from"../views/3d/webgl-engine/core/shaderLibrary/shading/PiUtils.glsl.js";import{symbolAlphaCutoff as m}from"../views/3d/webgl-engine/core/shaderLibrary/util/AlphaCutoff.js";import{ColorConversion as v}from"../views/3d/webgl-engine/core/shaderLibrary/util/ColorConversion.glsl.js";import{addProjViewLocalOrigin as f}from"../views/3d/webgl-engine/core/shaderLibrary/util/View.glsl.js";import{Float2PassUniform as h}from"../views/3d/webgl-engine/core/shaderModules/Float2PassUniform.js";import{Float4PassUniform as u}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as D}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as S}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{Matrix4PassUniform as b}from"../views/3d/webgl-engine/core/shaderModules/Matrix4PassUniform.js";import{ShaderBuilder as x}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{TransparencyPassType as L}from"../views/3d/webgl-engine/lib/TransparencyPassType.js";import{VertexAttribute as w}from"../views/3d/webgl-engine/lib/VertexAttribute.js";import{LineMarkerSpace as y}from"../views/3d/webgl-engine/shaders/LineMarkerTechniqueConfiguration.js";import{CapType as C}from"../views/3d/webgl-engine/shaders/RibbonLineTechniqueConfiguration.js";const j=1;function R(R){const A=new x,{vertex:P,fragment:F}=A,z=R.hasMultipassTerrain&&(R.output===o.Color||R.output===o.Alpha);A.include(g),A.include(a,R),A.include(l,R);const E=R.applyMarkerOffset&&!R.draped;E&&(P.uniforms.add(new D("markerScale",(e=>e.markerScale))),A.include(p,{space:y.World})),R.output===o.Depth&&A.include(s,R),A.include(n,R),f(P,R),P.uniforms.add([new b("inverseProjectionMatrix",((e,i)=>i.camera.inverseProjectionMatrix)),new h("nearFar",((e,i)=>i.camera.nearFar)),new D("miterLimit",(e=>"miter"!==e.join?0:e.miterLimit)),new u("viewport",((e,i)=>i.camera.fullViewport))]),P.constants.add("LARGE_HALF_FLOAT","float",65500),A.attributes.add(w.POSITION,"vec3"),A.attributes.add(w.SUBDIVISIONFACTOR,"float"),A.attributes.add(w.UV0,"vec2"),A.attributes.add(w.AUXPOS1,"vec3"),A.attributes.add(w.AUXPOS2,"vec3"),A.varyings.add("vColor","vec4"),A.varyings.add("vpos","vec3"),i(A),z&&A.varyings.add("depth","float");const T=R.capType===C.ROUND,W=R.stippleEnabled&&R.stippleScaleWithLineWidth||T;W&&A.varyings.add("vLineWidth","float");const V=R.stippleEnabled&&R.stippleScaleWithLineWidth;V&&A.varyings.add("vLineSizeInv","float");const O=R.innerColorEnabled||T;O&&A.varyings.add("vLineDistance","float");const N=R.stippleEnabled&&T,_=R.falloffEnabled||N;_&&A.varyings.add("vLineDistanceNorm","float"),T&&(A.varyings.add("vSegmentSDF","float"),A.varyings.add("vReverseSegmentSDF","float")),P.code.add(S`#define PERPENDICULAR(v) vec2(v.y, -v.x);
  6. float interp(float ncp, vec4 a, vec4 b) {
  7. return (-ncp - a.z) / (b.z - a.z);
  8. }
  9. vec2 rotate(vec2 v, float a) {
  10. float s = sin(a);
  11. float c = cos(a);
  12. mat2 m = mat2(c, -s, s, c);
  13. return m * v;
  14. }`),P.code.add(S`vec4 projectAndScale(vec4 pos) {
  15. vec4 posNdc = proj * pos;
  16. posNdc.xy *= viewport.zw / posNdc.w;
  17. return posNdc;
  18. }`),t(A),P.code.add(S`
  19. void clipAndTransform(inout vec4 pos, inout vec4 prev, inout vec4 next, in bool isStartVertex) {
  20. float vnp = nearFar[0] * 0.99;
  21. if(pos.z > -nearFar[0]) {
  22. //current pos behind ncp --> we need to clip
  23. if (!isStartVertex) {
  24. if(prev.z < -nearFar[0]) {
  25. //previous in front of ncp
  26. pos = mix(prev, pos, interp(vnp, prev, pos));
  27. next = pos;
  28. } else {
  29. pos = vec4(0.0, 0.0, 0.0, 1.0);
  30. }
  31. } else {
  32. if(next.z < -nearFar[0]) {
  33. //next in front of ncp
  34. pos = mix(pos, next, interp(vnp, pos, next));
  35. prev = pos;
  36. } else {
  37. pos = vec4(0.0, 0.0, 0.0, 1.0);
  38. }
  39. }
  40. } else {
  41. //current position visible
  42. if (prev.z > -nearFar[0]) {
  43. //previous behind ncp
  44. prev = mix(pos, prev, interp(vnp, pos, prev));
  45. }
  46. if (next.z > -nearFar[0]) {
  47. //next behind ncp
  48. next = mix(next, pos, interp(vnp, next, pos));
  49. }
  50. }
  51. ${z?"depth = pos.z;":""}
  52. linearDepth = calculateLinearDepth(nearFar,pos.z);
  53. pos = projectAndScale(pos);
  54. next = projectAndScale(next);
  55. prev = projectAndScale(prev);
  56. }
  57. `),P.uniforms.add(new D("pixelRatio",((e,i)=>i.camera.pixelRatio))),P.code.add(S`
  58. void main(void) {
  59. // unpack values from uv0.y
  60. bool isStartVertex = abs(abs(uv0.y)-3.0) == 1.0;
  61. float coverage = 1.0;
  62. // Check for special value of uv0.y which is used by the Renderer when graphics
  63. // are removed before the VBO is recompacted. If this is the case, then we just
  64. // project outside of clip space.
  65. if (uv0.y == 0.0) {
  66. // Project out of clip space
  67. gl_Position = vec4(1e038, 1e038, 1e038, 1.0);
  68. }
  69. else {
  70. bool isJoin = abs(uv0.y) < 3.0;
  71. float lineSize = getSize();
  72. float lineWidth = lineSize * pixelRatio;
  73. ${W?S`vLineWidth = lineWidth;`:""}
  74. ${V?S`vLineSizeInv = 1.0 / lineSize;`:""}
  75. // convert sub-pixel coverage to alpha
  76. if (lineWidth < 1.0) {
  77. coverage = lineWidth;
  78. lineWidth = 1.0;
  79. }else{
  80. // Ribbon lines cannot properly render non-integer sizes. Round width to integer size if
  81. // larger than one for better quality. Note that we do render < 1 pixels more or less correctly
  82. // so we only really care to round anything larger than 1.
  83. lineWidth = floor(lineWidth + 0.5);
  84. }
  85. vec4 pos = view * vec4(position.xyz, 1.0);
  86. vec4 prev = view * vec4(auxpos1.xyz, 1.0);
  87. vec4 next = view * vec4(auxpos2.xyz, 1.0);
  88. `),E&&P.code.add(S`vec4 other = isStartVertex ? next : prev;
  89. bool markersHidden = areWorldMarkersHidden(pos, other);
  90. if(!isJoin && !markersHidden) {
  91. pos.xyz += normalize(other.xyz - pos.xyz) * getWorldMarkerSize(pos) * 0.5;
  92. }`),P.code.add(S`clipAndTransform(pos, prev, next, isStartVertex);
  93. vec2 left = (pos.xy - prev.xy);
  94. vec2 right = (next.xy - pos.xy);
  95. float leftLen = length(left);
  96. float rightLen = length(right);`);(R.stippleEnabled||T)&&P.code.add(S`
  97. float isEndVertex = float(!isStartVertex);
  98. vec2 segmentOrigin = mix(pos.xy, prev.xy, isEndVertex);
  99. vec2 segment = mix(right, left, isEndVertex);
  100. ${T?S`vec2 segmentEnd = mix(next.xy, pos.xy, isEndVertex);`:""}
  101. `),P.code.add(S`left = (leftLen > 0.001) ? left/leftLen : vec2(0.0, 0.0);
  102. right = (rightLen > 0.001) ? right/rightLen : vec2(0.0, 0.0);
  103. vec2 capDisplacementDir = vec2(0, 0);
  104. vec2 joinDisplacementDir = vec2(0, 0);
  105. float displacementLen = lineWidth;
  106. if (isJoin) {
  107. bool isOutside = (left.x * right.y - left.y * right.x) * uv0.y > 0.0;
  108. joinDisplacementDir = normalize(left + right);
  109. joinDisplacementDir = PERPENDICULAR(joinDisplacementDir);
  110. if (leftLen > 0.001 && rightLen > 0.001) {
  111. float nDotSeg = dot(joinDisplacementDir, left);
  112. displacementLen /= length(nDotSeg * left - joinDisplacementDir);
  113. if (!isOutside) {
  114. displacementLen = min(displacementLen, min(leftLen, rightLen)/abs(nDotSeg));
  115. }
  116. }
  117. if (isOutside && (displacementLen > miterLimit * lineWidth)) {`),R.roundJoins?P.code.add(S`
  118. vec2 startDir = leftLen < 0.001 ? right : left;
  119. startDir = PERPENDICULAR(startDir);
  120. vec2 endDir = rightLen < 0.001 ? left : right;
  121. endDir = PERPENDICULAR(endDir);
  122. float factor = ${R.stippleEnabled?S`min(1.0, subdivisionFactor * ${S.float((j+2)/(j+1))})`:S`subdivisionFactor`};
  123. float rotationAngle = acos(clamp(dot(startDir, endDir), -1.0, 1.0));
  124. joinDisplacementDir = rotate(startDir, -sign(uv0.y) * factor * rotationAngle);
  125. `):P.code.add(S`if (leftLen < 0.001) {
  126. joinDisplacementDir = right;
  127. }
  128. else if (rightLen < 0.001) {
  129. joinDisplacementDir = left;
  130. }
  131. else {
  132. joinDisplacementDir = (isStartVertex || subdivisionFactor > 0.0) ? right : left;
  133. }
  134. joinDisplacementDir = PERPENDICULAR(joinDisplacementDir);`);const I=R.capType!==C.BUTT;return P.code.add(S`
  135. displacementLen = lineWidth;
  136. }
  137. } else {
  138. // CAP handling ---------------------------------------------------
  139. joinDisplacementDir = isStartVertex ? right : left;
  140. joinDisplacementDir = PERPENDICULAR(joinDisplacementDir);
  141. ${I?S`capDisplacementDir = isStartVertex ? -right : left;`:""}
  142. }
  143. `),P.code.add(S`
  144. // Displacement (in pixels) caused by join/or cap
  145. vec2 dpos = joinDisplacementDir * sign(uv0.y) * displacementLen + capDisplacementDir * displacementLen;
  146. ${_||O?S`float lineDistNorm = sign(uv0.y) * pos.w;`:""}
  147. ${O?S`vLineDistance = lineWidth * lineDistNorm;`:""}
  148. ${_?S`vLineDistanceNorm = lineDistNorm;`:""}
  149. pos.xy += dpos;
  150. `),T&&P.code.add(S`vec2 segmentDir = normalize(segment);
  151. vSegmentSDF = (isJoin && isStartVertex) ? LARGE_HALF_FLOAT : (dot(pos.xy - segmentOrigin, segmentDir) * pos.w) ;
  152. vReverseSegmentSDF = (isJoin && !isStartVertex) ? LARGE_HALF_FLOAT : (dot(pos.xy - segmentEnd, -segmentDir) * pos.w);`),R.stippleEnabled&&(R.draped?P.uniforms.add(new D("worldToScreenRatio",((e,i)=>1/i.screenToPCSRatio))):P.code.add(S`vec3 segmentCenter = mix((auxpos2 + position) * 0.5, (position + auxpos1) * 0.5, isEndVertex);
  153. float worldToScreenRatio = computeWorldToScreenRatio(segmentCenter);`),P.code.add(S`float segmentLengthScreenDouble = length(segment);
  154. float segmentLengthScreen = segmentLengthScreenDouble * 0.5;
  155. float discreteWorldToScreenRatio = discretizeWorldToScreenRatio(worldToScreenRatio);
  156. float segmentLengthRender = length(mix(auxpos2 - position, position - auxpos1, isEndVertex));
  157. vStipplePatternStretch = worldToScreenRatio / discreteWorldToScreenRatio;`),R.draped?P.code.add(S`float segmentLengthPseudoScreen = segmentLengthScreen / pixelRatio * discreteWorldToScreenRatio / worldToScreenRatio;
  158. float startPseudoScreen = uv0.x * discreteWorldToScreenRatio - mix(0.0, segmentLengthPseudoScreen, isEndVertex);`):P.code.add(S`float startPseudoScreen = mix(uv0.x, uv0.x - segmentLengthRender, isEndVertex) * discreteWorldToScreenRatio;
  159. float segmentLengthPseudoScreen = segmentLengthRender * discreteWorldToScreenRatio;`),P.uniforms.add(new D("stipplePatternPixelSize",(e=>d(e)))),P.code.add(S`
  160. float patternLength = ${R.stippleScaleWithLineWidth?"lineSize * ":""} stipplePatternPixelSize;
  161. // Compute the coordinates at both start and end of the line segment, because we need both to clamp to in the fragment shader
  162. vStippleDistanceLimits = computeStippleDistanceLimits(startPseudoScreen, segmentLengthPseudoScreen, segmentLengthScreen, patternLength);
  163. vStippleDistance = mix(vStippleDistanceLimits.x, vStippleDistanceLimits.y, isEndVertex);
  164. // Adjust the coordinate to the displaced position (the pattern is shortened/overextended on the in/outside of joins)
  165. if (segmentLengthScreenDouble >= 0.001) {
  166. // Project the actual vertex position onto the line segment. Note that the resulting factor is within [0..1] at the
  167. // original vertex positions, and slightly outside of that range at the displaced positions
  168. vec2 stippleDisplacement = pos.xy - segmentOrigin;
  169. float stippleDisplacementFactor = dot(segment, stippleDisplacement) / (segmentLengthScreenDouble * segmentLengthScreenDouble);
  170. // Apply this offset to the actual vertex coordinate (can be screen or pseudo-screen space)
  171. vStippleDistance += (stippleDisplacementFactor - isEndVertex) * (vStippleDistanceLimits.y - vStippleDistanceLimits.x);
  172. }
  173. // Cancel out perspective correct interpolation because we want this length the really represent the screen distance
  174. vStippleDistanceLimits *= pos.w;
  175. vStippleDistance *= pos.w;
  176. // Disable stipple distance limits on caps
  177. vStippleDistanceLimits = isJoin ?
  178. vStippleDistanceLimits :
  179. isStartVertex ?
  180. vec2(-1e038, vStippleDistanceLimits.y) :
  181. vec2(vStippleDistanceLimits.x, 1e038);
  182. `)),P.code.add(S`
  183. // Convert back into NDC
  184. pos.xy = (pos.xy / viewport.zw) * pos.w;
  185. vColor = getColor();
  186. vColor.a *= coverage;
  187. ${R.wireframe&&!R.draped?"pos.z -= 0.001 * pos.w;":""}
  188. // transform final position to camera space for slicing
  189. vpos = (inverseProjectionMatrix * pos).xyz;
  190. gl_Position = pos;
  191. forwardObjectAndLayerIdColor();
  192. }
  193. }
  194. `),z&&A.include(c,R),A.include(r,R),F.include(v),F.code.add(S`
  195. void main() {
  196. discardBySlice(vpos);
  197. ${z?"terrainDepthTest(gl_FragCoord, depth);":""}
  198. `),R.wireframe?F.code.add(S`vec4 finalColor = vec4(1.0, 0.0, 1.0, 1.0);`):(T&&F.code.add(S`
  199. float sdf = min(vSegmentSDF, vReverseSegmentSDF);
  200. vec2 fragmentPosition = vec2(
  201. min(sdf, 0.0),
  202. vLineDistance
  203. ) * gl_FragCoord.w;
  204. float fragmentRadius = length(fragmentPosition);
  205. float fragmentCapSDF = (fragmentRadius - vLineWidth) * 0.5; // Divide by 2 to transform from double pixel scale
  206. float capCoverage = clamp(0.5 - fragmentCapSDF, 0.0, 1.0);
  207. if (capCoverage < ${S.float(m)}) {
  208. discard;
  209. }
  210. `),N?F.code.add(S`
  211. vec2 stipplePosition = vec2(
  212. min(getStippleSDF() * 2.0 - 1.0, 0.0),
  213. vLineDistanceNorm * gl_FragCoord.w
  214. );
  215. float stippleRadius = length(stipplePosition * vLineWidth);
  216. float stippleCapSDF = (stippleRadius - vLineWidth) * 0.5; // Divide by 2 to transform from double pixel scale
  217. float stippleCoverage = clamp(0.5 - stippleCapSDF, 0.0, 1.0);
  218. float stippleAlpha = step(${S.float(m)}, stippleCoverage);
  219. `):F.code.add(S`float stippleAlpha = getStippleAlpha();`),F.uniforms.add(new u("intrinsicColor",(e=>e.color))),F.code.add(S`discardByStippleAlpha(stippleAlpha, stippleAlphaColorDiscard);
  220. vec4 color = intrinsicColor * vColor;`),R.innerColorEnabled&&(F.uniforms.add(new u("innerColor",(i=>e(i.innerColor,i.color)))),F.uniforms.add(new D("innerWidth",((e,i)=>e.innerWidth*i.camera.pixelRatio))),F.code.add(S`float distToInner = abs(vLineDistance * gl_FragCoord.w) - innerWidth;
  221. float innerAA = clamp(0.5 - distToInner, 0.0, 1.0);
  222. float innerAlpha = innerColor.a + color.a * (1.0 - innerColor.a);
  223. color = mix(color, vec4(innerColor.rgb, innerAlpha), innerAA);`)),F.code.add(S`vec4 finalColor = blendStipple(color, stippleAlpha);`),R.falloffEnabled&&(F.uniforms.add(new D("falloff",(e=>e.falloff))),F.code.add(S`finalColor.a *= pow(max(0.0, 1.0 - abs(vLineDistanceNorm * gl_FragCoord.w)), falloff);`))),F.code.add(S`
  224. if (finalColor.a < ${S.float(m)}) {
  225. discard;
  226. }
  227. ${R.output===o.Alpha?S`gl_FragColor = vec4(finalColor.a);`:""}
  228. ${R.output===o.Color?S`gl_FragColor = highlightSlice(finalColor, vpos);`:""}
  229. ${R.output===o.Color&&R.transparencyPassType===L.Color?"gl_FragColor = premultiplyAlpha(gl_FragColor);":""}
  230. ${R.output===o.Highlight?S`gl_FragColor = vec4(1.0);`:""}
  231. ${R.output===o.Depth?S`outputDepth(linearDepth);`:""}
  232. ${R.output===o.ObjectAndLayerIdColor?S`outputObjectAndLayerIdColor();`:""}
  233. }
  234. `),A}const A=Object.freeze(Object.defineProperty({__proto__:null,NUM_ROUND_JOIN_SUBDIVISIONS:j,build:R},Symbol.toStringTag,{value:"Module"}));export{j as N,A as R,R as b};