智慧水务管理系统 - 精河县供水工程综合管理平台

BillboardCollectionFS.glsl 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. uniform sampler2D u_atlas;
  2. uniform float u_coarseDepthTestDistance;
  3. uniform float u_threePointDepthTestDistance;
  4. #ifdef VECTOR_TILE
  5. uniform vec4 u_highlightColor;
  6. #endif
  7. in vec2 v_textureCoordinates;
  8. in vec4 v_pickColor;
  9. in vec4 v_color;
  10. flat in vec2 v_splitDirectionAndEllipsoidDepthEC;
  11. #ifdef SDF
  12. in vec4 v_outlineColor;
  13. in float v_outlineWidth;
  14. #endif
  15. in vec4 v_compressed; // x: eyeDepth, y: applyTranslate & enableDepthCheck, z: dimensions, w: imageSize
  16. const float SHIFT_LEFT1 = 2.0;
  17. const float SHIFT_RIGHT1 = 1.0 / 2.0;
  18. float getGlobeDepthAtCoords(vec2 st)
  19. {
  20. float logDepthOrDepth = czm_unpackDepth(texture(czm_globeDepthTexture, st));
  21. if (logDepthOrDepth == 0.0)
  22. {
  23. return 0.0; // not on the globe
  24. }
  25. vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth);
  26. return eyeCoordinate.z / eyeCoordinate.w;
  27. }
  28. #ifdef SDF
  29. // Get the distance from the edge of a glyph at a given position sampling an SDF texture.
  30. float getDistance(vec2 position)
  31. {
  32. return texture(u_atlas, position).r;
  33. }
  34. // Samples the sdf texture at the given position and produces a color based on the fill color and the outline.
  35. vec4 getSDFColor(vec2 position, float outlineWidth, vec4 outlineColor, float smoothing)
  36. {
  37. float distance = getDistance(position);
  38. if (outlineWidth > 0.0)
  39. {
  40. // Don't get the outline edge exceed the SDF_EDGE
  41. float outlineEdge = clamp(SDF_EDGE - outlineWidth, 0.0, SDF_EDGE);
  42. float outlineFactor = smoothstep(SDF_EDGE - smoothing, SDF_EDGE + smoothing, distance);
  43. vec4 sdfColor = mix(outlineColor, v_color, outlineFactor);
  44. float alpha = smoothstep(outlineEdge - smoothing, outlineEdge + smoothing, distance);
  45. return vec4(sdfColor.rgb, sdfColor.a * alpha);
  46. }
  47. else
  48. {
  49. float alpha = smoothstep(SDF_EDGE - smoothing, SDF_EDGE + smoothing, distance);
  50. return vec4(v_color.rgb, v_color.a * alpha);
  51. }
  52. }
  53. #endif
  54. bool getDepthTestEnabled() {
  55. float temp = v_compressed.y;
  56. temp = temp * SHIFT_RIGHT1;
  57. float temp2 = (temp - floor(temp)) * SHIFT_LEFT1;
  58. return temp2 != 0.0;
  59. }
  60. float getRelativeEyeDepth(float eyeDepth, float distanceToEllipsoid, float epsilon) {
  61. float depthDifferential = eyeDepth - distanceToEllipsoid;
  62. float depthRatio = abs(depthDifferential / distanceToEllipsoid);
  63. if (depthRatio < epsilon) {
  64. // The approximations are imprecise, so use an epsilon check for small value differences and assume a value of 0.0
  65. return 0.0;
  66. }
  67. return depthDifferential;
  68. }
  69. // Extra manual depth testing is done to allow more control over how a billboard is occluded
  70. // by the globe when near and far from the camera.
  71. void doDepthTest(float eyeDepth, float globeDepth) {
  72. #ifdef VS_THREE_POINT_DEPTH_CHECK
  73. // Since discarding vertices is not possible, the vertex shader sets eyeDepth to 0 to indicate the depth test failed. Apply the discard here.
  74. if (eyeDepth > -u_threePointDepthTestDistance) {
  75. if (eyeDepth == 0.0) {
  76. discard;
  77. }
  78. return;
  79. }
  80. #endif
  81. bool useGlobeDepth = eyeDepth > -u_coarseDepthTestDistance;
  82. if (useGlobeDepth && globeDepth == 0.0) {
  83. // Pixel is not on the globe, so there is no distance to compare against. Pass.
  84. return;
  85. }
  86. // If the camera is close, compare against the globe depth texture that includes depth from the 3D tile pass.
  87. if (useGlobeDepth && getRelativeEyeDepth(eyeDepth, globeDepth, czm_epsilon1) < 0.0) {
  88. discard;
  89. }
  90. }
  91. #ifdef LOG_DEPTH
  92. void writeDepth(float eyeDepth, float globeDepth, float distanceToEllipsoid) {
  93. // If we've made it here, the manual depth test above determined that this fragment should be visible.
  94. // But the automatic depth test must still run in order to write the result to the depth buffer, and its results may
  95. // disagree with our manual depth test's results. To prefer our manual results when in front of the globe, apply an offset towards the camera.
  96. float depthArg = v_depthFromNearPlusOne;
  97. if (globeDepth != 0.0 && getRelativeEyeDepth(eyeDepth, distanceToEllipsoid, czm_epsilon3) > 0.0) {
  98. float globeDepthFromNearPlusOne = (-globeDepth - czm_currentFrustum.x) + 1.0;
  99. float nudge = max(globeDepthFromNearPlusOne * 5e-6, czm_epsilon7);
  100. float globeOnTop = max(1.0, globeDepthFromNearPlusOne - nudge);
  101. depthArg = min(depthArg, globeOnTop);
  102. }
  103. czm_writeLogDepth(depthArg);
  104. }
  105. #endif
  106. void main()
  107. {
  108. if (v_splitDirectionAndEllipsoidDepthEC.x < 0.0 && gl_FragCoord.x > czm_splitPosition) {
  109. discard;
  110. }
  111. if (v_splitDirectionAndEllipsoidDepthEC.x > 0.0 && gl_FragCoord.x < czm_splitPosition) {
  112. discard;
  113. }
  114. if (getDepthTestEnabled()) {
  115. vec2 fragSt = gl_FragCoord.xy / czm_viewport.zw;
  116. float eyeDepth = v_compressed.x;
  117. float globeDepth = getGlobeDepthAtCoords(fragSt);
  118. float distanceToEllipsoid = -v_splitDirectionAndEllipsoidDepthEC.y;
  119. doDepthTest(eyeDepth, globeDepth);
  120. #ifdef LOG_DEPTH
  121. writeDepth(eyeDepth, globeDepth, distanceToEllipsoid);
  122. #endif
  123. }
  124. vec4 color = texture(u_atlas, v_textureCoordinates);
  125. #ifdef SDF
  126. float outlineWidth = v_outlineWidth;
  127. vec4 outlineColor = v_outlineColor;
  128. // Get the current distance
  129. float distance = getDistance(v_textureCoordinates);
  130. #if (__VERSION__ == 300 || defined(GL_OES_standard_derivatives))
  131. float smoothing = fwidth(distance);
  132. // Get an offset that is approximately half the distance to the neighbor pixels
  133. // 0.354 is approximately half of 1/sqrt(2)
  134. vec2 sampleOffset = 0.354 * vec2(dFdx(v_textureCoordinates) + dFdy(v_textureCoordinates));
  135. // Sample the center point
  136. vec4 center = getSDFColor(v_textureCoordinates, outlineWidth, outlineColor, smoothing);
  137. // Sample the 4 neighbors
  138. vec4 color1 = getSDFColor(v_textureCoordinates + vec2(sampleOffset.x, sampleOffset.y), outlineWidth, outlineColor, smoothing);
  139. vec4 color2 = getSDFColor(v_textureCoordinates + vec2(-sampleOffset.x, sampleOffset.y), outlineWidth, outlineColor, smoothing);
  140. vec4 color3 = getSDFColor(v_textureCoordinates + vec2(-sampleOffset.x, -sampleOffset.y), outlineWidth, outlineColor, smoothing);
  141. vec4 color4 = getSDFColor(v_textureCoordinates + vec2(sampleOffset.x, -sampleOffset.y), outlineWidth, outlineColor, smoothing);
  142. // Equally weight the center sample and the 4 neighboring samples
  143. color = (center + color1 + color2 + color3 + color4)/5.0;
  144. #else
  145. // If no derivatives available (IE 10?), just do a single sample
  146. float smoothing = 1.0/32.0;
  147. color = getSDFColor(v_textureCoordinates, outlineWidth, outlineColor, smoothing);
  148. #endif
  149. color = czm_gammaCorrect(color);
  150. #else
  151. color = czm_gammaCorrect(color);
  152. color *= czm_gammaCorrect(v_color);
  153. #endif
  154. // Fully transparent parts of the billboard are not pickable.
  155. #if !defined(OPAQUE) && !defined(TRANSLUCENT)
  156. if (color.a < 0.005) // matches 0/255 and 1/255
  157. {
  158. discard;
  159. }
  160. #else
  161. // The billboard is rendered twice. The opaque pass discards translucent fragments
  162. // and the translucent pass discards opaque fragments.
  163. #ifdef OPAQUE
  164. if (color.a < 0.995) // matches < 254/255
  165. {
  166. discard;
  167. }
  168. #else
  169. if (color.a >= 0.995) // matches 254/255 and 255/255
  170. {
  171. discard;
  172. }
  173. #endif
  174. #endif
  175. #ifdef VECTOR_TILE
  176. color *= u_highlightColor;
  177. #endif
  178. out_FragColor = color;
  179. }