BillboardCollectionFS.glsl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifdef GL_OES_standard_derivatives
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. uniform sampler2D u_atlas;
  5. #ifdef VECTOR_TILE
  6. uniform vec4 u_highlightColor;
  7. #endif
  8. in vec2 v_textureCoordinates;
  9. in vec4 v_pickColor;
  10. in vec4 v_color;
  11. #ifdef SDF
  12. in vec4 v_outlineColor;
  13. in float v_outlineWidth;
  14. #endif
  15. #ifdef FRAGMENT_DEPTH_CHECK
  16. in vec4 v_textureCoordinateBounds; // the min and max x and y values for the texture coordinates
  17. in vec4 v_originTextureCoordinateAndTranslate; // texture coordinate at the origin, billboard translate (used for label glyphs)
  18. in vec4 v_compressed; // x: eyeDepth, y: applyTranslate & enableDepthCheck, z: dimensions, w: imageSize
  19. in mat2 v_rotationMatrix;
  20. const float SHIFT_LEFT12 = 4096.0;
  21. const float SHIFT_LEFT1 = 2.0;
  22. const float SHIFT_RIGHT12 = 1.0 / 4096.0;
  23. const float SHIFT_RIGHT1 = 1.0 / 2.0;
  24. float getGlobeDepth(vec2 adjustedST, vec2 depthLookupST, bool applyTranslate, vec2 dimensions, vec2 imageSize)
  25. {
  26. vec2 lookupVector = imageSize * (depthLookupST - adjustedST);
  27. lookupVector = v_rotationMatrix * lookupVector;
  28. vec2 labelOffset = (dimensions - imageSize) * (depthLookupST - vec2(0.0, v_originTextureCoordinateAndTranslate.y)); // aligns label glyph with bounding rectangle. Will be zero for billboards because dimensions and imageSize will be equal
  29. vec2 translation = v_originTextureCoordinateAndTranslate.zw;
  30. if (applyTranslate)
  31. {
  32. // this is only needed for labels where the horizontal origin is not LEFT
  33. // it moves the label back to where the "origin" should be since all label glyphs are set to HorizontalOrigin.LEFT
  34. translation += (dimensions * v_originTextureCoordinateAndTranslate.xy * vec2(1.0, 0.0));
  35. }
  36. vec2 st = ((lookupVector - translation + labelOffset) + gl_FragCoord.xy) / czm_viewport.zw;
  37. float logDepthOrDepth = czm_unpackDepth(texture(czm_globeDepthTexture, st));
  38. if (logDepthOrDepth == 0.0)
  39. {
  40. return 0.0; // not on the globe
  41. }
  42. vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth);
  43. return eyeCoordinate.z / eyeCoordinate.w;
  44. }
  45. #endif
  46. #ifdef SDF
  47. // Get the distance from the edge of a glyph at a given position sampling an SDF texture.
  48. float getDistance(vec2 position)
  49. {
  50. return texture(u_atlas, position).r;
  51. }
  52. // Samples the sdf texture at the given position and produces a color based on the fill color and the outline.
  53. vec4 getSDFColor(vec2 position, float outlineWidth, vec4 outlineColor, float smoothing)
  54. {
  55. float distance = getDistance(position);
  56. if (outlineWidth > 0.0)
  57. {
  58. // Don't get the outline edge exceed the SDF_EDGE
  59. float outlineEdge = clamp(SDF_EDGE - outlineWidth, 0.0, SDF_EDGE);
  60. float outlineFactor = smoothstep(SDF_EDGE - smoothing, SDF_EDGE + smoothing, distance);
  61. vec4 sdfColor = mix(outlineColor, v_color, outlineFactor);
  62. float alpha = smoothstep(outlineEdge - smoothing, outlineEdge + smoothing, distance);
  63. return vec4(sdfColor.rgb, sdfColor.a * alpha);
  64. }
  65. else
  66. {
  67. float alpha = smoothstep(SDF_EDGE - smoothing, SDF_EDGE + smoothing, distance);
  68. return vec4(v_color.rgb, v_color.a * alpha);
  69. }
  70. }
  71. #endif
  72. void main()
  73. {
  74. vec4 color = texture(u_atlas, v_textureCoordinates);
  75. #ifdef SDF
  76. float outlineWidth = v_outlineWidth;
  77. vec4 outlineColor = v_outlineColor;
  78. // Get the current distance
  79. float distance = getDistance(v_textureCoordinates);
  80. #if (__VERSION__ == 300 || defined(GL_OES_standard_derivatives))
  81. float smoothing = fwidth(distance);
  82. // Get an offset that is approximately half the distance to the neighbor pixels
  83. // 0.354 is approximately half of 1/sqrt(2)
  84. vec2 sampleOffset = 0.354 * vec2(dFdx(v_textureCoordinates) + dFdy(v_textureCoordinates));
  85. // Sample the center point
  86. vec4 center = getSDFColor(v_textureCoordinates, outlineWidth, outlineColor, smoothing);
  87. // Sample the 4 neighbors
  88. vec4 color1 = getSDFColor(v_textureCoordinates + vec2(sampleOffset.x, sampleOffset.y), outlineWidth, outlineColor, smoothing);
  89. vec4 color2 = getSDFColor(v_textureCoordinates + vec2(-sampleOffset.x, sampleOffset.y), outlineWidth, outlineColor, smoothing);
  90. vec4 color3 = getSDFColor(v_textureCoordinates + vec2(-sampleOffset.x, -sampleOffset.y), outlineWidth, outlineColor, smoothing);
  91. vec4 color4 = getSDFColor(v_textureCoordinates + vec2(sampleOffset.x, -sampleOffset.y), outlineWidth, outlineColor, smoothing);
  92. // Equally weight the center sample and the 4 neighboring samples
  93. color = (center + color1 + color2 + color3 + color4)/5.0;
  94. #else
  95. // If no derivatives available (IE 10?), just do a single sample
  96. float smoothing = 1.0/32.0;
  97. color = getSDFColor(v_textureCoordinates, outlineWidth, outlineColor, smoothing);
  98. #endif
  99. color = czm_gammaCorrect(color);
  100. #else
  101. color = czm_gammaCorrect(color);
  102. color *= czm_gammaCorrect(v_color);
  103. #endif
  104. // Fully transparent parts of the billboard are not pickable.
  105. #if !defined(OPAQUE) && !defined(TRANSLUCENT)
  106. if (color.a < 0.005) // matches 0/255 and 1/255
  107. {
  108. discard;
  109. }
  110. #else
  111. // The billboard is rendered twice. The opaque pass discards translucent fragments
  112. // and the translucent pass discards opaque fragments.
  113. #ifdef OPAQUE
  114. if (color.a < 0.995) // matches < 254/255
  115. {
  116. discard;
  117. }
  118. #else
  119. if (color.a >= 0.995) // matches 254/255 and 255/255
  120. {
  121. discard;
  122. }
  123. #endif
  124. #endif
  125. #ifdef VECTOR_TILE
  126. color *= u_highlightColor;
  127. #endif
  128. out_FragColor = color;
  129. #ifdef LOG_DEPTH
  130. czm_writeLogDepth();
  131. #endif
  132. #ifdef FRAGMENT_DEPTH_CHECK
  133. float temp = v_compressed.y;
  134. temp = temp * SHIFT_RIGHT1;
  135. float temp2 = (temp - floor(temp)) * SHIFT_LEFT1;
  136. bool enableDepthTest = temp2 != 0.0;
  137. bool applyTranslate = floor(temp) != 0.0;
  138. if (enableDepthTest) {
  139. temp = v_compressed.z;
  140. temp = temp * SHIFT_RIGHT12;
  141. vec2 dimensions;
  142. dimensions.y = (temp - floor(temp)) * SHIFT_LEFT12;
  143. dimensions.x = floor(temp);
  144. temp = v_compressed.w;
  145. temp = temp * SHIFT_RIGHT12;
  146. vec2 imageSize;
  147. imageSize.y = (temp - floor(temp)) * SHIFT_LEFT12;
  148. imageSize.x = floor(temp);
  149. vec2 adjustedST = v_textureCoordinates - v_textureCoordinateBounds.xy;
  150. adjustedST = adjustedST / vec2(v_textureCoordinateBounds.z - v_textureCoordinateBounds.x, v_textureCoordinateBounds.w - v_textureCoordinateBounds.y);
  151. float epsilonEyeDepth = v_compressed.x + czm_epsilon1;
  152. float globeDepth1 = getGlobeDepth(adjustedST, v_originTextureCoordinateAndTranslate.xy, applyTranslate, dimensions, imageSize);
  153. // negative values go into the screen
  154. if (globeDepth1 != 0.0 && globeDepth1 > epsilonEyeDepth)
  155. {
  156. float globeDepth2 = getGlobeDepth(adjustedST, vec2(0.0, 1.0), applyTranslate, dimensions, imageSize); // top left corner
  157. if (globeDepth2 != 0.0 && globeDepth2 > epsilonEyeDepth)
  158. {
  159. float globeDepth3 = getGlobeDepth(adjustedST, vec2(1.0, 1.0), applyTranslate, dimensions, imageSize); // top right corner
  160. if (globeDepth3 != 0.0 && globeDepth3 > epsilonEyeDepth)
  161. {
  162. discard;
  163. }
  164. }
  165. }
  166. }
  167. #endif
  168. }