GlobeVS.glsl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. attribute vec4 position3DAndHeight;
  2. attribute vec4 textureCoordAndEncodedNormals;
  3. uniform vec3 u_center3D;
  4. uniform mat4 u_modifiedModelView;
  5. uniform vec4 u_tileRectangle;
  6. // Uniforms for 2D Mercator projection
  7. uniform vec2 u_southAndNorthLatitude;
  8. uniform vec3 u_southMercatorYLowAndHighAndOneOverHeight;
  9. varying vec3 v_positionMC;
  10. varying vec3 v_positionEC;
  11. varying vec2 v_textureCoordinates;
  12. varying vec3 v_normalMC;
  13. varying vec3 v_normalEC;
  14. // These functions are generated at runtime.
  15. vec4 getPosition(vec3 position3DWC);
  16. float get2DYPositionFraction();
  17. vec4 getPosition3DMode(vec3 position3DWC)
  18. {
  19. return czm_projection * (u_modifiedModelView * vec4(position3DAndHeight.xyz, 1.0));
  20. }
  21. float get2DMercatorYPositionFraction()
  22. {
  23. // The width of a tile at level 11, in radians and assuming a single root tile, is
  24. // 2.0 * czm_pi / pow(2.0, 11.0)
  25. // We want to just linearly interpolate the 2D position from the texture coordinates
  26. // when we're at this level or higher. The constant below is the expression
  27. // above evaluated and then rounded up at the 4th significant digit.
  28. const float maxTileWidth = 0.003068;
  29. float positionFraction = textureCoordAndEncodedNormals.y;
  30. float southLatitude = u_southAndNorthLatitude.x;
  31. float northLatitude = u_southAndNorthLatitude.y;
  32. if (northLatitude - southLatitude > maxTileWidth)
  33. {
  34. float southMercatorYLow = u_southMercatorYLowAndHighAndOneOverHeight.x;
  35. float southMercatorYHigh = u_southMercatorYLowAndHighAndOneOverHeight.y;
  36. float oneOverMercatorHeight = u_southMercatorYLowAndHighAndOneOverHeight.z;
  37. float currentLatitude = mix(southLatitude, northLatitude, textureCoordAndEncodedNormals.y);
  38. currentLatitude = clamp(currentLatitude, -czm_webMercatorMaxLatitude, czm_webMercatorMaxLatitude);
  39. positionFraction = czm_latitudeToWebMercatorFraction(currentLatitude, southMercatorYLow, southMercatorYHigh, oneOverMercatorHeight);
  40. }
  41. return positionFraction;
  42. }
  43. float get2DGeographicYPositionFraction()
  44. {
  45. return textureCoordAndEncodedNormals.y;
  46. }
  47. vec4 getPositionPlanarEarth(vec3 position3DWC, float height2D)
  48. {
  49. float yPositionFraction = get2DYPositionFraction();
  50. vec4 rtcPosition2D = vec4(height2D, mix(u_tileRectangle.st, u_tileRectangle.pq, vec2(textureCoordAndEncodedNormals.x, yPositionFraction)), 1.0);
  51. return czm_projection * (u_modifiedModelView * rtcPosition2D);
  52. }
  53. vec4 getPosition2DMode(vec3 position3DWC)
  54. {
  55. return getPositionPlanarEarth(position3DWC, 0.0);
  56. }
  57. vec4 getPositionColumbusViewMode(vec3 position3DWC)
  58. {
  59. return getPositionPlanarEarth(position3DWC, position3DAndHeight.w);
  60. }
  61. vec4 getPositionMorphingMode(vec3 position3DWC)
  62. {
  63. // We do not do RTC while morphing, so there is potential for jitter.
  64. // This is unlikely to be noticeable, though.
  65. float yPositionFraction = get2DYPositionFraction();
  66. vec4 position2DWC = vec4(0.0, mix(u_tileRectangle.st, u_tileRectangle.pq, vec2(textureCoordAndEncodedNormals.x, yPositionFraction)), 1.0);
  67. vec4 morphPosition = czm_columbusViewMorph(position2DWC, vec4(position3DWC, 1.0), czm_morphTime);
  68. return czm_modelViewProjection * morphPosition;
  69. }
  70. void main()
  71. {
  72. vec3 position3DWC = position3DAndHeight.xyz + u_center3D;
  73. gl_Position = getPosition(position3DWC);
  74. #if defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_DAYNIGHT_SHADING)
  75. v_positionEC = (czm_modelView3D * vec4(position3DWC, 1.0)).xyz;
  76. v_positionMC = position3DWC; // position in model coordinates
  77. #elif defined(ENABLE_VERTEX_LIGHTING)
  78. v_positionEC = (czm_modelView3D * vec4(position3DWC, 1.0)).xyz;
  79. v_positionMC = position3DWC; // position in model coordinates
  80. float encodedNormal = textureCoordAndEncodedNormals.z;
  81. v_normalMC = czm_octDecode(encodedNormal);
  82. v_normalEC = czm_normal3D * v_normalMC;
  83. #endif
  84. v_textureCoordinates = textureCoordAndEncodedNormals.xy;
  85. }