GlobeVS.glsl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. uniform vec3 u_center3D;
  9. uniform mat4 u_modifiedModelView;
  10. uniform mat4 u_modifiedModelViewProjection;
  11. uniform vec4 u_tileRectangle;
  12. // Uniforms for 2D Mercator projection
  13. uniform vec2 u_southAndNorthLatitude;
  14. uniform vec2 u_southMercatorYAndOneOverHeight;
  15. varying vec3 v_positionMC;
  16. varying vec3 v_positionEC;
  17. varying vec3 v_textureCoordinates;
  18. varying vec3 v_normalMC;
  19. varying vec3 v_normalEC;
  20. #ifdef APPLY_MATERIAL
  21. varying float v_slope;
  22. varying float v_aspect;
  23. varying float v_height;
  24. #endif
  25. #if defined(FOG) || defined(GROUND_ATMOSPHERE) || defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT)
  26. varying float v_distance;
  27. #endif
  28. #if defined(FOG) || defined(GROUND_ATMOSPHERE)
  29. varying vec3 v_fogMieColor;
  30. varying vec3 v_fogRayleighColor;
  31. #endif
  32. // These functions are generated at runtime.
  33. vec4 getPosition(vec3 position, float height, vec2 textureCoordinates);
  34. float get2DYPositionFraction(vec2 textureCoordinates);
  35. vec4 getPosition3DMode(vec3 position, float height, vec2 textureCoordinates)
  36. {
  37. return u_modifiedModelViewProjection * vec4(position, 1.0);
  38. }
  39. float get2DMercatorYPositionFraction(vec2 textureCoordinates)
  40. {
  41. // The width of a tile at level 11, in radians and assuming a single root tile, is
  42. // 2.0 * czm_pi / pow(2.0, 11.0)
  43. // We want to just linearly interpolate the 2D position from the texture coordinates
  44. // when we're at this level or higher. The constant below is the expression
  45. // above evaluated and then rounded up at the 4th significant digit.
  46. const float maxTileWidth = 0.003068;
  47. float positionFraction = textureCoordinates.y;
  48. float southLatitude = u_southAndNorthLatitude.x;
  49. float northLatitude = u_southAndNorthLatitude.y;
  50. if (northLatitude - southLatitude > maxTileWidth)
  51. {
  52. float southMercatorY = u_southMercatorYAndOneOverHeight.x;
  53. float oneOverMercatorHeight = u_southMercatorYAndOneOverHeight.y;
  54. float currentLatitude = mix(southLatitude, northLatitude, textureCoordinates.y);
  55. currentLatitude = clamp(currentLatitude, -czm_webMercatorMaxLatitude, czm_webMercatorMaxLatitude);
  56. positionFraction = czm_latitudeToWebMercatorFraction(currentLatitude, southMercatorY, oneOverMercatorHeight);
  57. }
  58. return positionFraction;
  59. }
  60. float get2DGeographicYPositionFraction(vec2 textureCoordinates)
  61. {
  62. return textureCoordinates.y;
  63. }
  64. vec4 getPositionPlanarEarth(vec3 position, float height, vec2 textureCoordinates)
  65. {
  66. float yPositionFraction = get2DYPositionFraction(textureCoordinates);
  67. vec4 rtcPosition2D = vec4(height, mix(u_tileRectangle.st, u_tileRectangle.pq, vec2(textureCoordinates.x, yPositionFraction)), 1.0);
  68. return u_modifiedModelViewProjection * rtcPosition2D;
  69. }
  70. vec4 getPosition2DMode(vec3 position, float height, vec2 textureCoordinates)
  71. {
  72. return getPositionPlanarEarth(position, 0.0, textureCoordinates);
  73. }
  74. vec4 getPositionColumbusViewMode(vec3 position, float height, vec2 textureCoordinates)
  75. {
  76. return getPositionPlanarEarth(position, height, textureCoordinates);
  77. }
  78. vec4 getPositionMorphingMode(vec3 position, float height, vec2 textureCoordinates)
  79. {
  80. // We do not do RTC while morphing, so there is potential for jitter.
  81. // This is unlikely to be noticeable, though.
  82. vec3 position3DWC = position + u_center3D;
  83. float yPositionFraction = get2DYPositionFraction(textureCoordinates);
  84. vec4 position2DWC = vec4(height, mix(u_tileRectangle.st, u_tileRectangle.pq, vec2(textureCoordinates.x, yPositionFraction)), 1.0);
  85. vec4 morphPosition = czm_columbusViewMorph(position2DWC, vec4(position3DWC, 1.0), czm_morphTime);
  86. return czm_modelViewProjection * morphPosition;
  87. }
  88. #ifdef QUANTIZATION_BITS12
  89. uniform vec2 u_minMaxHeight;
  90. uniform mat4 u_scaleAndBias;
  91. #endif
  92. void main()
  93. {
  94. #ifdef QUANTIZATION_BITS12
  95. vec2 xy = czm_decompressTextureCoordinates(compressed0.x);
  96. vec2 zh = czm_decompressTextureCoordinates(compressed0.y);
  97. vec3 position = vec3(xy, zh.x);
  98. float height = zh.y;
  99. vec2 textureCoordinates = czm_decompressTextureCoordinates(compressed0.z);
  100. height = height * (u_minMaxHeight.y - u_minMaxHeight.x) + u_minMaxHeight.x;
  101. position = (u_scaleAndBias * vec4(position, 1.0)).xyz;
  102. #if (defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL)) && defined(INCLUDE_WEB_MERCATOR_Y)
  103. float webMercatorT = czm_decompressTextureCoordinates(compressed0.w).x;
  104. float encodedNormal = compressed1;
  105. #elif defined(INCLUDE_WEB_MERCATOR_Y)
  106. float webMercatorT = czm_decompressTextureCoordinates(compressed0.w).x;
  107. float encodedNormal = 0.0;
  108. #elif defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL)
  109. float webMercatorT = textureCoordinates.y;
  110. float encodedNormal = compressed0.w;
  111. #else
  112. float webMercatorT = textureCoordinates.y;
  113. float encodedNormal = 0.0;
  114. #endif
  115. #else
  116. // A single float per element
  117. vec3 position = position3DAndHeight.xyz;
  118. float height = position3DAndHeight.w;
  119. vec2 textureCoordinates = textureCoordAndEncodedNormals.xy;
  120. #if (defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)) && defined(INCLUDE_WEB_MERCATOR_Y)
  121. float webMercatorT = textureCoordAndEncodedNormals.z;
  122. float encodedNormal = textureCoordAndEncodedNormals.w;
  123. #elif defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)
  124. float webMercatorT = textureCoordinates.y;
  125. float encodedNormal = textureCoordAndEncodedNormals.z;
  126. #elif defined(INCLUDE_WEB_MERCATOR_Y)
  127. float webMercatorT = textureCoordAndEncodedNormals.z;
  128. float encodedNormal = 0.0;
  129. #else
  130. float webMercatorT = textureCoordinates.y;
  131. float encodedNormal = 0.0;
  132. #endif
  133. #endif
  134. vec3 position3DWC = position + u_center3D;
  135. gl_Position = getPosition(position, height, textureCoordinates);
  136. v_textureCoordinates = vec3(textureCoordinates, webMercatorT);
  137. #if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)
  138. v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz;
  139. v_positionMC = position3DWC; // position in model coordinates
  140. vec3 normalMC = czm_octDecode(encodedNormal);
  141. v_normalMC = normalMC;
  142. v_normalEC = czm_normal3D * v_normalMC;
  143. #elif defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_DAYNIGHT_SHADING) || defined(GENERATE_POSITION) || defined(HDR)
  144. v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz;
  145. v_positionMC = position3DWC; // position in model coordinates
  146. #endif
  147. #if defined(FOG) || defined(GROUND_ATMOSPHERE)
  148. AtmosphereColor atmosFogColor = computeGroundAtmosphereFromSpace(position3DWC, false, vec3(0.0));
  149. v_fogMieColor = atmosFogColor.mie;
  150. v_fogRayleighColor = atmosFogColor.rayleigh;
  151. #endif
  152. #if defined(FOG) || defined(GROUND_ATMOSPHERE) || defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT)
  153. v_distance = length((czm_modelView3D * vec4(position3DWC, 1.0)).xyz);
  154. #endif
  155. #ifdef APPLY_MATERIAL
  156. float northPoleZ = czm_ellipsoidRadii.z;
  157. vec3 northPolePositionMC = vec3(0.0, 0.0, northPoleZ);
  158. vec3 ellipsoidNormal = normalize(v_positionMC); // For a sphere this is correct, but not generally for an ellipsoid.
  159. vec3 vectorEastMC = normalize(cross(northPolePositionMC - v_positionMC, ellipsoidNormal));
  160. float dotProd = abs(dot(ellipsoidNormal, v_normalMC));
  161. v_slope = acos(dotProd);
  162. vec3 normalRejected = ellipsoidNormal * dotProd;
  163. vec3 normalProjected = v_normalMC - normalRejected;
  164. vec3 aspectVector = normalize(normalProjected);
  165. v_aspect = acos(dot(aspectVector, vectorEastMC));
  166. float determ = dot(cross(vectorEastMC, aspectVector), ellipsoidNormal);
  167. v_aspect = czm_branchFreeTernary(determ < 0.0, 2.0 * czm_pi - v_aspect, v_aspect);
  168. v_height = height;
  169. #endif
  170. }