GlobeFS.glsl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //#define SHOW_TILE_BOUNDARIES
  2. uniform vec4 u_initialColor;
  3. #if TEXTURE_UNITS > 0
  4. uniform sampler2D u_dayTextures[TEXTURE_UNITS];
  5. uniform vec4 u_dayTextureTranslationAndScale[TEXTURE_UNITS];
  6. #ifdef APPLY_ALPHA
  7. uniform float u_dayTextureAlpha[TEXTURE_UNITS];
  8. #endif
  9. #ifdef APPLY_BRIGHTNESS
  10. uniform float u_dayTextureBrightness[TEXTURE_UNITS];
  11. #endif
  12. #ifdef APPLY_CONTRAST
  13. uniform float u_dayTextureContrast[TEXTURE_UNITS];
  14. #endif
  15. #ifdef APPLY_HUE
  16. uniform float u_dayTextureHue[TEXTURE_UNITS];
  17. #endif
  18. #ifdef APPLY_SATURATION
  19. uniform float u_dayTextureSaturation[TEXTURE_UNITS];
  20. #endif
  21. #ifdef APPLY_GAMMA
  22. uniform float u_dayTextureOneOverGamma[TEXTURE_UNITS];
  23. #endif
  24. uniform vec4 u_dayTextureTexCoordsRectangle[TEXTURE_UNITS];
  25. #endif
  26. #ifdef SHOW_REFLECTIVE_OCEAN
  27. uniform sampler2D u_waterMask;
  28. uniform vec4 u_waterMaskTranslationAndScale;
  29. uniform float u_zoomedOutOceanSpecularIntensity;
  30. #endif
  31. #ifdef SHOW_OCEAN_WAVES
  32. uniform sampler2D u_oceanNormalMap;
  33. #endif
  34. #ifdef ENABLE_DAYNIGHT_SHADING
  35. uniform vec2 u_lightingFadeDistance;
  36. #endif
  37. varying vec3 v_positionMC;
  38. varying vec3 v_positionEC;
  39. varying vec2 v_textureCoordinates;
  40. varying vec3 v_normalMC;
  41. varying vec3 v_normalEC;
  42. vec4 sampleAndBlend(
  43. vec4 previousColor,
  44. sampler2D texture,
  45. vec2 tileTextureCoordinates,
  46. vec4 textureCoordinateRectangle,
  47. vec4 textureCoordinateTranslationAndScale,
  48. float textureAlpha,
  49. float textureBrightness,
  50. float textureContrast,
  51. float textureHue,
  52. float textureSaturation,
  53. float textureOneOverGamma)
  54. {
  55. // This crazy step stuff sets the alpha to 0.0 if this following condition is true:
  56. // tileTextureCoordinates.s < textureCoordinateRectangle.s ||
  57. // tileTextureCoordinates.s > textureCoordinateRectangle.p ||
  58. // tileTextureCoordinates.t < textureCoordinateRectangle.t ||
  59. // tileTextureCoordinates.t > textureCoordinateRectangle.q
  60. // In other words, the alpha is zero if the fragment is outside the rectangle
  61. // covered by this texture. Would an actual 'if' yield better performance?
  62. vec2 alphaMultiplier = step(textureCoordinateRectangle.st, tileTextureCoordinates);
  63. textureAlpha = textureAlpha * alphaMultiplier.x * alphaMultiplier.y;
  64. alphaMultiplier = step(vec2(0.0), textureCoordinateRectangle.pq - tileTextureCoordinates);
  65. textureAlpha = textureAlpha * alphaMultiplier.x * alphaMultiplier.y;
  66. vec2 translation = textureCoordinateTranslationAndScale.xy;
  67. vec2 scale = textureCoordinateTranslationAndScale.zw;
  68. vec2 textureCoordinates = tileTextureCoordinates * scale + translation;
  69. vec4 sample = texture2D(texture, textureCoordinates);
  70. vec3 color = sample.rgb;
  71. float alpha = sample.a;
  72. #ifdef APPLY_BRIGHTNESS
  73. color = mix(vec3(0.0), color, textureBrightness);
  74. #endif
  75. #ifdef APPLY_CONTRAST
  76. color = mix(vec3(0.5), color, textureContrast);
  77. #endif
  78. #ifdef APPLY_HUE
  79. color = czm_hue(color, textureHue);
  80. #endif
  81. #ifdef APPLY_SATURATION
  82. color = czm_saturation(color, textureSaturation);
  83. #endif
  84. #ifdef APPLY_GAMMA
  85. color = pow(color, vec3(textureOneOverGamma));
  86. #endif
  87. float sourceAlpha = alpha * textureAlpha;
  88. float outAlpha = mix(previousColor.a, 1.0, sourceAlpha);
  89. vec3 outColor = mix(previousColor.rgb * previousColor.a, color, sourceAlpha) / outAlpha;
  90. return vec4(outColor, outAlpha);
  91. }
  92. vec4 computeDayColor(vec4 initialColor, vec2 textureCoordinates);
  93. vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat3 enuToEye, vec4 imageryColor, float specularMapValue);
  94. void main()
  95. {
  96. // The clamp below works around an apparent bug in Chrome Canary v23.0.1241.0
  97. // where the fragment shader sees textures coordinates < 0.0 and > 1.0 for the
  98. // fragments on the edges of tiles even though the vertex shader is outputting
  99. // coordinates strictly in the 0-1 range.
  100. vec4 color = computeDayColor(u_initialColor, clamp(v_textureCoordinates, 0.0, 1.0));
  101. #ifdef SHOW_TILE_BOUNDARIES
  102. if (v_textureCoordinates.x < (1.0/256.0) || v_textureCoordinates.x > (255.0/256.0) ||
  103. v_textureCoordinates.y < (1.0/256.0) || v_textureCoordinates.y > (255.0/256.0))
  104. {
  105. color = vec4(1.0, 0.0, 0.0, 1.0);
  106. }
  107. #endif
  108. #if defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_DAYNIGHT_SHADING)
  109. vec3 normalMC = normalize(czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0))); // normalized surface normal in model coordinates
  110. vec3 normalEC = normalize(czm_normal3D * normalMC); // normalized surface normal in eye coordiantes
  111. #elif defined(ENABLE_VERTEX_LIGHTING)
  112. vec3 normalMC = normalize(v_normalMC); // normalized surface normal in model coordinates
  113. vec3 normalEC = normalize(v_normalEC); // normalized surface normal in eye coordiantes
  114. #endif
  115. #ifdef SHOW_REFLECTIVE_OCEAN
  116. vec2 waterMaskTranslation = u_waterMaskTranslationAndScale.xy;
  117. vec2 waterMaskScale = u_waterMaskTranslationAndScale.zw;
  118. vec2 waterMaskTextureCoordinates = v_textureCoordinates * waterMaskScale + waterMaskTranslation;
  119. float mask = texture2D(u_waterMask, waterMaskTextureCoordinates).r;
  120. if (mask > 0.0)
  121. {
  122. mat3 enuToEye = czm_eastNorthUpToEyeCoordinates(v_positionMC, normalEC);
  123. vec2 ellipsoidTextureCoordinates = czm_ellipsoidWgs84TextureCoordinates(normalMC);
  124. vec2 ellipsoidFlippedTextureCoordinates = czm_ellipsoidWgs84TextureCoordinates(normalMC.zyx);
  125. vec2 textureCoordinates = mix(ellipsoidTextureCoordinates, ellipsoidFlippedTextureCoordinates, czm_morphTime * smoothstep(0.9, 0.95, normalMC.z));
  126. color = computeWaterColor(v_positionEC, textureCoordinates, enuToEye, color, mask);
  127. }
  128. #endif
  129. #ifdef ENABLE_VERTEX_LIGHTING
  130. float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalEC) * 0.9 + 0.3, 0.0, 1.0);
  131. gl_FragColor = vec4(color.rgb * diffuseIntensity, color.a);
  132. #elif defined(ENABLE_DAYNIGHT_SHADING)
  133. float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalEC) * 5.0 + 0.3, 0.0, 1.0);
  134. float cameraDist = length(czm_view[3]);
  135. float fadeOutDist = u_lightingFadeDistance.x;
  136. float fadeInDist = u_lightingFadeDistance.y;
  137. float t = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);
  138. diffuseIntensity = mix(1.0, diffuseIntensity, t);
  139. gl_FragColor = vec4(color.rgb * diffuseIntensity, color.a);
  140. #else
  141. gl_FragColor = color;
  142. #endif
  143. }
  144. #ifdef SHOW_REFLECTIVE_OCEAN
  145. float waveFade(float edge0, float edge1, float x)
  146. {
  147. float y = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  148. return pow(1.0 - y, 5.0);
  149. }
  150. float linearFade(float edge0, float edge1, float x)
  151. {
  152. return clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  153. }
  154. // Based on water rendering by Jonas Wagner:
  155. // http://29a.ch/2012/7/19/webgl-terrain-rendering-water-fog
  156. // low altitude wave settings
  157. const float oceanFrequencyLowAltitude = 825000.0;
  158. const float oceanAnimationSpeedLowAltitude = 0.004;
  159. const float oceanOneOverAmplitudeLowAltitude = 1.0 / 2.0;
  160. const float oceanSpecularIntensity = 0.5;
  161. // high altitude wave settings
  162. const float oceanFrequencyHighAltitude = 125000.0;
  163. const float oceanAnimationSpeedHighAltitude = 0.008;
  164. const float oceanOneOverAmplitudeHighAltitude = 1.0 / 2.0;
  165. vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat3 enuToEye, vec4 imageryColor, float specularMapValue)
  166. {
  167. vec3 positionToEyeEC = -positionEyeCoordinates;
  168. float positionToEyeECLength = length(positionToEyeEC);
  169. // The double normalize below works around a bug in Firefox on Android devices.
  170. vec3 normalizedpositionToEyeEC = normalize(normalize(positionToEyeEC));
  171. // Fade out the waves as the camera moves far from the surface.
  172. float waveIntensity = waveFade(70000.0, 1000000.0, positionToEyeECLength);
  173. #ifdef SHOW_OCEAN_WAVES
  174. // high altitude waves
  175. float time = czm_frameNumber * oceanAnimationSpeedHighAltitude;
  176. vec4 noise = czm_getWaterNoise(u_oceanNormalMap, textureCoordinates * oceanFrequencyHighAltitude, time, 0.0);
  177. vec3 normalTangentSpaceHighAltitude = vec3(noise.xy, noise.z * oceanOneOverAmplitudeHighAltitude);
  178. // low altitude waves
  179. time = czm_frameNumber * oceanAnimationSpeedLowAltitude;
  180. noise = czm_getWaterNoise(u_oceanNormalMap, textureCoordinates * oceanFrequencyLowAltitude, time, 0.0);
  181. vec3 normalTangentSpaceLowAltitude = vec3(noise.xy, noise.z * oceanOneOverAmplitudeLowAltitude);
  182. // blend the 2 wave layers based on distance to surface
  183. float highAltitudeFade = linearFade(0.0, 60000.0, positionToEyeECLength);
  184. float lowAltitudeFade = 1.0 - linearFade(20000.0, 60000.0, positionToEyeECLength);
  185. vec3 normalTangentSpace =
  186. (highAltitudeFade * normalTangentSpaceHighAltitude) +
  187. (lowAltitudeFade * normalTangentSpaceLowAltitude);
  188. normalTangentSpace = normalize(normalTangentSpace);
  189. // fade out the normal perturbation as we move farther from the water surface
  190. normalTangentSpace.xy *= waveIntensity;
  191. normalTangentSpace = normalize(normalTangentSpace);
  192. #else
  193. vec3 normalTangentSpace = vec3(0.0, 0.0, 1.0);
  194. #endif
  195. vec3 normalEC = enuToEye * normalTangentSpace;
  196. const vec3 waveHighlightColor = vec3(0.3, 0.45, 0.6);
  197. // Use diffuse light to highlight the waves
  198. float diffuseIntensity = czm_getLambertDiffuse(czm_sunDirectionEC, normalEC);
  199. vec3 diffuseHighlight = waveHighlightColor * diffuseIntensity;
  200. #ifdef SHOW_OCEAN_WAVES
  201. // Where diffuse light is low or non-existent, use wave highlights based solely on
  202. // the wave bumpiness and no particular light direction.
  203. float tsPerturbationRatio = normalTangentSpace.z;
  204. vec3 nonDiffuseHighlight = mix(waveHighlightColor * 5.0 * (1.0 - tsPerturbationRatio), vec3(0.0), diffuseIntensity);
  205. #else
  206. vec3 nonDiffuseHighlight = vec3(0.0);
  207. #endif
  208. // Add specular highlights in 3D, and in all modes when zoomed in.
  209. float specularIntensity = czm_getSpecular(czm_sunDirectionEC, normalizedpositionToEyeEC, normalEC, 10.0) + 0.25 * czm_getSpecular(czm_moonDirectionEC, normalizedpositionToEyeEC, normalEC, 10.0);
  210. float surfaceReflectance = mix(0.0, mix(u_zoomedOutOceanSpecularIntensity, oceanSpecularIntensity, waveIntensity), specularMapValue);
  211. float specular = specularIntensity * surfaceReflectance;
  212. return vec4(imageryColor.rgb + diffuseHighlight + nonDiffuseHighlight + specular, imageryColor.a);
  213. }
  214. #endif // #ifdef SHOW_REFLECTIVE_OCEAN