GlobeVS.glsl 9.1 KB

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