BillboardCollectionFS.js 7.9 KB

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