PolylineShadowVolumeVS.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "attribute vec3 position3DHigh;\n\
  3. attribute vec3 position3DLow;\n\
  4. \n\
  5. // In 2D and in 3D, texture coordinate normalization component signs encodes:\n\
  6. // * X sign - sidedness relative to right plane\n\
  7. // * Y sign - is negative OR magnitude is greater than 1.0 if vertex is on bottom of volume\n\
  8. #ifndef COLUMBUS_VIEW_2D\n\
  9. attribute vec4 startHiAndForwardOffsetX;\n\
  10. attribute vec4 startLoAndForwardOffsetY;\n\
  11. attribute vec4 startNormalAndForwardOffsetZ;\n\
  12. attribute vec4 endNormalAndTextureCoordinateNormalizationX;\n\
  13. attribute vec4 rightNormalAndTextureCoordinateNormalizationY;\n\
  14. #else\n\
  15. attribute vec4 startHiLo2D;\n\
  16. attribute vec4 offsetAndRight2D;\n\
  17. attribute vec4 startEndNormals2D;\n\
  18. attribute vec2 texcoordNormalization2D;\n\
  19. #endif\n\
  20. \n\
  21. attribute float batchId;\n\
  22. \n\
  23. varying vec4 v_startPlaneNormalEcAndHalfWidth;\n\
  24. varying vec4 v_endPlaneNormalEcAndBatchId;\n\
  25. varying vec4 v_rightPlaneEC;\n\
  26. varying vec4 v_endEcAndStartEcX;\n\
  27. varying vec4 v_texcoordNormalizationAndStartEcYZ;\n\
  28. \n\
  29. // For materials\n\
  30. #ifdef WIDTH_VARYING\n\
  31. varying float v_width;\n\
  32. #endif\n\
  33. #ifdef ANGLE_VARYING\n\
  34. varying float v_polylineAngle;\n\
  35. #endif\n\
  36. \n\
  37. #ifdef PER_INSTANCE_COLOR\n\
  38. varying vec4 v_color;\n\
  39. #endif\n\
  40. \n\
  41. void main()\n\
  42. {\n\
  43. #ifdef COLUMBUS_VIEW_2D\n\
  44. vec3 ecStart = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(vec3(0.0, startHiLo2D.xy), vec3(0.0, startHiLo2D.zw))).xyz;\n\
  45. \n\
  46. vec3 forwardDirectionEC = czm_normal * vec3(0.0, offsetAndRight2D.xy);\n\
  47. vec3 ecEnd = forwardDirectionEC + ecStart;\n\
  48. forwardDirectionEC = normalize(forwardDirectionEC);\n\
  49. \n\
  50. // Right plane\n\
  51. v_rightPlaneEC.xyz = czm_normal * vec3(0.0, offsetAndRight2D.zw);\n\
  52. v_rightPlaneEC.w = -dot(v_rightPlaneEC.xyz, ecStart);\n\
  53. \n\
  54. // start plane\n\
  55. vec4 startPlaneEC;\n\
  56. startPlaneEC.xyz = czm_normal * vec3(0.0, startEndNormals2D.xy);\n\
  57. startPlaneEC.w = -dot(startPlaneEC.xyz, ecStart);\n\
  58. \n\
  59. // end plane\n\
  60. vec4 endPlaneEC;\n\
  61. endPlaneEC.xyz = czm_normal * vec3(0.0, startEndNormals2D.zw);\n\
  62. endPlaneEC.w = -dot(endPlaneEC.xyz, ecEnd);\n\
  63. \n\
  64. v_texcoordNormalizationAndStartEcYZ.x = abs(texcoordNormalization2D.x);\n\
  65. v_texcoordNormalizationAndStartEcYZ.y = texcoordNormalization2D.y;\n\
  66. \n\
  67. #else // COLUMBUS_VIEW_2D\n\
  68. vec3 ecStart = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(startHiAndForwardOffsetX.xyz, startLoAndForwardOffsetY.xyz)).xyz;\n\
  69. vec3 offset = czm_normal * vec3(startHiAndForwardOffsetX.w, startLoAndForwardOffsetY.w, startNormalAndForwardOffsetZ.w);\n\
  70. vec3 ecEnd = ecStart + offset;\n\
  71. \n\
  72. vec3 forwardDirectionEC = normalize(offset);\n\
  73. \n\
  74. // start plane\n\
  75. vec4 startPlaneEC;\n\
  76. startPlaneEC.xyz = czm_normal * startNormalAndForwardOffsetZ.xyz;\n\
  77. startPlaneEC.w = -dot(startPlaneEC.xyz, ecStart);\n\
  78. \n\
  79. // end plane\n\
  80. vec4 endPlaneEC;\n\
  81. endPlaneEC.xyz = czm_normal * endNormalAndTextureCoordinateNormalizationX.xyz;\n\
  82. endPlaneEC.w = -dot(endPlaneEC.xyz, ecEnd);\n\
  83. \n\
  84. // Right plane\n\
  85. v_rightPlaneEC.xyz = czm_normal * rightNormalAndTextureCoordinateNormalizationY.xyz;\n\
  86. v_rightPlaneEC.w = -dot(v_rightPlaneEC.xyz, ecStart);\n\
  87. \n\
  88. v_texcoordNormalizationAndStartEcYZ.x = abs(endNormalAndTextureCoordinateNormalizationX.w);\n\
  89. v_texcoordNormalizationAndStartEcYZ.y = rightNormalAndTextureCoordinateNormalizationY.w;\n\
  90. \n\
  91. #endif // COLUMBUS_VIEW_2D\n\
  92. \n\
  93. v_endEcAndStartEcX.xyz = ecEnd;\n\
  94. v_endEcAndStartEcX.w = ecStart.x;\n\
  95. v_texcoordNormalizationAndStartEcYZ.zw = ecStart.yz;\n\
  96. \n\
  97. #ifdef PER_INSTANCE_COLOR\n\
  98. v_color = czm_batchTable_color(batchId);\n\
  99. #endif // PER_INSTANCE_COLOR\n\
  100. \n\
  101. // Compute a normal along which to \"push\" the position out, extending the miter depending on view distance.\n\
  102. // Position has already been \"pushed\" by unit length along miter normal, and miter normals are encoded in the planes.\n\
  103. // Decode the normal to use at this specific vertex, push the position back, and then push to where it needs to be.\n\
  104. vec4 positionRelativeToEye = czm_computePosition();\n\
  105. \n\
  106. // Check distance to the end plane and start plane, pick the plane that is closer\n\
  107. vec4 positionEC = czm_modelViewRelativeToEye * positionRelativeToEye; // w = 1.0, see czm_computePosition\n\
  108. float absStartPlaneDistance = abs(czm_planeDistance(startPlaneEC, positionEC.xyz));\n\
  109. float absEndPlaneDistance = abs(czm_planeDistance(endPlaneEC, positionEC.xyz));\n\
  110. vec3 planeDirection = czm_branchFreeTernary(absStartPlaneDistance < absEndPlaneDistance, startPlaneEC.xyz, endPlaneEC.xyz);\n\
  111. vec3 upOrDown = normalize(cross(v_rightPlaneEC.xyz, planeDirection)); // Points \"up\" for start plane, \"down\" at end plane.\n\
  112. vec3 normalEC = normalize(cross(planeDirection, upOrDown)); // In practice, the opposite seems to work too.\n\
  113. \n\
  114. // Extrude bottom vertices downward for far view distances, like for GroundPrimitives\n\
  115. upOrDown = cross(forwardDirectionEC, normalEC);\n\
  116. upOrDown = float(czm_sceneMode == czm_sceneMode3D) * upOrDown;\n\
  117. upOrDown = float(v_texcoordNormalizationAndStartEcYZ.y > 1.0 || v_texcoordNormalizationAndStartEcYZ.y < 0.0) * upOrDown;\n\
  118. upOrDown = min(GLOBE_MINIMUM_ALTITUDE, czm_geometricToleranceOverMeter * length(positionRelativeToEye.xyz)) * upOrDown;\n\
  119. positionEC.xyz += upOrDown;\n\
  120. \n\
  121. v_texcoordNormalizationAndStartEcYZ.y = czm_branchFreeTernary(v_texcoordNormalizationAndStartEcYZ.y > 1.0, 0.0, abs(v_texcoordNormalizationAndStartEcYZ.y));\n\
  122. \n\
  123. // Determine distance along normalEC to push for a volume of appropriate width.\n\
  124. // Make volumes about double pixel width for a conservative fit - in practice the\n\
  125. // extra cost here is minimal compared to the loose volume heights.\n\
  126. //\n\
  127. // N = normalEC (guaranteed \"right-facing\")\n\
  128. // R = rightEC\n\
  129. // p = angle between N and R\n\
  130. // w = distance to push along R if R == N\n\
  131. // d = distance to push along N\n\
  132. //\n\
  133. // N R\n\
  134. // { \ p| } * cos(p) = dot(N, R) = w / d\n\
  135. // d\ \ | |w * d = w / dot(N, R)\n\
  136. // { \| }\n\
  137. // o---------- polyline segment ---->\n\
  138. //\n\
  139. float width = czm_batchTable_width(batchId);\n\
  140. #ifdef WIDTH_VARYING\n\
  141. v_width = width;\n\
  142. #endif\n\
  143. \n\
  144. v_startPlaneNormalEcAndHalfWidth.xyz = startPlaneEC.xyz;\n\
  145. v_startPlaneNormalEcAndHalfWidth.w = width * 0.5;\n\
  146. \n\
  147. v_endPlaneNormalEcAndBatchId.xyz = endPlaneEC.xyz;\n\
  148. v_endPlaneNormalEcAndBatchId.w = batchId;\n\
  149. \n\
  150. width = width * max(0.0, czm_metersPerPixel(positionEC)); // width = distance to push along R\n\
  151. width = width / dot(normalEC, v_rightPlaneEC.xyz); // width = distance to push along N\n\
  152. \n\
  153. // Determine if this vertex is on the \"left\" or \"right\"\n\
  154. #ifdef COLUMBUS_VIEW_2D\n\
  155. normalEC *= sign(texcoordNormalization2D.x);\n\
  156. #else\n\
  157. normalEC *= sign(endNormalAndTextureCoordinateNormalizationX.w);\n\
  158. #endif\n\
  159. \n\
  160. positionEC.xyz += width * normalEC;\n\
  161. gl_Position = czm_depthClamp(czm_projection * positionEC);\n\
  162. \n\
  163. #ifdef ANGLE_VARYING\n\
  164. // Approximate relative screen space direction of the line.\n\
  165. vec2 approxLineDirection = normalize(vec2(forwardDirectionEC.x, -forwardDirectionEC.y));\n\
  166. approxLineDirection.y = czm_branchFreeTernary(approxLineDirection.x == 0.0 && approxLineDirection.y == 0.0, -1.0, approxLineDirection.y);\n\
  167. v_polylineAngle = czm_fastApproximateAtan(approxLineDirection.x, approxLineDirection.y);\n\
  168. #endif\n\
  169. }\n\
  170. ";