GlobeFS.js 11 KB

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