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

PolygonSignedDistanceFS.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "in vec2 v_textureCoordinates;\n\
  3. \n\
  4. uniform int u_polygonsLength;\n\
  5. uniform int u_extentsLength;\n\
  6. uniform highp sampler2D u_polygonTexture;\n\
  7. uniform highp sampler2D u_extentsTexture;\n\
  8. \n\
  9. int getPolygonIndex(float dimension, vec2 coord) {\n\
  10. vec2 uv = coord.xy * dimension;\n\
  11. return int(floor(uv.y) * dimension + floor(uv.x));\n\
  12. }\n\
  13. \n\
  14. vec2 getLookupUv(ivec2 dimensions, int i) {\n\
  15. int pixY = i / dimensions.x;\n\
  16. int pixX = i - (pixY * dimensions.x);\n\
  17. float pixelWidth = 1.0 / float(dimensions.x);\n\
  18. float pixelHeight = 1.0 / float(dimensions.y);\n\
  19. float u = (float(pixX) + 0.5) * pixelWidth; // sample from center of pixel\n\
  20. float v = (float(pixY) + 0.5) * pixelHeight;\n\
  21. return vec2(u, v);\n\
  22. }\n\
  23. \n\
  24. vec4 getExtents(int i) {\n\
  25. return texture(u_extentsTexture, getLookupUv(textureSize(u_extentsTexture, 0), i));\n\
  26. }\n\
  27. \n\
  28. ivec2 getPositionsLengthAndExtentsIndex(int i) {\n\
  29. vec2 uv = getLookupUv(textureSize(u_polygonTexture, 0), i);\n\
  30. vec4 value = texture(u_polygonTexture, uv);\n\
  31. return ivec2(int(value.x), int(value.y));\n\
  32. }\n\
  33. \n\
  34. vec2 getPolygonPosition(int i) {\n\
  35. vec2 uv = getLookupUv(textureSize(u_polygonTexture, 0), i);\n\
  36. return texture(u_polygonTexture, uv).xy;\n\
  37. }\n\
  38. \n\
  39. vec2 getCoordinates(vec2 textureCoordinates, vec4 extents) {\n\
  40. float latitude = mix(extents.x, extents.x + 1.0 / extents.z, textureCoordinates.y);\n\
  41. float longitude = mix(extents.y, extents.y + 1.0 / extents.w, textureCoordinates.x);\n\
  42. return vec2(latitude, longitude);\n\
  43. }\n\
  44. \n\
  45. void main() {\n\
  46. int lastPolygonIndex = 0;\n\
  47. out_FragColor = vec4(1.0);\n\
  48. \n\
  49. // Get the relevant region of the texture\n\
  50. float dimension = float(u_extentsLength);\n\
  51. if (u_extentsLength > 2) {\n\
  52. dimension = ceil(log2(float(u_extentsLength)));\n\
  53. }\n\
  54. int regionIndex = getPolygonIndex(dimension, v_textureCoordinates);\n\
  55. \n\
  56. if (regionIndex >= u_extentsLength) {\n\
  57. return; // done (no polygons in this region)\n\
  58. }\n\
  59. \n\
  60. for (int polygonIndex = 0; polygonIndex < u_polygonsLength; polygonIndex++) {\n\
  61. ivec2 positionsLengthAndExtents = getPositionsLengthAndExtentsIndex(lastPolygonIndex);\n\
  62. int positionsLength = positionsLengthAndExtents.x;\n\
  63. int polygonExtentsIndex = positionsLengthAndExtents.y;\n\
  64. lastPolygonIndex += 1;\n\
  65. \n\
  66. // Read the individual polygon extent (2 pixels: south/west, latRange/lonRange)\n\
  67. vec2 extentsSouthWest = getPolygonPosition(lastPolygonIndex);\n\
  68. vec2 extentsRange = getPolygonPosition(lastPolygonIndex + 1);\n\
  69. vec4 polygonExtent = vec4(extentsSouthWest, extentsRange);\n\
  70. lastPolygonIndex += 2;\n\
  71. \n\
  72. if (polygonExtentsIndex < regionIndex) {\n\
  73. lastPolygonIndex += positionsLength;\n\
  74. continue; // skip to next (TODO: could optimize further if we knew how many polygons to skip)\n\
  75. } else if (polygonExtentsIndex > regionIndex) {\n\
  76. break; // done (we know polygons are sorted by regionIndex)\n\
  77. }\n\
  78. \n\
  79. // Only compute signed distance for the relevant part of the atlas\n\
  80. float clipAmount = czm_infinity;\n\
  81. vec4 extents = getExtents(polygonExtentsIndex);\n\
  82. vec2 textureOffset = vec2(mod(float(polygonExtentsIndex), dimension), floor(float(polygonExtentsIndex) / dimension)) / dimension;\n\
  83. vec2 p = getCoordinates((v_textureCoordinates - textureOffset) * dimension, extents); // current pixel position\n\
  84. \n\
  85. // Only consider polygons whos boundingbox includes current pixel (with a slight padding)\n\
  86. float padding = 0.05; // 5% of polygon extents\n\
  87. float polygonNorth = polygonExtent.x + polygonExtent.z;\n\
  88. float polygonEast = polygonExtent.y + polygonExtent.w;\n\
  89. float latPadding = padding * polygonExtent.z; // padding as fraction of latitude range\n\
  90. float lonPadding = padding * polygonExtent.w; // padding as fraction of longitude range\n\
  91. if (p.x < polygonExtent.x - latPadding || p.x > polygonNorth + latPadding ||\n\
  92. p.y < polygonExtent.y - lonPadding || p.y > polygonEast + lonPadding) {\n\
  93. lastPolygonIndex += positionsLength;\n\
  94. continue; // skip to next\n\
  95. }\n\
  96. \n\
  97. float s = 1.0;\n\
  98. \n\
  99. // Check each edge for absolute distance.\n\
  100. // Cache the previous vertex to halve the texture reads per iteration.\n\
  101. vec2 prev = getPolygonPosition(lastPolygonIndex + positionsLength - 1);\n\
  102. for (int i = 0; i < positionsLength; i++) {\n\
  103. vec2 a = getPolygonPosition(lastPolygonIndex + i);\n\
  104. vec2 b = prev;\n\
  105. prev = a;\n\
  106. \n\
  107. vec2 ab = b - a;\n\
  108. vec2 pa = p - a;\n\
  109. float t = dot(pa, ab) / dot(ab, ab);\n\
  110. t = clamp(t, 0.0, 1.0);\n\
  111. \n\
  112. vec2 pq = pa - t * ab;\n\
  113. float d = length(pq);\n\
  114. \n\
  115. // Inside / outside computation to determine sign\n\
  116. bvec3 cond = bvec3(p.y >= a.y, \n\
  117. p.y < b.y, \n\
  118. ab.x * pa.y > ab.y * pa.x);\n\
  119. if (all(cond) || all(not(cond))) s = -s;\n\
  120. if (abs(d) < abs(clipAmount)) {\n\
  121. clipAmount = d;\n\
  122. }\n\
  123. }\n\
  124. \n\
  125. // Normalize the range to [0,1]\n\
  126. vec4 result = (s * vec4(clipAmount * length(extents.zw))) / 2.0 + 0.5;\n\
  127. // In the case where we've iterated through multiple polygons, take the minimum\n\
  128. out_FragColor = min(out_FragColor, result);\n\
  129. \n\
  130. lastPolygonIndex += positionsLength;\n\
  131. }\n\
  132. }";