PolylineShadowVolumeMorphVS.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "in vec3 position3DHigh;\n\
  3. in vec3 position3DLow;\n\
  4. \n\
  5. in vec4 startHiAndForwardOffsetX;\n\
  6. in vec4 startLoAndForwardOffsetY;\n\
  7. in vec4 startNormalAndForwardOffsetZ;\n\
  8. in vec4 endNormalAndTextureCoordinateNormalizationX;\n\
  9. in vec4 rightNormalAndTextureCoordinateNormalizationY;\n\
  10. in vec4 startHiLo2D;\n\
  11. in vec4 offsetAndRight2D;\n\
  12. in vec4 startEndNormals2D;\n\
  13. in vec2 texcoordNormalization2D;\n\
  14. \n\
  15. in float batchId;\n\
  16. \n\
  17. out vec3 v_forwardDirectionEC;\n\
  18. out vec3 v_texcoordNormalizationAndHalfWidth;\n\
  19. out float v_batchId;\n\
  20. \n\
  21. // For materials\n\
  22. #ifdef WIDTH_VARYING\n\
  23. out float v_width;\n\
  24. #endif\n\
  25. #ifdef ANGLE_VARYING\n\
  26. out float v_polylineAngle;\n\
  27. #endif\n\
  28. \n\
  29. #ifdef PER_INSTANCE_COLOR\n\
  30. out vec4 v_color;\n\
  31. #else\n\
  32. out vec2 v_alignedPlaneDistances;\n\
  33. out float v_texcoordT;\n\
  34. #endif\n\
  35. \n\
  36. // Morphing planes using SLERP or NLERP doesn't seem to work, so instead draw the material directly on the shadow volume.\n\
  37. // Morph views are from very far away and aren't meant to be used precisely, so this should be sufficient.\n\
  38. void main()\n\
  39. {\n\
  40. v_batchId = batchId;\n\
  41. \n\
  42. // Start position\n\
  43. vec4 posRelativeToEye2D = czm_translateRelativeToEye(vec3(0.0, startHiLo2D.xy), vec3(0.0, startHiLo2D.zw));\n\
  44. vec4 posRelativeToEye3D = czm_translateRelativeToEye(startHiAndForwardOffsetX.xyz, startLoAndForwardOffsetY.xyz);\n\
  45. vec4 posRelativeToEye = czm_columbusViewMorph(posRelativeToEye2D, posRelativeToEye3D, czm_morphTime);\n\
  46. vec3 posEc2D = (czm_modelViewRelativeToEye * posRelativeToEye2D).xyz;\n\
  47. vec3 posEc3D = (czm_modelViewRelativeToEye * posRelativeToEye3D).xyz;\n\
  48. vec3 startEC = (czm_modelViewRelativeToEye * posRelativeToEye).xyz;\n\
  49. \n\
  50. // Start plane\n\
  51. vec4 startPlane2D;\n\
  52. vec4 startPlane3D;\n\
  53. startPlane2D.xyz = czm_normal * vec3(0.0, startEndNormals2D.xy);\n\
  54. startPlane3D.xyz = czm_normal * startNormalAndForwardOffsetZ.xyz;\n\
  55. startPlane2D.w = -dot(startPlane2D.xyz, posEc2D);\n\
  56. startPlane3D.w = -dot(startPlane3D.xyz, posEc3D);\n\
  57. \n\
  58. // Right plane\n\
  59. vec4 rightPlane2D;\n\
  60. vec4 rightPlane3D;\n\
  61. rightPlane2D.xyz = czm_normal * vec3(0.0, offsetAndRight2D.zw);\n\
  62. rightPlane3D.xyz = czm_normal * rightNormalAndTextureCoordinateNormalizationY.xyz;\n\
  63. rightPlane2D.w = -dot(rightPlane2D.xyz, posEc2D);\n\
  64. rightPlane3D.w = -dot(rightPlane3D.xyz, posEc3D);\n\
  65. \n\
  66. // End position\n\
  67. posRelativeToEye2D = posRelativeToEye2D + vec4(0.0, offsetAndRight2D.xy, 0.0);\n\
  68. posRelativeToEye3D = posRelativeToEye3D + vec4(startHiAndForwardOffsetX.w, startLoAndForwardOffsetY.w, startNormalAndForwardOffsetZ.w, 0.0);\n\
  69. posRelativeToEye = czm_columbusViewMorph(posRelativeToEye2D, posRelativeToEye3D, czm_morphTime);\n\
  70. posEc2D = (czm_modelViewRelativeToEye * posRelativeToEye2D).xyz;\n\
  71. posEc3D = (czm_modelViewRelativeToEye * posRelativeToEye3D).xyz;\n\
  72. vec3 endEC = (czm_modelViewRelativeToEye * posRelativeToEye).xyz;\n\
  73. vec3 forwardEc3D = czm_normal * normalize(vec3(startHiAndForwardOffsetX.w, startLoAndForwardOffsetY.w, startNormalAndForwardOffsetZ.w));\n\
  74. vec3 forwardEc2D = czm_normal * normalize(vec3(0.0, offsetAndRight2D.xy));\n\
  75. \n\
  76. // End plane\n\
  77. vec4 endPlane2D;\n\
  78. vec4 endPlane3D;\n\
  79. endPlane2D.xyz = czm_normal * vec3(0.0, startEndNormals2D.zw);\n\
  80. endPlane3D.xyz = czm_normal * endNormalAndTextureCoordinateNormalizationX.xyz;\n\
  81. endPlane2D.w = -dot(endPlane2D.xyz, posEc2D);\n\
  82. endPlane3D.w = -dot(endPlane3D.xyz, posEc3D);\n\
  83. \n\
  84. // Forward direction\n\
  85. v_forwardDirectionEC = normalize(endEC - startEC);\n\
  86. \n\
  87. vec2 cleanTexcoordNormalization2D;\n\
  88. cleanTexcoordNormalization2D.x = abs(texcoordNormalization2D.x);\n\
  89. cleanTexcoordNormalization2D.y = czm_branchFreeTernary(texcoordNormalization2D.y > 1.0, 0.0, abs(texcoordNormalization2D.y));\n\
  90. vec2 cleanTexcoordNormalization3D;\n\
  91. cleanTexcoordNormalization3D.x = abs(endNormalAndTextureCoordinateNormalizationX.w);\n\
  92. cleanTexcoordNormalization3D.y = rightNormalAndTextureCoordinateNormalizationY.w;\n\
  93. cleanTexcoordNormalization3D.y = czm_branchFreeTernary(cleanTexcoordNormalization3D.y > 1.0, 0.0, abs(cleanTexcoordNormalization3D.y));\n\
  94. \n\
  95. v_texcoordNormalizationAndHalfWidth.xy = mix(cleanTexcoordNormalization2D, cleanTexcoordNormalization3D, czm_morphTime);\n\
  96. \n\
  97. #ifdef PER_INSTANCE_COLOR\n\
  98. v_color = czm_batchTable_color(batchId);\n\
  99. #else // PER_INSTANCE_COLOR\n\
  100. // For computing texture coordinates\n\
  101. \n\
  102. v_alignedPlaneDistances.x = -dot(v_forwardDirectionEC, startEC);\n\
  103. v_alignedPlaneDistances.y = -dot(-v_forwardDirectionEC, endEC);\n\
  104. #endif // PER_INSTANCE_COLOR\n\
  105. \n\
  106. #ifdef WIDTH_VARYING\n\
  107. float width = czm_batchTable_width(batchId);\n\
  108. float halfWidth = width * 0.5;\n\
  109. v_width = width;\n\
  110. v_texcoordNormalizationAndHalfWidth.z = halfWidth;\n\
  111. #else\n\
  112. float halfWidth = 0.5 * czm_batchTable_width(batchId);\n\
  113. v_texcoordNormalizationAndHalfWidth.z = halfWidth;\n\
  114. #endif\n\
  115. \n\
  116. // Compute a normal along which to \"push\" the position out, extending the miter depending on view distance.\n\
  117. // Position has already been \"pushed\" by unit length along miter normal, and miter normals are encoded in the planes.\n\
  118. // Decode the normal to use at this specific vertex, push the position back, and then push to where it needs to be.\n\
  119. // Since this is morphing, compute both 3D and 2D positions and then blend.\n\
  120. \n\
  121. // ****** 3D ******\n\
  122. // Check distance to the end plane and start plane, pick the plane that is closer\n\
  123. vec4 positionEc3D = czm_modelViewRelativeToEye * czm_translateRelativeToEye(position3DHigh, position3DLow); // w = 1.0, see czm_computePosition\n\
  124. float absStartPlaneDistance = abs(czm_planeDistance(startPlane3D, positionEc3D.xyz));\n\
  125. float absEndPlaneDistance = abs(czm_planeDistance(endPlane3D, positionEc3D.xyz));\n\
  126. vec3 planeDirection = czm_branchFreeTernary(absStartPlaneDistance < absEndPlaneDistance, startPlane3D.xyz, endPlane3D.xyz);\n\
  127. vec3 upOrDown = normalize(cross(rightPlane3D.xyz, planeDirection)); // Points \"up\" for start plane, \"down\" at end plane.\n\
  128. vec3 normalEC = normalize(cross(planeDirection, upOrDown)); // In practice, the opposite seems to work too.\n\
  129. \n\
  130. // Nudge the top vertex upwards to prevent flickering\n\
  131. vec3 geodeticSurfaceNormal = normalize(cross(normalEC, forwardEc3D));\n\
  132. geodeticSurfaceNormal *= float(0.0 <= rightNormalAndTextureCoordinateNormalizationY.w && rightNormalAndTextureCoordinateNormalizationY.w <= 1.0);\n\
  133. geodeticSurfaceNormal *= MAX_TERRAIN_HEIGHT;\n\
  134. positionEc3D.xyz += geodeticSurfaceNormal;\n\
  135. \n\
  136. // Determine if this vertex is on the \"left\" or \"right\"\n\
  137. normalEC *= sign(endNormalAndTextureCoordinateNormalizationX.w);\n\
  138. \n\
  139. // A \"perfect\" implementation would push along normals according to the angle against forward.\n\
  140. // In practice, just pushing the normal out by halfWidth is sufficient for morph views.\n\
  141. positionEc3D.xyz += halfWidth * max(0.0, czm_metersPerPixel(positionEc3D)) * normalEC; // prevent artifacts when czm_metersPerPixel is negative (behind camera)\n\
  142. \n\
  143. // ****** 2D ******\n\
  144. // Check distance to the end plane and start plane, pick the plane that is closer\n\
  145. vec4 positionEc2D = czm_modelViewRelativeToEye * czm_translateRelativeToEye(position2DHigh.zxy, position2DLow.zxy); // w = 1.0, see czm_computePosition\n\
  146. absStartPlaneDistance = abs(czm_planeDistance(startPlane2D, positionEc2D.xyz));\n\
  147. absEndPlaneDistance = abs(czm_planeDistance(endPlane2D, positionEc2D.xyz));\n\
  148. planeDirection = czm_branchFreeTernary(absStartPlaneDistance < absEndPlaneDistance, startPlane2D.xyz, endPlane2D.xyz);\n\
  149. upOrDown = normalize(cross(rightPlane2D.xyz, planeDirection)); // Points \"up\" for start plane, \"down\" at end plane.\n\
  150. normalEC = normalize(cross(planeDirection, upOrDown)); // In practice, the opposite seems to work too.\n\
  151. \n\
  152. // Nudge the top vertex upwards to prevent flickering\n\
  153. geodeticSurfaceNormal = normalize(cross(normalEC, forwardEc2D));\n\
  154. geodeticSurfaceNormal *= float(0.0 <= texcoordNormalization2D.y && texcoordNormalization2D.y <= 1.0);\n\
  155. geodeticSurfaceNormal *= MAX_TERRAIN_HEIGHT;\n\
  156. positionEc2D.xyz += geodeticSurfaceNormal;\n\
  157. \n\
  158. // Determine if this vertex is on the \"left\" or \"right\"\n\
  159. normalEC *= sign(texcoordNormalization2D.x);\n\
  160. #ifndef PER_INSTANCE_COLOR\n\
  161. // Use vertex's sidedness to compute its texture coordinate.\n\
  162. v_texcoordT = clamp(sign(texcoordNormalization2D.x), 0.0, 1.0);\n\
  163. #endif\n\
  164. \n\
  165. // A \"perfect\" implementation would push along normals according to the angle against forward.\n\
  166. // In practice, just pushing the normal out by halfWidth is sufficient for morph views.\n\
  167. positionEc2D.xyz += halfWidth * max(0.0, czm_metersPerPixel(positionEc2D)) * normalEC; // prevent artifacts when czm_metersPerPixel is negative (behind camera)\n\
  168. \n\
  169. // Blend for actual position\n\
  170. gl_Position = czm_projection * mix(positionEc2D, positionEc3D, czm_morphTime);\n\
  171. \n\
  172. #ifdef ANGLE_VARYING\n\
  173. // Approximate relative screen space direction of the line.\n\
  174. vec2 approxLineDirection = normalize(vec2(v_forwardDirectionEC.x, -v_forwardDirectionEC.y));\n\
  175. approxLineDirection.y = czm_branchFreeTernary(approxLineDirection.x == 0.0 && approxLineDirection.y == 0.0, -1.0, approxLineDirection.y);\n\
  176. v_polylineAngle = czm_fastApproximateAtan(approxLineDirection.x, approxLineDirection.y);\n\
  177. #endif\n\
  178. }\n\
  179. ";