BillboardCollectionVS.glsl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #ifdef INSTANCED
  2. in vec2 direction;
  3. #endif
  4. in vec4 positionHighAndScale;
  5. in vec4 positionLowAndRotation;
  6. in vec4 compressedAttribute0; // pixel offset, translate, horizontal origin, vertical origin, show, direction, texture coordinates (texture offset)
  7. in vec4 compressedAttribute1; // aligned axis, translucency by distance, image width
  8. in vec4 compressedAttribute2; // label horizontal origin, image height, color, pick color, size in meters, valid aligned axis, 13 bits free
  9. in vec4 eyeOffset; // eye offset in meters, 4 bytes free (texture range)
  10. in vec4 scaleByDistance; // near, nearScale, far, farScale
  11. in vec4 pixelOffsetScaleByDistance; // near, nearScale, far, farScale
  12. in vec4 compressedAttribute3; // distance display condition near, far, disableDepthTestDistance, dimensions
  13. in vec2 sdf; // sdf outline color (rgb) and width (w)
  14. #if defined(VERTEX_DEPTH_CHECK) || defined(FRAGMENT_DEPTH_CHECK)
  15. in vec4 textureCoordinateBoundsOrLabelTranslate; // the min and max x and y values for the texture coordinates
  16. #endif
  17. #ifdef VECTOR_TILE
  18. in float a_batchId;
  19. #endif
  20. out vec2 v_textureCoordinates;
  21. #ifdef FRAGMENT_DEPTH_CHECK
  22. out vec4 v_textureCoordinateBounds;
  23. out vec4 v_originTextureCoordinateAndTranslate;
  24. out vec4 v_compressed; // x: eyeDepth, y: applyTranslate & enableDepthCheck, z: dimensions, w: imageSize
  25. out mat2 v_rotationMatrix;
  26. #endif
  27. out vec4 v_pickColor;
  28. out vec4 v_color;
  29. #ifdef SDF
  30. out vec4 v_outlineColor;
  31. out float v_outlineWidth;
  32. #endif
  33. const float UPPER_BOUND = 32768.0;
  34. const float SHIFT_LEFT16 = 65536.0;
  35. const float SHIFT_LEFT12 = 4096.0;
  36. const float SHIFT_LEFT8 = 256.0;
  37. const float SHIFT_LEFT7 = 128.0;
  38. const float SHIFT_LEFT5 = 32.0;
  39. const float SHIFT_LEFT3 = 8.0;
  40. const float SHIFT_LEFT2 = 4.0;
  41. const float SHIFT_LEFT1 = 2.0;
  42. const float SHIFT_RIGHT12 = 1.0 / 4096.0;
  43. const float SHIFT_RIGHT8 = 1.0 / 256.0;
  44. const float SHIFT_RIGHT7 = 1.0 / 128.0;
  45. const float SHIFT_RIGHT5 = 1.0 / 32.0;
  46. const float SHIFT_RIGHT3 = 1.0 / 8.0;
  47. const float SHIFT_RIGHT2 = 1.0 / 4.0;
  48. const float SHIFT_RIGHT1 = 1.0 / 2.0;
  49. vec4 addScreenSpaceOffset(vec4 positionEC, vec2 imageSize, float scale, vec2 direction, vec2 origin, vec2 translate, vec2 pixelOffset, vec3 alignedAxis, bool validAlignedAxis, float rotation, bool sizeInMeters, out mat2 rotationMatrix, out float mpp)
  50. {
  51. // Note the halfSize cannot be computed in JavaScript because it is sent via
  52. // compressed vertex attributes that coerce it to an integer.
  53. vec2 halfSize = imageSize * scale * 0.5;
  54. halfSize *= ((direction * 2.0) - 1.0);
  55. vec2 originTranslate = origin * abs(halfSize);
  56. #if defined(ROTATION) || defined(ALIGNED_AXIS)
  57. if (validAlignedAxis || rotation != 0.0)
  58. {
  59. float angle = rotation;
  60. if (validAlignedAxis)
  61. {
  62. vec4 projectedAlignedAxis = czm_modelView3D * vec4(alignedAxis, 0.0);
  63. angle += sign(-projectedAlignedAxis.x) * acos(sign(projectedAlignedAxis.y) * (projectedAlignedAxis.y * projectedAlignedAxis.y) /
  64. (projectedAlignedAxis.x * projectedAlignedAxis.x + projectedAlignedAxis.y * projectedAlignedAxis.y));
  65. }
  66. float cosTheta = cos(angle);
  67. float sinTheta = sin(angle);
  68. rotationMatrix = mat2(cosTheta, sinTheta, -sinTheta, cosTheta);
  69. halfSize = rotationMatrix * halfSize;
  70. }
  71. else
  72. {
  73. rotationMatrix = mat2(1.0, 0.0, 0.0, 1.0);
  74. }
  75. #endif
  76. mpp = czm_metersPerPixel(positionEC);
  77. positionEC.xy += (originTranslate + halfSize) * czm_branchFreeTernary(sizeInMeters, 1.0, mpp);
  78. positionEC.xy += (translate + pixelOffset) * mpp;
  79. return positionEC;
  80. }
  81. #ifdef VERTEX_DEPTH_CHECK
  82. float getGlobeDepth(vec4 positionEC)
  83. {
  84. vec4 posWC = czm_eyeToWindowCoordinates(positionEC);
  85. float globeDepth = czm_unpackDepth(texture(czm_globeDepthTexture, posWC.xy / czm_viewport.zw));
  86. if (globeDepth == 0.0)
  87. {
  88. return 0.0; // not on the globe
  89. }
  90. vec4 eyeCoordinate = czm_windowToEyeCoordinates(posWC.xy, globeDepth);
  91. return eyeCoordinate.z / eyeCoordinate.w;
  92. }
  93. #endif
  94. void main()
  95. {
  96. // Modifying this shader may also require modifications to Billboard._computeScreenSpacePosition
  97. // unpack attributes
  98. vec3 positionHigh = positionHighAndScale.xyz;
  99. vec3 positionLow = positionLowAndRotation.xyz;
  100. float scale = positionHighAndScale.w;
  101. #if defined(ROTATION) || defined(ALIGNED_AXIS)
  102. float rotation = positionLowAndRotation.w;
  103. #else
  104. float rotation = 0.0;
  105. #endif
  106. float compressed = compressedAttribute0.x;
  107. vec2 pixelOffset;
  108. pixelOffset.x = floor(compressed * SHIFT_RIGHT7);
  109. compressed -= pixelOffset.x * SHIFT_LEFT7;
  110. pixelOffset.x -= UPPER_BOUND;
  111. vec2 origin;
  112. origin.x = floor(compressed * SHIFT_RIGHT5);
  113. compressed -= origin.x * SHIFT_LEFT5;
  114. origin.y = floor(compressed * SHIFT_RIGHT3);
  115. compressed -= origin.y * SHIFT_LEFT3;
  116. #ifdef FRAGMENT_DEPTH_CHECK
  117. vec2 depthOrigin = origin.xy;
  118. #endif
  119. origin -= vec2(1.0);
  120. float show = floor(compressed * SHIFT_RIGHT2);
  121. compressed -= show * SHIFT_LEFT2;
  122. #ifdef INSTANCED
  123. vec2 textureCoordinatesBottomLeft = czm_decompressTextureCoordinates(compressedAttribute0.w);
  124. vec2 textureCoordinatesRange = czm_decompressTextureCoordinates(eyeOffset.w);
  125. vec2 textureCoordinates = textureCoordinatesBottomLeft + direction * textureCoordinatesRange;
  126. #else
  127. vec2 direction;
  128. direction.x = floor(compressed * SHIFT_RIGHT1);
  129. direction.y = compressed - direction.x * SHIFT_LEFT1;
  130. vec2 textureCoordinates = czm_decompressTextureCoordinates(compressedAttribute0.w);
  131. #endif
  132. float temp = compressedAttribute0.y * SHIFT_RIGHT8;
  133. pixelOffset.y = -(floor(temp) - UPPER_BOUND);
  134. vec2 translate;
  135. translate.y = (temp - floor(temp)) * SHIFT_LEFT16;
  136. temp = compressedAttribute0.z * SHIFT_RIGHT8;
  137. translate.x = floor(temp) - UPPER_BOUND;
  138. translate.y += (temp - floor(temp)) * SHIFT_LEFT8;
  139. translate.y -= UPPER_BOUND;
  140. temp = compressedAttribute1.x * SHIFT_RIGHT8;
  141. float temp2 = floor(compressedAttribute2.w * SHIFT_RIGHT2);
  142. vec2 imageSize = vec2(floor(temp), temp2);
  143. #ifdef FRAGMENT_DEPTH_CHECK
  144. float labelHorizontalOrigin = floor(compressedAttribute2.w - (temp2 * SHIFT_LEFT2));
  145. float applyTranslate = 0.0;
  146. if (labelHorizontalOrigin != 0.0) // is a billboard, so set apply translate to false
  147. {
  148. applyTranslate = 1.0;
  149. labelHorizontalOrigin -= 2.0;
  150. depthOrigin.x = labelHorizontalOrigin + 1.0;
  151. }
  152. depthOrigin = vec2(1.0) - (depthOrigin * 0.5);
  153. #endif
  154. #ifdef EYE_DISTANCE_TRANSLUCENCY
  155. vec4 translucencyByDistance;
  156. translucencyByDistance.x = compressedAttribute1.z;
  157. translucencyByDistance.z = compressedAttribute1.w;
  158. translucencyByDistance.y = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;
  159. temp = compressedAttribute1.y * SHIFT_RIGHT8;
  160. translucencyByDistance.w = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;
  161. #endif
  162. #if defined(VERTEX_DEPTH_CHECK) || defined(FRAGMENT_DEPTH_CHECK)
  163. temp = compressedAttribute3.w;
  164. temp = temp * SHIFT_RIGHT12;
  165. vec2 dimensions;
  166. dimensions.y = (temp - floor(temp)) * SHIFT_LEFT12;
  167. dimensions.x = floor(temp);
  168. #endif
  169. #ifdef ALIGNED_AXIS
  170. vec3 alignedAxis = czm_octDecode(floor(compressedAttribute1.y * SHIFT_RIGHT8));
  171. temp = compressedAttribute2.z * SHIFT_RIGHT5;
  172. bool validAlignedAxis = (temp - floor(temp)) * SHIFT_LEFT1 > 0.0;
  173. #else
  174. vec3 alignedAxis = vec3(0.0);
  175. bool validAlignedAxis = false;
  176. #endif
  177. vec4 pickColor;
  178. vec4 color;
  179. temp = compressedAttribute2.y;
  180. temp = temp * SHIFT_RIGHT8;
  181. pickColor.b = (temp - floor(temp)) * SHIFT_LEFT8;
  182. temp = floor(temp) * SHIFT_RIGHT8;
  183. pickColor.g = (temp - floor(temp)) * SHIFT_LEFT8;
  184. pickColor.r = floor(temp);
  185. temp = compressedAttribute2.x;
  186. temp = temp * SHIFT_RIGHT8;
  187. color.b = (temp - floor(temp)) * SHIFT_LEFT8;
  188. temp = floor(temp) * SHIFT_RIGHT8;
  189. color.g = (temp - floor(temp)) * SHIFT_LEFT8;
  190. color.r = floor(temp);
  191. temp = compressedAttribute2.z * SHIFT_RIGHT8;
  192. bool sizeInMeters = floor((temp - floor(temp)) * SHIFT_LEFT7) > 0.0;
  193. temp = floor(temp) * SHIFT_RIGHT8;
  194. pickColor.a = (temp - floor(temp)) * SHIFT_LEFT8;
  195. pickColor /= 255.0;
  196. color.a = floor(temp);
  197. color /= 255.0;
  198. ///////////////////////////////////////////////////////////////////////////
  199. vec4 p = czm_translateRelativeToEye(positionHigh, positionLow);
  200. vec4 positionEC = czm_modelViewRelativeToEye * p;
  201. #if defined(FRAGMENT_DEPTH_CHECK) || defined(VERTEX_DEPTH_CHECK)
  202. float eyeDepth = positionEC.z;
  203. #endif
  204. positionEC = czm_eyeOffset(positionEC, eyeOffset.xyz);
  205. positionEC.xyz *= show;
  206. ///////////////////////////////////////////////////////////////////////////
  207. #if defined(EYE_DISTANCE_SCALING) || defined(EYE_DISTANCE_TRANSLUCENCY) || defined(EYE_DISTANCE_PIXEL_OFFSET) || defined(DISTANCE_DISPLAY_CONDITION) || defined(DISABLE_DEPTH_DISTANCE)
  208. float lengthSq;
  209. if (czm_sceneMode == czm_sceneMode2D)
  210. {
  211. // 2D camera distance is a special case
  212. // treat all billboards as flattened to the z=0.0 plane
  213. lengthSq = czm_eyeHeight2D.y;
  214. }
  215. else
  216. {
  217. lengthSq = dot(positionEC.xyz, positionEC.xyz);
  218. }
  219. #endif
  220. #ifdef EYE_DISTANCE_SCALING
  221. float distanceScale = czm_nearFarScalar(scaleByDistance, lengthSq);
  222. scale *= distanceScale;
  223. translate *= distanceScale;
  224. // push vertex behind near plane for clipping
  225. if (scale == 0.0)
  226. {
  227. positionEC.xyz = vec3(0.0);
  228. }
  229. #endif
  230. float translucency = 1.0;
  231. #ifdef EYE_DISTANCE_TRANSLUCENCY
  232. translucency = czm_nearFarScalar(translucencyByDistance, lengthSq);
  233. // push vertex behind near plane for clipping
  234. if (translucency == 0.0)
  235. {
  236. positionEC.xyz = vec3(0.0);
  237. }
  238. #endif
  239. #ifdef EYE_DISTANCE_PIXEL_OFFSET
  240. float pixelOffsetScale = czm_nearFarScalar(pixelOffsetScaleByDistance, lengthSq);
  241. pixelOffset *= pixelOffsetScale;
  242. #endif
  243. #ifdef DISTANCE_DISPLAY_CONDITION
  244. float nearSq = compressedAttribute3.x;
  245. float farSq = compressedAttribute3.y;
  246. if (lengthSq < nearSq || lengthSq > farSq)
  247. {
  248. positionEC.xyz = vec3(0.0);
  249. }
  250. #endif
  251. mat2 rotationMatrix;
  252. float mpp;
  253. #ifdef DISABLE_DEPTH_DISTANCE
  254. float disableDepthTestDistance = compressedAttribute3.z;
  255. #endif
  256. #ifdef VERTEX_DEPTH_CHECK
  257. if (lengthSq < disableDepthTestDistance) {
  258. float depthsilon = 10.0;
  259. vec2 labelTranslate = textureCoordinateBoundsOrLabelTranslate.xy;
  260. vec4 pEC1 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(0.0), origin, labelTranslate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  261. float globeDepth1 = getGlobeDepth(pEC1);
  262. if (globeDepth1 != 0.0 && pEC1.z + depthsilon < globeDepth1)
  263. {
  264. vec4 pEC2 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(0.0, 1.0), origin, labelTranslate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  265. float globeDepth2 = getGlobeDepth(pEC2);
  266. if (globeDepth2 != 0.0 && pEC2.z + depthsilon < globeDepth2)
  267. {
  268. vec4 pEC3 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(1.0), origin, labelTranslate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  269. float globeDepth3 = getGlobeDepth(pEC3);
  270. if (globeDepth3 != 0.0 && pEC3.z + depthsilon < globeDepth3)
  271. {
  272. positionEC.xyz = vec3(0.0);
  273. }
  274. }
  275. }
  276. }
  277. #endif
  278. positionEC = addScreenSpaceOffset(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  279. gl_Position = czm_projection * positionEC;
  280. v_textureCoordinates = textureCoordinates;
  281. #ifdef LOG_DEPTH
  282. czm_vertexLogDepth();
  283. #endif
  284. #ifdef DISABLE_DEPTH_DISTANCE
  285. if (disableDepthTestDistance == 0.0 && czm_minimumDisableDepthTestDistance != 0.0)
  286. {
  287. disableDepthTestDistance = czm_minimumDisableDepthTestDistance;
  288. }
  289. if (disableDepthTestDistance != 0.0)
  290. {
  291. // Don't try to "multiply both sides" by w. Greater/less-than comparisons won't work for negative values of w.
  292. float zclip = gl_Position.z / gl_Position.w;
  293. bool clipped = (zclip < -1.0 || zclip > 1.0);
  294. if (!clipped && (disableDepthTestDistance < 0.0 || (lengthSq > 0.0 && lengthSq < disableDepthTestDistance)))
  295. {
  296. // Position z on the near plane.
  297. gl_Position.z = -gl_Position.w;
  298. #ifdef LOG_DEPTH
  299. v_depthFromNearPlusOne = 1.0;
  300. #endif
  301. }
  302. }
  303. #endif
  304. #ifdef FRAGMENT_DEPTH_CHECK
  305. if (sizeInMeters) {
  306. translate /= mpp;
  307. dimensions /= mpp;
  308. imageSize /= mpp;
  309. }
  310. #if defined(ROTATION) || defined(ALIGNED_AXIS)
  311. v_rotationMatrix = rotationMatrix;
  312. #else
  313. v_rotationMatrix = mat2(1.0, 0.0, 0.0, 1.0);
  314. #endif
  315. float enableDepthCheck = 0.0;
  316. if (lengthSq < disableDepthTestDistance)
  317. {
  318. enableDepthCheck = 1.0;
  319. }
  320. float dw = floor(clamp(dimensions.x, 0.0, SHIFT_LEFT12));
  321. float dh = floor(clamp(dimensions.y, 0.0, SHIFT_LEFT12));
  322. float iw = floor(clamp(imageSize.x, 0.0, SHIFT_LEFT12));
  323. float ih = floor(clamp(imageSize.y, 0.0, SHIFT_LEFT12));
  324. v_compressed.x = eyeDepth;
  325. v_compressed.y = applyTranslate * SHIFT_LEFT1 + enableDepthCheck;
  326. v_compressed.z = dw * SHIFT_LEFT12 + dh;
  327. v_compressed.w = iw * SHIFT_LEFT12 + ih;
  328. v_originTextureCoordinateAndTranslate.xy = depthOrigin;
  329. v_originTextureCoordinateAndTranslate.zw = translate;
  330. v_textureCoordinateBounds = textureCoordinateBoundsOrLabelTranslate;
  331. #endif
  332. #ifdef SDF
  333. vec4 outlineColor;
  334. float outlineWidth;
  335. temp = sdf.x;
  336. temp = temp * SHIFT_RIGHT8;
  337. outlineColor.b = (temp - floor(temp)) * SHIFT_LEFT8;
  338. temp = floor(temp) * SHIFT_RIGHT8;
  339. outlineColor.g = (temp - floor(temp)) * SHIFT_LEFT8;
  340. outlineColor.r = floor(temp);
  341. temp = sdf.y;
  342. temp = temp * SHIFT_RIGHT8;
  343. float temp3 = (temp - floor(temp)) * SHIFT_LEFT8;
  344. temp = floor(temp) * SHIFT_RIGHT8;
  345. outlineWidth = (temp - floor(temp)) * SHIFT_LEFT8;
  346. outlineColor.a = floor(temp);
  347. outlineColor /= 255.0;
  348. v_outlineWidth = outlineWidth / 255.0;
  349. v_outlineColor = outlineColor;
  350. v_outlineColor.a *= translucency;
  351. #endif
  352. v_pickColor = pickColor;
  353. v_color = color;
  354. v_color.a *= translucency;
  355. }