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

BillboardCollectionVS.glsl 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. uniform float u_threePointDepthTestDistance;
  2. in vec2 direction;
  3. in vec4 positionHighAndScale;
  4. in vec4 positionLowAndRotation;
  5. in vec4 compressedAttribute0; // pixel offset, translate, horizontal origin, vertical origin, show, direction, texture coordinates (texture offset)
  6. in vec4 compressedAttribute1; // aligned axis, translucency by distance, image width
  7. in vec4 compressedAttribute2; // label horizontal origin, image height, color, pick color, size in meters, valid aligned axis, 13 bits free
  8. in vec4 eyeOffset; // eye offset in meters, 4 bytes free (texture range)
  9. in vec4 scaleByDistance; // near, nearScale, far, farScale
  10. in vec4 pixelOffsetScaleByDistance; // near, nearScale, far, farScale
  11. in vec4 compressedAttribute3; // distance display condition near, far, disableDepthTestDistanceSq, dimensions
  12. in vec2 sdf; // sdf outline color (rgb) and width (w)
  13. in float splitDirection; // splitDirection
  14. #ifdef VS_THREE_POINT_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. out vec4 v_compressed; // x: eyeDepth, y: applyTranslate & enableDepthCheck, z: dimensions, w: imageSize
  22. out vec4 v_pickColor;
  23. out vec4 v_color;
  24. flat out vec2 v_splitDirectionAndEllipsoidDepthEC; // x: splitDirection, y: ellipsoid depth in eye coordinates
  25. #ifdef SDF
  26. out vec4 v_outlineColor;
  27. out float v_outlineWidth;
  28. #endif
  29. const float UPPER_BOUND = 32768.0;
  30. const float SHIFT_LEFT16 = 65536.0;
  31. const float SHIFT_LEFT12 = 4096.0;
  32. const float SHIFT_LEFT8 = 256.0;
  33. const float SHIFT_LEFT7 = 128.0;
  34. const float SHIFT_LEFT5 = 32.0;
  35. const float SHIFT_LEFT3 = 8.0;
  36. const float SHIFT_LEFT2 = 4.0;
  37. const float SHIFT_LEFT1 = 2.0;
  38. const float SHIFT_RIGHT12 = 1.0 / 4096.0;
  39. const float SHIFT_RIGHT8 = 1.0 / 256.0;
  40. const float SHIFT_RIGHT7 = 1.0 / 128.0;
  41. const float SHIFT_RIGHT5 = 1.0 / 32.0;
  42. const float SHIFT_RIGHT3 = 1.0 / 8.0;
  43. const float SHIFT_RIGHT2 = 1.0 / 4.0;
  44. const float SHIFT_RIGHT1 = 1.0 / 2.0;
  45. 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)
  46. {
  47. // Note the halfSize cannot be computed in JavaScript because it is sent via
  48. // compressed vertex attributes that coerce it to an integer.
  49. vec2 halfSize = imageSize * scale * 0.5;
  50. halfSize *= ((direction * 2.0) - 1.0);
  51. vec2 originTranslate = origin * abs(halfSize);
  52. #if defined(ROTATION) || defined(ALIGNED_AXIS)
  53. if (validAlignedAxis || rotation != 0.0)
  54. {
  55. float angle = rotation;
  56. if (validAlignedAxis)
  57. {
  58. vec4 projectedAlignedAxis = czm_modelView3D * vec4(alignedAxis, 0.0);
  59. angle += sign(-projectedAlignedAxis.x) * acos(sign(projectedAlignedAxis.y) * (projectedAlignedAxis.y * projectedAlignedAxis.y) /
  60. (projectedAlignedAxis.x * projectedAlignedAxis.x + projectedAlignedAxis.y * projectedAlignedAxis.y));
  61. }
  62. float cosTheta = cos(angle);
  63. float sinTheta = sin(angle);
  64. rotationMatrix = mat2(cosTheta, sinTheta, -sinTheta, cosTheta);
  65. halfSize = rotationMatrix * halfSize;
  66. }
  67. else
  68. {
  69. rotationMatrix = mat2(1.0, 0.0, 0.0, 1.0);
  70. }
  71. #endif
  72. mpp = czm_metersPerPixel(positionEC);
  73. positionEC.xy += (originTranslate + halfSize) * czm_branchFreeTernary(sizeInMeters, 1.0, mpp);
  74. positionEC.xy += (translate + pixelOffset) * mpp;
  75. return positionEC;
  76. }
  77. #ifdef VS_THREE_POINT_DEPTH_CHECK
  78. float getGlobeDepth(vec4 positionEC)
  79. {
  80. vec4 posWC = czm_eyeToWindowCoordinates(positionEC);
  81. float globeDepth = czm_unpackDepth(texture(czm_globeDepthTexture, posWC.xy / czm_viewport.zw));
  82. if (globeDepth == 0.0)
  83. {
  84. return 0.0; // not on the globe
  85. }
  86. vec4 eyeCoordinate = czm_windowToEyeCoordinates(posWC.xy, globeDepth);
  87. return eyeCoordinate.z / eyeCoordinate.w;
  88. }
  89. #endif
  90. void main()
  91. {
  92. // Modifying this shader may also require modifications to Billboard._computeScreenSpacePosition
  93. // unpack attributes
  94. vec3 positionHigh = positionHighAndScale.xyz;
  95. vec3 positionLow = positionLowAndRotation.xyz;
  96. float scale = positionHighAndScale.w;
  97. #if defined(ROTATION) || defined(ALIGNED_AXIS)
  98. float rotation = positionLowAndRotation.w;
  99. #else
  100. float rotation = 0.0;
  101. #endif
  102. float compressed = compressedAttribute0.x;
  103. vec2 pixelOffset;
  104. pixelOffset.x = floor(compressed * SHIFT_RIGHT7);
  105. compressed -= pixelOffset.x * SHIFT_LEFT7;
  106. pixelOffset.x -= UPPER_BOUND;
  107. vec2 origin;
  108. origin.x = floor(compressed * SHIFT_RIGHT5);
  109. compressed -= origin.x * SHIFT_LEFT5;
  110. origin.y = floor(compressed * SHIFT_RIGHT3);
  111. compressed -= origin.y * SHIFT_LEFT3;
  112. origin -= vec2(1.0);
  113. float show = floor(compressed * SHIFT_RIGHT2);
  114. compressed -= show * SHIFT_LEFT2;
  115. vec2 textureCoordinatesBottomLeft = czm_decompressTextureCoordinates(compressedAttribute0.w);
  116. vec2 textureCoordinatesRange = czm_decompressTextureCoordinates(eyeOffset.w);
  117. vec2 textureCoordinates = textureCoordinatesBottomLeft + direction * textureCoordinatesRange;
  118. float temp = compressedAttribute0.y * SHIFT_RIGHT8;
  119. pixelOffset.y = -(floor(temp) - UPPER_BOUND);
  120. vec2 translate;
  121. translate.y = (temp - floor(temp)) * SHIFT_LEFT16;
  122. temp = compressedAttribute0.z * SHIFT_RIGHT8;
  123. translate.x = floor(temp) - UPPER_BOUND;
  124. translate.x *= SHIFT_RIGHT2; // undo translateX scaling (helps preserve subpixel precision, see BillboardCollection.js attribute writer for more info)
  125. translate.y += (temp - floor(temp)) * SHIFT_LEFT8;
  126. translate.y -= UPPER_BOUND;
  127. translate.y *= SHIFT_RIGHT2;
  128. temp = compressedAttribute1.x * SHIFT_RIGHT8;
  129. float temp2 = floor(compressedAttribute2.w * SHIFT_RIGHT2);
  130. vec2 imageSize = vec2(floor(temp), temp2);
  131. #ifdef EYE_DISTANCE_TRANSLUCENCY
  132. vec4 translucencyByDistance;
  133. translucencyByDistance.x = compressedAttribute1.z;
  134. translucencyByDistance.z = compressedAttribute1.w;
  135. translucencyByDistance.y = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;
  136. temp = compressedAttribute1.y * SHIFT_RIGHT8;
  137. translucencyByDistance.w = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;
  138. #endif
  139. #ifdef VS_THREE_POINT_DEPTH_CHECK
  140. temp = compressedAttribute3.w;
  141. temp = temp * SHIFT_RIGHT12;
  142. vec2 dimensions;
  143. dimensions.y = (temp - floor(temp)) * SHIFT_LEFT12;
  144. dimensions.x = floor(temp);
  145. #endif
  146. #ifdef ALIGNED_AXIS
  147. vec3 alignedAxis = czm_octDecode(floor(compressedAttribute1.y * SHIFT_RIGHT8));
  148. temp = compressedAttribute2.z * SHIFT_RIGHT5;
  149. bool validAlignedAxis = (temp - floor(temp)) * SHIFT_LEFT1 > 0.0;
  150. #else
  151. vec3 alignedAxis = vec3(0.0);
  152. bool validAlignedAxis = false;
  153. #endif
  154. vec4 color = czm_decodeRGB8(compressedAttribute2.x);
  155. vec4 pickColor = czm_decodeRGB8(compressedAttribute2.y);
  156. temp = compressedAttribute2.z * SHIFT_RIGHT8;
  157. bool sizeInMeters = floor((temp - floor(temp)) * SHIFT_LEFT7) > 0.0;
  158. temp = floor(temp) * SHIFT_RIGHT8;
  159. pickColor.a = (temp - floor(temp)) * SHIFT_LEFT8;
  160. pickColor.a /= 255.0;
  161. color.a = floor(temp);
  162. color.a /= 255.0;
  163. ///////////////////////////////////////////////////////////////////////////
  164. vec4 p = czm_translateRelativeToEye(positionHigh, positionLow);
  165. vec4 positionEC = czm_modelViewRelativeToEye * p;
  166. positionEC = czm_eyeOffset(positionEC, eyeOffset.xyz);
  167. positionEC.xyz *= show;
  168. ///////////////////////////////////////////////////////////////////////////
  169. #if defined(EYE_DISTANCE_SCALING) || defined(EYE_DISTANCE_TRANSLUCENCY) || defined(EYE_DISTANCE_PIXEL_OFFSET) || defined(DISTANCE_DISPLAY_CONDITION) || defined(DISABLE_DEPTH_DISTANCE)
  170. float lengthSq;
  171. if (czm_sceneMode == czm_sceneMode2D)
  172. {
  173. // 2D camera distance is a special case
  174. // treat all billboards as flattened to the z=0.0 plane
  175. lengthSq = czm_eyeHeight2D.y;
  176. }
  177. else
  178. {
  179. lengthSq = dot(positionEC.xyz, positionEC.xyz);
  180. }
  181. #endif
  182. #ifdef EYE_DISTANCE_SCALING
  183. float distanceScale = czm_nearFarScalar(scaleByDistance, lengthSq);
  184. scale *= distanceScale;
  185. translate *= distanceScale;
  186. // push vertex behind near plane for clipping
  187. if (scale == 0.0)
  188. {
  189. positionEC.xyz = vec3(0.0);
  190. }
  191. #endif
  192. float translucency = 1.0;
  193. #ifdef EYE_DISTANCE_TRANSLUCENCY
  194. translucency = czm_nearFarScalar(translucencyByDistance, lengthSq);
  195. // push vertex behind near plane for clipping
  196. if (translucency == 0.0)
  197. {
  198. positionEC.xyz = vec3(0.0);
  199. }
  200. #endif
  201. #ifdef EYE_DISTANCE_PIXEL_OFFSET
  202. float pixelOffsetScale = czm_nearFarScalar(pixelOffsetScaleByDistance, lengthSq);
  203. pixelOffset *= pixelOffsetScale;
  204. #endif
  205. #ifdef DISTANCE_DISPLAY_CONDITION
  206. float nearSq = compressedAttribute3.x;
  207. float farSq = compressedAttribute3.y;
  208. if (lengthSq < nearSq || lengthSq > farSq)
  209. {
  210. positionEC.xyz = vec3(0.0);
  211. }
  212. #endif
  213. mat2 rotationMatrix;
  214. float mpp;
  215. float enableDepthCheck = 1.0;
  216. #ifdef DISABLE_DEPTH_DISTANCE
  217. float disableDepthTestDistanceSq = compressedAttribute3.z;
  218. if (disableDepthTestDistanceSq == 0.0 && czm_minimumDisableDepthTestDistance != 0.0)
  219. {
  220. disableDepthTestDistanceSq = czm_minimumDisableDepthTestDistance;
  221. }
  222. if (lengthSq < disableDepthTestDistanceSq || disableDepthTestDistanceSq < 0.0)
  223. {
  224. enableDepthCheck = 0.0;
  225. }
  226. #endif
  227. v_splitDirectionAndEllipsoidDepthEC.y = czm_infinity;
  228. vec3 ellipsoidCenter = czm_view[3].xyz;
  229. vec3 rayDirection = normalize(positionEC.xyz);
  230. czm_ray ray = czm_ray(vec3(0.0), rayDirection);
  231. vec3 ellipsoid_inverseRadii = czm_ellipsoidInverseRadii;
  232. czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoidCenter, ellipsoid_inverseRadii);
  233. if (!czm_isEmpty(intersection))
  234. {
  235. v_splitDirectionAndEllipsoidDepthEC.y = intersection.start;
  236. }
  237. v_compressed.y = enableDepthCheck;
  238. #ifdef VS_THREE_POINT_DEPTH_CHECK
  239. if (lengthSq < (u_threePointDepthTestDistance * u_threePointDepthTestDistance) && (enableDepthCheck == 1.0)) {
  240. float depthsilon = 10.0;
  241. vec2 depthOrigin;
  242. // Horizontal origin for labels comes from a special attribute. If that value is 0, this is a billboard - use the regular origin.
  243. // Otherwise, transform the label origin to -1, 0, 1 (right, center, left).
  244. depthOrigin.x = floor(compressedAttribute2.w - (temp2 * SHIFT_LEFT2));
  245. depthOrigin.x = czm_branchFreeTernary(depthOrigin.x == 0.0, origin.x, depthOrigin.x - 2.0);
  246. depthOrigin.y = origin.y;
  247. vec4 pEC1 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(0.0), depthOrigin, vec2(0.0), pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  248. float globeDepth1 = getGlobeDepth(pEC1);
  249. if (globeDepth1 != 0.0 && pEC1.z + depthsilon < globeDepth1)
  250. {
  251. vec4 pEC2 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(0.0, 1.0), depthOrigin, vec2(0.0), pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  252. float globeDepth2 = getGlobeDepth(pEC2);
  253. if (globeDepth2 != 0.0 && pEC2.z + depthsilon < globeDepth2)
  254. {
  255. vec4 pEC3 = addScreenSpaceOffset(positionEC, dimensions, scale, vec2(1.0), depthOrigin, vec2(0.0), pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  256. float globeDepth3 = getGlobeDepth(pEC3);
  257. if (globeDepth3 != 0.0 && pEC3.z + depthsilon < globeDepth3)
  258. {
  259. // "Discard" this vertex, as three key points fail depth test.
  260. positionEC.xyz = vec3(0.0);
  261. }
  262. }
  263. }
  264. }
  265. #endif
  266. // Write out the eyespace depth before applying the screen space offset, but after potentially "discarding" the vertex
  267. // by setting its eyespace position to zero, via the three-point depth test above.
  268. v_compressed.x = positionEC.z;
  269. positionEC = addScreenSpaceOffset(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters, rotationMatrix, mpp);
  270. gl_Position = czm_projection * positionEC;
  271. v_textureCoordinates = textureCoordinates;
  272. #ifdef LOG_DEPTH
  273. czm_vertexLogDepth();
  274. #endif
  275. #ifdef DISABLE_DEPTH_DISTANCE
  276. if (disableDepthTestDistanceSq != 0.0)
  277. {
  278. // Don't try to "multiply both sides" by w. Greater/less-than comparisons won't work for negative values of w.
  279. float zclip = gl_Position.z / gl_Position.w;
  280. bool clipped = (zclip < -1.0 || zclip > 1.0);
  281. // disableDepthTestDistanceSq can be less than zero if it's explicitly set to -1 in JS (as a sentinel value equivalent to infinity)
  282. if (!clipped && (disableDepthTestDistanceSq < 0.0 || (lengthSq > 0.0 && lengthSq < disableDepthTestDistanceSq)))
  283. {
  284. // Position z on the near plane.
  285. gl_Position.z = -gl_Position.w;
  286. #ifdef LOG_DEPTH
  287. v_depthFromNearPlusOne = 1.0;
  288. #endif
  289. }
  290. }
  291. #endif
  292. #ifdef SDF
  293. vec4 outlineColor = czm_decodeRGB8(sdf.x);
  294. float outlineWidth;
  295. temp = sdf.y;
  296. temp = temp * SHIFT_RIGHT8;
  297. float temp3 = (temp - floor(temp)) * SHIFT_LEFT8;
  298. temp = floor(temp) * SHIFT_RIGHT8;
  299. outlineWidth = (temp - floor(temp)) * SHIFT_LEFT8;
  300. outlineColor.a = floor(temp);
  301. outlineColor.a /= 255.0;
  302. v_outlineWidth = outlineWidth / 255.0;
  303. v_outlineColor = outlineColor;
  304. v_outlineColor.a *= translucency;
  305. #endif
  306. v_pickColor = pickColor;
  307. v_color = color;
  308. v_color.a *= translucency;
  309. v_splitDirectionAndEllipsoidDepthEC.x = splitDirection;
  310. }