PolylineCommon.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //This file is automatically rebuilt by the Cesium build process.
  2. /*global define*/
  3. define(function() {
  4. "use strict";
  5. return "void clipLineSegmentToNearPlane(\n\
  6. vec3 p0,\n\
  7. vec3 p1,\n\
  8. out vec4 positionWC,\n\
  9. out bool clipped,\n\
  10. out bool culledByNearPlane)\n\
  11. {\n\
  12. culledByNearPlane = false;\n\
  13. clipped = false;\n\
  14. \n\
  15. vec3 p1ToP0 = p1 - p0;\n\
  16. float magnitude = length(p1ToP0);\n\
  17. vec3 direction = normalize(p1ToP0);\n\
  18. float endPoint0Distance = -(czm_currentFrustum.x + p0.z);\n\
  19. float denominator = -direction.z;\n\
  20. \n\
  21. if (endPoint0Distance < 0.0 && abs(denominator) < czm_epsilon7)\n\
  22. {\n\
  23. culledByNearPlane = true;\n\
  24. }\n\
  25. else if (endPoint0Distance < 0.0 && abs(denominator) > czm_epsilon7)\n\
  26. {\n\
  27. // t = (-plane distance - dot(plane normal, ray origin)) / dot(plane normal, ray direction)\n\
  28. float t = (czm_currentFrustum.x + p0.z) / denominator;\n\
  29. if (t < 0.0 || t > magnitude)\n\
  30. {\n\
  31. culledByNearPlane = true;\n\
  32. }\n\
  33. else\n\
  34. {\n\
  35. p0 = p0 + t * direction;\n\
  36. clipped = true;\n\
  37. }\n\
  38. }\n\
  39. \n\
  40. positionWC = czm_eyeToWindowCoordinates(vec4(p0, 1.0));\n\
  41. }\n\
  42. \n\
  43. vec4 getPolylineWindowCoordinates(vec4 position, vec4 previous, vec4 next, float expandDirection, float width, bool usePrevious) {\n\
  44. vec4 endPointWC, p0, p1;\n\
  45. bool culledByNearPlane, clipped;\n\
  46. \n\
  47. vec4 positionEC = czm_modelViewRelativeToEye * position;\n\
  48. vec4 prevEC = czm_modelViewRelativeToEye * previous;\n\
  49. vec4 nextEC = czm_modelViewRelativeToEye * next;\n\
  50. \n\
  51. clipLineSegmentToNearPlane(prevEC.xyz, positionEC.xyz, p0, clipped, culledByNearPlane);\n\
  52. clipLineSegmentToNearPlane(nextEC.xyz, positionEC.xyz, p1, clipped, culledByNearPlane);\n\
  53. clipLineSegmentToNearPlane(positionEC.xyz, usePrevious ? prevEC.xyz : nextEC.xyz, endPointWC, clipped, culledByNearPlane);\n\
  54. \n\
  55. if (culledByNearPlane)\n\
  56. {\n\
  57. return vec4(0.0, 0.0, 0.0, 1.0);\n\
  58. }\n\
  59. \n\
  60. vec2 prevWC = normalize(p0.xy - endPointWC.xy);\n\
  61. vec2 nextWC = normalize(p1.xy - endPointWC.xy);\n\
  62. \n\
  63. float expandWidth = width * 0.5;\n\
  64. vec2 direction;\n\
  65. \n\
  66. if (czm_equalsEpsilon(normalize(previous.xyz - position.xyz), vec3(0.0), czm_epsilon1) || czm_equalsEpsilon(prevWC, -nextWC, czm_epsilon1))\n\
  67. {\n\
  68. direction = vec2(-nextWC.y, nextWC.x);\n\
  69. }\n\
  70. else if (czm_equalsEpsilon(normalize(next.xyz - position.xyz), vec3(0.0), czm_epsilon1) || clipped)\n\
  71. {\n\
  72. direction = vec2(prevWC.y, -prevWC.x);\n\
  73. }\n\
  74. else\n\
  75. {\n\
  76. vec2 normal = vec2(-nextWC.y, nextWC.x);\n\
  77. direction = normalize((nextWC + prevWC) * 0.5);\n\
  78. if (dot(direction, normal) < 0.0)\n\
  79. {\n\
  80. direction = -direction;\n\
  81. }\n\
  82. \n\
  83. // The sine of the angle between the two vectors is given by the formula\n\
  84. // |a x b| = |a||b|sin(theta)\n\
  85. // which is\n\
  86. // float sinAngle = length(cross(vec3(direction, 0.0), vec3(nextWC, 0.0)));\n\
  87. // Because the z components of both vectors are zero, the x and y coordinate will be zero.\n\
  88. // Therefore, the sine of the angle is just the z component of the cross product.\n\
  89. float sinAngle = abs(direction.x * nextWC.y - direction.y * nextWC.x);\n\
  90. expandWidth = clamp(expandWidth / sinAngle, 0.0, width * 2.0);\n\
  91. }\n\
  92. \n\
  93. vec2 offset = direction * expandDirection * expandWidth * czm_resolutionScale;\n\
  94. return vec4(endPointWC.xy + offset, -endPointWC.z, 1.0);\n\
  95. }\n\
  96. ";
  97. });