GlobeVS.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "#ifdef QUANTIZATION_BITS12\n\
  3. in vec4 compressed0;\n\
  4. in float compressed1;\n\
  5. #else\n\
  6. in vec4 position3DAndHeight;\n\
  7. in vec4 textureCoordAndEncodedNormals;\n\
  8. #endif\n\
  9. \n\
  10. #ifdef GEODETIC_SURFACE_NORMALS\n\
  11. in vec3 geodeticSurfaceNormal;\n\
  12. #endif\n\
  13. \n\
  14. #ifdef EXAGGERATION\n\
  15. uniform vec2 u_terrainExaggerationAndRelativeHeight;\n\
  16. #endif\n\
  17. \n\
  18. uniform vec3 u_center3D;\n\
  19. uniform mat4 u_modifiedModelView;\n\
  20. uniform mat4 u_modifiedModelViewProjection;\n\
  21. uniform vec4 u_tileRectangle;\n\
  22. \n\
  23. // Uniforms for 2D Mercator projection\n\
  24. uniform vec2 u_southAndNorthLatitude;\n\
  25. uniform vec2 u_southMercatorYAndOneOverHeight;\n\
  26. \n\
  27. out vec3 v_positionMC;\n\
  28. out vec3 v_positionEC;\n\
  29. \n\
  30. out vec3 v_textureCoordinates;\n\
  31. out vec3 v_normalMC;\n\
  32. out vec3 v_normalEC;\n\
  33. \n\
  34. #ifdef APPLY_MATERIAL\n\
  35. out float v_slope;\n\
  36. out float v_aspect;\n\
  37. out float v_height;\n\
  38. #endif\n\
  39. \n\
  40. #if defined(FOG) || defined(GROUND_ATMOSPHERE) || defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT)\n\
  41. out float v_distance;\n\
  42. #endif\n\
  43. \n\
  44. #if defined(FOG) || defined(GROUND_ATMOSPHERE)\n\
  45. out vec3 v_atmosphereRayleighColor;\n\
  46. out vec3 v_atmosphereMieColor;\n\
  47. out float v_atmosphereOpacity;\n\
  48. #endif\n\
  49. \n\
  50. // These functions are generated at runtime.\n\
  51. vec4 getPosition(vec3 position, float height, vec2 textureCoordinates);\n\
  52. float get2DYPositionFraction(vec2 textureCoordinates);\n\
  53. \n\
  54. vec4 getPosition3DMode(vec3 position, float height, vec2 textureCoordinates)\n\
  55. {\n\
  56. return u_modifiedModelViewProjection * vec4(position, 1.0);\n\
  57. }\n\
  58. \n\
  59. float get2DMercatorYPositionFraction(vec2 textureCoordinates)\n\
  60. {\n\
  61. // The width of a tile at level 11, in radians and assuming a single root tile, is\n\
  62. // 2.0 * czm_pi / pow(2.0, 11.0)\n\
  63. // We want to just linearly interpolate the 2D position from the texture coordinates\n\
  64. // when we're at this level or higher. The constant below is the expression\n\
  65. // above evaluated and then rounded up at the 4th significant digit.\n\
  66. const float maxTileWidth = 0.003068;\n\
  67. float positionFraction = textureCoordinates.y;\n\
  68. float southLatitude = u_southAndNorthLatitude.x;\n\
  69. float northLatitude = u_southAndNorthLatitude.y;\n\
  70. if (northLatitude - southLatitude > maxTileWidth)\n\
  71. {\n\
  72. float southMercatorY = u_southMercatorYAndOneOverHeight.x;\n\
  73. float oneOverMercatorHeight = u_southMercatorYAndOneOverHeight.y;\n\
  74. \n\
  75. float currentLatitude = mix(southLatitude, northLatitude, textureCoordinates.y);\n\
  76. currentLatitude = clamp(currentLatitude, -czm_webMercatorMaxLatitude, czm_webMercatorMaxLatitude);\n\
  77. positionFraction = czm_latitudeToWebMercatorFraction(currentLatitude, southMercatorY, oneOverMercatorHeight);\n\
  78. }\n\
  79. return positionFraction;\n\
  80. }\n\
  81. \n\
  82. float get2DGeographicYPositionFraction(vec2 textureCoordinates)\n\
  83. {\n\
  84. return textureCoordinates.y;\n\
  85. }\n\
  86. \n\
  87. vec4 getPositionPlanarEarth(vec3 position, float height, vec2 textureCoordinates)\n\
  88. {\n\
  89. float yPositionFraction = get2DYPositionFraction(textureCoordinates);\n\
  90. vec4 rtcPosition2D = vec4(height, mix(u_tileRectangle.st, u_tileRectangle.pq, vec2(textureCoordinates.x, yPositionFraction)), 1.0);\n\
  91. return u_modifiedModelViewProjection * rtcPosition2D;\n\
  92. }\n\
  93. \n\
  94. vec4 getPosition2DMode(vec3 position, float height, vec2 textureCoordinates)\n\
  95. {\n\
  96. return getPositionPlanarEarth(position, 0.0, textureCoordinates);\n\
  97. }\n\
  98. \n\
  99. vec4 getPositionColumbusViewMode(vec3 position, float height, vec2 textureCoordinates)\n\
  100. {\n\
  101. return getPositionPlanarEarth(position, height, textureCoordinates);\n\
  102. }\n\
  103. \n\
  104. vec4 getPositionMorphingMode(vec3 position, float height, vec2 textureCoordinates)\n\
  105. {\n\
  106. // We do not do RTC while morphing, so there is potential for jitter.\n\
  107. // This is unlikely to be noticeable, though.\n\
  108. vec3 position3DWC = position + u_center3D;\n\
  109. float yPositionFraction = get2DYPositionFraction(textureCoordinates);\n\
  110. vec4 position2DWC = vec4(height, mix(u_tileRectangle.st, u_tileRectangle.pq, vec2(textureCoordinates.x, yPositionFraction)), 1.0);\n\
  111. vec4 morphPosition = czm_columbusViewMorph(position2DWC, vec4(position3DWC, 1.0), czm_morphTime);\n\
  112. return czm_modelViewProjection * morphPosition;\n\
  113. }\n\
  114. \n\
  115. #ifdef QUANTIZATION_BITS12\n\
  116. uniform vec2 u_minMaxHeight;\n\
  117. uniform mat4 u_scaleAndBias;\n\
  118. #endif\n\
  119. \n\
  120. void main()\n\
  121. {\n\
  122. #ifdef QUANTIZATION_BITS12\n\
  123. vec2 xy = czm_decompressTextureCoordinates(compressed0.x);\n\
  124. vec2 zh = czm_decompressTextureCoordinates(compressed0.y);\n\
  125. vec3 position = vec3(xy, zh.x);\n\
  126. float height = zh.y;\n\
  127. vec2 textureCoordinates = czm_decompressTextureCoordinates(compressed0.z);\n\
  128. \n\
  129. height = height * (u_minMaxHeight.y - u_minMaxHeight.x) + u_minMaxHeight.x;\n\
  130. position = (u_scaleAndBias * vec4(position, 1.0)).xyz;\n\
  131. \n\
  132. #if (defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL)) && defined(INCLUDE_WEB_MERCATOR_Y)\n\
  133. float webMercatorT = czm_decompressTextureCoordinates(compressed0.w).x;\n\
  134. float encodedNormal = compressed1;\n\
  135. #elif defined(INCLUDE_WEB_MERCATOR_Y)\n\
  136. float webMercatorT = czm_decompressTextureCoordinates(compressed0.w).x;\n\
  137. float encodedNormal = 0.0;\n\
  138. #elif defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL)\n\
  139. float webMercatorT = textureCoordinates.y;\n\
  140. float encodedNormal = compressed0.w;\n\
  141. #else\n\
  142. float webMercatorT = textureCoordinates.y;\n\
  143. float encodedNormal = 0.0;\n\
  144. #endif\n\
  145. \n\
  146. #else\n\
  147. // A single float per element\n\
  148. vec3 position = position3DAndHeight.xyz;\n\
  149. float height = position3DAndHeight.w;\n\
  150. vec2 textureCoordinates = textureCoordAndEncodedNormals.xy;\n\
  151. \n\
  152. #if (defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)) && defined(INCLUDE_WEB_MERCATOR_Y)\n\
  153. float webMercatorT = textureCoordAndEncodedNormals.z;\n\
  154. float encodedNormal = textureCoordAndEncodedNormals.w;\n\
  155. #elif defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)\n\
  156. float webMercatorT = textureCoordinates.y;\n\
  157. float encodedNormal = textureCoordAndEncodedNormals.z;\n\
  158. #elif defined(INCLUDE_WEB_MERCATOR_Y)\n\
  159. float webMercatorT = textureCoordAndEncodedNormals.z;\n\
  160. float encodedNormal = 0.0;\n\
  161. #else\n\
  162. float webMercatorT = textureCoordinates.y;\n\
  163. float encodedNormal = 0.0;\n\
  164. #endif\n\
  165. \n\
  166. #endif\n\
  167. \n\
  168. vec3 position3DWC = position + u_center3D;\n\
  169. \n\
  170. #ifdef GEODETIC_SURFACE_NORMALS\n\
  171. vec3 ellipsoidNormal = geodeticSurfaceNormal;\n\
  172. #else\n\
  173. vec3 ellipsoidNormal = normalize(position3DWC);\n\
  174. #endif\n\
  175. \n\
  176. #if defined(EXAGGERATION) && defined(GEODETIC_SURFACE_NORMALS)\n\
  177. float exaggeration = u_terrainExaggerationAndRelativeHeight.x;\n\
  178. float relativeHeight = u_terrainExaggerationAndRelativeHeight.y;\n\
  179. float newHeight = (height - relativeHeight) * exaggeration + relativeHeight;\n\
  180. \n\
  181. // stop from going through center of earth\n\
  182. float minRadius = min(min(czm_ellipsoidRadii.x, czm_ellipsoidRadii.y), czm_ellipsoidRadii.z);\n\
  183. newHeight = max(newHeight, -minRadius);\n\
  184. \n\
  185. vec3 offset = ellipsoidNormal * (newHeight - height);\n\
  186. position += offset;\n\
  187. position3DWC += offset;\n\
  188. height = newHeight;\n\
  189. #endif\n\
  190. \n\
  191. gl_Position = getPosition(position, height, textureCoordinates);\n\
  192. \n\
  193. v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz;\n\
  194. v_positionMC = position3DWC; // position in model coordinates\n\
  195. \n\
  196. v_textureCoordinates = vec3(textureCoordinates, webMercatorT);\n\
  197. \n\
  198. #if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)\n\
  199. vec3 normalMC = czm_octDecode(encodedNormal);\n\
  200. \n\
  201. #if defined(EXAGGERATION) && defined(GEODETIC_SURFACE_NORMALS)\n\
  202. vec3 projection = dot(normalMC, ellipsoidNormal) * ellipsoidNormal;\n\
  203. vec3 rejection = normalMC - projection;\n\
  204. normalMC = normalize(projection + rejection * exaggeration);\n\
  205. #endif\n\
  206. \n\
  207. v_normalMC = normalMC;\n\
  208. v_normalEC = czm_normal3D * v_normalMC;\n\
  209. #endif\n\
  210. \n\
  211. #if defined(FOG) || (defined(GROUND_ATMOSPHERE) && !defined(PER_FRAGMENT_GROUND_ATMOSPHERE))\n\
  212. \n\
  213. bool dynamicLighting = false;\n\
  214. \n\
  215. #if defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_DAYNIGHT_SHADING) || defined(ENABLE_VERTEX_LIGHTING))\n\
  216. dynamicLighting = true;\n\
  217. #endif\n\
  218. \n\
  219. #if defined(DYNAMIC_ATMOSPHERE_LIGHTING_FROM_SUN)\n\
  220. vec3 atmosphereLightDirection = czm_sunDirectionWC;\n\
  221. #else\n\
  222. vec3 atmosphereLightDirection = czm_lightDirectionWC;\n\
  223. #endif\n\
  224. \n\
  225. vec3 lightDirection = czm_branchFreeTernary(dynamicLighting, atmosphereLightDirection, normalize(position3DWC));\n\
  226. \n\
  227. computeAtmosphereScattering(\n\
  228. position3DWC,\n\
  229. lightDirection,\n\
  230. v_atmosphereRayleighColor,\n\
  231. v_atmosphereMieColor,\n\
  232. v_atmosphereOpacity\n\
  233. );\n\
  234. #endif\n\
  235. \n\
  236. #if defined(FOG) || defined(GROUND_ATMOSPHERE) || defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT)\n\
  237. v_distance = length((czm_modelView3D * vec4(position3DWC, 1.0)).xyz);\n\
  238. #endif\n\
  239. \n\
  240. #ifdef APPLY_MATERIAL\n\
  241. float northPoleZ = czm_ellipsoidRadii.z;\n\
  242. vec3 northPolePositionMC = vec3(0.0, 0.0, northPoleZ);\n\
  243. vec3 vectorEastMC = normalize(cross(northPolePositionMC - v_positionMC, ellipsoidNormal));\n\
  244. float dotProd = abs(dot(ellipsoidNormal, v_normalMC));\n\
  245. v_slope = acos(dotProd);\n\
  246. vec3 normalRejected = ellipsoidNormal * dotProd;\n\
  247. vec3 normalProjected = v_normalMC - normalRejected;\n\
  248. vec3 aspectVector = normalize(normalProjected);\n\
  249. v_aspect = acos(dot(aspectVector, vectorEastMC));\n\
  250. float determ = dot(cross(vectorEastMC, aspectVector), ellipsoidNormal);\n\
  251. v_aspect = czm_branchFreeTernary(determ < 0.0, 2.0 * czm_pi - v_aspect, v_aspect);\n\
  252. v_height = height;\n\
  253. #endif\n\
  254. }\n\
  255. ";