GlobeFS.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform vec4 u_initialColor;\n\
  3. \n\
  4. #if TEXTURE_UNITS > 0\n\
  5. uniform sampler2D u_dayTextures[TEXTURE_UNITS];\n\
  6. uniform vec4 u_dayTextureTranslationAndScale[TEXTURE_UNITS];\n\
  7. uniform bool u_dayTextureUseWebMercatorT[TEXTURE_UNITS];\n\
  8. \n\
  9. #ifdef APPLY_ALPHA\n\
  10. uniform float u_dayTextureAlpha[TEXTURE_UNITS];\n\
  11. #endif\n\
  12. \n\
  13. #ifdef APPLY_DAY_NIGHT_ALPHA\n\
  14. uniform float u_dayTextureNightAlpha[TEXTURE_UNITS];\n\
  15. uniform float u_dayTextureDayAlpha[TEXTURE_UNITS];\n\
  16. #endif\n\
  17. \n\
  18. #ifdef APPLY_SPLIT\n\
  19. uniform float u_dayTextureSplit[TEXTURE_UNITS];\n\
  20. #endif\n\
  21. \n\
  22. #ifdef APPLY_BRIGHTNESS\n\
  23. uniform float u_dayTextureBrightness[TEXTURE_UNITS];\n\
  24. #endif\n\
  25. \n\
  26. #ifdef APPLY_CONTRAST\n\
  27. uniform float u_dayTextureContrast[TEXTURE_UNITS];\n\
  28. #endif\n\
  29. \n\
  30. #ifdef APPLY_HUE\n\
  31. uniform float u_dayTextureHue[TEXTURE_UNITS];\n\
  32. #endif\n\
  33. \n\
  34. #ifdef APPLY_SATURATION\n\
  35. uniform float u_dayTextureSaturation[TEXTURE_UNITS];\n\
  36. #endif\n\
  37. \n\
  38. #ifdef APPLY_GAMMA\n\
  39. uniform float u_dayTextureOneOverGamma[TEXTURE_UNITS];\n\
  40. #endif\n\
  41. \n\
  42. #ifdef APPLY_IMAGERY_CUTOUT\n\
  43. uniform vec4 u_dayTextureCutoutRectangles[TEXTURE_UNITS];\n\
  44. #endif\n\
  45. \n\
  46. #ifdef APPLY_COLOR_TO_ALPHA\n\
  47. uniform vec4 u_colorsToAlpha[TEXTURE_UNITS];\n\
  48. #endif\n\
  49. \n\
  50. uniform vec4 u_dayTextureTexCoordsRectangle[TEXTURE_UNITS];\n\
  51. #endif\n\
  52. \n\
  53. #ifdef SHOW_REFLECTIVE_OCEAN\n\
  54. uniform sampler2D u_waterMask;\n\
  55. uniform vec4 u_waterMaskTranslationAndScale;\n\
  56. uniform float u_zoomedOutOceanSpecularIntensity;\n\
  57. #endif\n\
  58. \n\
  59. #ifdef SHOW_OCEAN_WAVES\n\
  60. uniform sampler2D u_oceanNormalMap;\n\
  61. #endif\n\
  62. \n\
  63. #if defined(ENABLE_DAYNIGHT_SHADING) || defined(GROUND_ATMOSPHERE)\n\
  64. uniform vec2 u_lightingFadeDistance;\n\
  65. #endif\n\
  66. \n\
  67. #ifdef TILE_LIMIT_RECTANGLE\n\
  68. uniform vec4 u_cartographicLimitRectangle;\n\
  69. #endif\n\
  70. \n\
  71. #ifdef GROUND_ATMOSPHERE\n\
  72. uniform vec2 u_nightFadeDistance;\n\
  73. #endif\n\
  74. \n\
  75. #ifdef ENABLE_CLIPPING_PLANES\n\
  76. uniform highp sampler2D u_clippingPlanes;\n\
  77. uniform mat4 u_clippingPlanesMatrix;\n\
  78. uniform vec4 u_clippingPlanesEdgeStyle;\n\
  79. #endif\n\
  80. \n\
  81. #if defined(GROUND_ATMOSPHERE) || defined(FOG) && defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING))\n\
  82. uniform float u_minimumBrightness;\n\
  83. #endif\n\
  84. \n\
  85. #ifdef COLOR_CORRECT\n\
  86. uniform vec3 u_hsbShift; // Hue, saturation, brightness\n\
  87. #endif\n\
  88. \n\
  89. #ifdef HIGHLIGHT_FILL_TILE\n\
  90. uniform vec4 u_fillHighlightColor;\n\
  91. #endif\n\
  92. \n\
  93. #ifdef TRANSLUCENT\n\
  94. uniform vec4 u_frontFaceAlphaByDistance;\n\
  95. uniform vec4 u_backFaceAlphaByDistance;\n\
  96. uniform vec4 u_translucencyRectangle;\n\
  97. #endif\n\
  98. \n\
  99. #ifdef UNDERGROUND_COLOR\n\
  100. uniform vec4 u_undergroundColor;\n\
  101. uniform vec4 u_undergroundColorAlphaByDistance;\n\
  102. #endif\n\
  103. \n\
  104. #ifdef ENABLE_VERTEX_LIGHTING\n\
  105. uniform float u_lambertDiffuseMultiplier;\n\
  106. #endif\n\
  107. \n\
  108. varying vec3 v_positionMC;\n\
  109. varying vec3 v_positionEC;\n\
  110. varying vec3 v_textureCoordinates;\n\
  111. varying vec3 v_normalMC;\n\
  112. varying vec3 v_normalEC;\n\
  113. \n\
  114. #ifdef APPLY_MATERIAL\n\
  115. varying float v_height;\n\
  116. varying float v_slope;\n\
  117. varying float v_aspect;\n\
  118. #endif\n\
  119. \n\
  120. #if defined(FOG) || defined(GROUND_ATMOSPHERE) || defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT)\n\
  121. varying float v_distance;\n\
  122. #endif\n\
  123. \n\
  124. #if defined(GROUND_ATMOSPHERE) || defined(FOG)\n\
  125. varying vec3 v_atmosphereRayleighColor;\n\
  126. varying vec3 v_atmosphereMieColor;\n\
  127. varying float v_atmosphereOpacity;\n\
  128. #endif\n\
  129. \n\
  130. #if defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT)\n\
  131. float interpolateByDistance(vec4 nearFarScalar, float distance)\n\
  132. {\n\
  133. float startDistance = nearFarScalar.x;\n\
  134. float startValue = nearFarScalar.y;\n\
  135. float endDistance = nearFarScalar.z;\n\
  136. float endValue = nearFarScalar.w;\n\
  137. float t = clamp((distance - startDistance) / (endDistance - startDistance), 0.0, 1.0);\n\
  138. return mix(startValue, endValue, t);\n\
  139. }\n\
  140. #endif\n\
  141. \n\
  142. #if defined(UNDERGROUND_COLOR) || defined(TRANSLUCENT) || defined(APPLY_MATERIAL)\n\
  143. vec4 alphaBlend(vec4 sourceColor, vec4 destinationColor)\n\
  144. {\n\
  145. return sourceColor * vec4(sourceColor.aaa, 1.0) + destinationColor * (1.0 - sourceColor.a);\n\
  146. }\n\
  147. #endif\n\
  148. \n\
  149. #ifdef TRANSLUCENT\n\
  150. bool inTranslucencyRectangle()\n\
  151. {\n\
  152. return\n\
  153. v_textureCoordinates.x > u_translucencyRectangle.x &&\n\
  154. v_textureCoordinates.x < u_translucencyRectangle.z &&\n\
  155. v_textureCoordinates.y > u_translucencyRectangle.y &&\n\
  156. v_textureCoordinates.y < u_translucencyRectangle.w;\n\
  157. }\n\
  158. #endif\n\
  159. \n\
  160. vec4 sampleAndBlend(\n\
  161. vec4 previousColor,\n\
  162. sampler2D textureToSample,\n\
  163. vec2 tileTextureCoordinates,\n\
  164. vec4 textureCoordinateRectangle,\n\
  165. vec4 textureCoordinateTranslationAndScale,\n\
  166. float textureAlpha,\n\
  167. float textureNightAlpha,\n\
  168. float textureDayAlpha,\n\
  169. float textureBrightness,\n\
  170. float textureContrast,\n\
  171. float textureHue,\n\
  172. float textureSaturation,\n\
  173. float textureOneOverGamma,\n\
  174. float split,\n\
  175. vec4 colorToAlpha,\n\
  176. float nightBlend)\n\
  177. {\n\
  178. // This crazy step stuff sets the alpha to 0.0 if this following condition is true:\n\
  179. // tileTextureCoordinates.s < textureCoordinateRectangle.s ||\n\
  180. // tileTextureCoordinates.s > textureCoordinateRectangle.p ||\n\
  181. // tileTextureCoordinates.t < textureCoordinateRectangle.t ||\n\
  182. // tileTextureCoordinates.t > textureCoordinateRectangle.q\n\
  183. // In other words, the alpha is zero if the fragment is outside the rectangle\n\
  184. // covered by this texture. Would an actual 'if' yield better performance?\n\
  185. vec2 alphaMultiplier = step(textureCoordinateRectangle.st, tileTextureCoordinates);\n\
  186. textureAlpha = textureAlpha * alphaMultiplier.x * alphaMultiplier.y;\n\
  187. \n\
  188. alphaMultiplier = step(vec2(0.0), textureCoordinateRectangle.pq - tileTextureCoordinates);\n\
  189. textureAlpha = textureAlpha * alphaMultiplier.x * alphaMultiplier.y;\n\
  190. \n\
  191. #if defined(APPLY_DAY_NIGHT_ALPHA) && defined(ENABLE_DAYNIGHT_SHADING)\n\
  192. textureAlpha *= mix(textureDayAlpha, textureNightAlpha, nightBlend);\n\
  193. #endif\n\
  194. \n\
  195. vec2 translation = textureCoordinateTranslationAndScale.xy;\n\
  196. vec2 scale = textureCoordinateTranslationAndScale.zw;\n\
  197. vec2 textureCoordinates = tileTextureCoordinates * scale + translation;\n\
  198. vec4 value = texture2D(textureToSample, textureCoordinates);\n\
  199. vec3 color = value.rgb;\n\
  200. float alpha = value.a;\n\
  201. \n\
  202. #ifdef APPLY_COLOR_TO_ALPHA\n\
  203. vec3 colorDiff = abs(color.rgb - colorToAlpha.rgb);\n\
  204. colorDiff.r = max(max(colorDiff.r, colorDiff.g), colorDiff.b);\n\
  205. alpha = czm_branchFreeTernary(colorDiff.r < colorToAlpha.a, 0.0, alpha);\n\
  206. #endif\n\
  207. \n\
  208. #if !defined(APPLY_GAMMA)\n\
  209. vec4 tempColor = czm_gammaCorrect(vec4(color, alpha));\n\
  210. color = tempColor.rgb;\n\
  211. alpha = tempColor.a;\n\
  212. #else\n\
  213. color = pow(color, vec3(textureOneOverGamma));\n\
  214. #endif\n\
  215. \n\
  216. #ifdef APPLY_SPLIT\n\
  217. float splitPosition = czm_splitPosition;\n\
  218. // Split to the left\n\
  219. if (split < 0.0 && gl_FragCoord.x > splitPosition) {\n\
  220. alpha = 0.0;\n\
  221. }\n\
  222. // Split to the right\n\
  223. else if (split > 0.0 && gl_FragCoord.x < splitPosition) {\n\
  224. alpha = 0.0;\n\
  225. }\n\
  226. #endif\n\
  227. \n\
  228. #ifdef APPLY_BRIGHTNESS\n\
  229. color = mix(vec3(0.0), color, textureBrightness);\n\
  230. #endif\n\
  231. \n\
  232. #ifdef APPLY_CONTRAST\n\
  233. color = mix(vec3(0.5), color, textureContrast);\n\
  234. #endif\n\
  235. \n\
  236. #ifdef APPLY_HUE\n\
  237. color = czm_hue(color, textureHue);\n\
  238. #endif\n\
  239. \n\
  240. #ifdef APPLY_SATURATION\n\
  241. color = czm_saturation(color, textureSaturation);\n\
  242. #endif\n\
  243. \n\
  244. float sourceAlpha = alpha * textureAlpha;\n\
  245. float outAlpha = mix(previousColor.a, 1.0, sourceAlpha);\n\
  246. outAlpha += sign(outAlpha) - 1.0;\n\
  247. \n\
  248. vec3 outColor = mix(previousColor.rgb * previousColor.a, color, sourceAlpha) / outAlpha;\n\
  249. \n\
  250. // When rendering imagery for a tile in multiple passes,\n\
  251. // some GPU/WebGL implementation combinations will not blend fragments in\n\
  252. // additional passes correctly if their computation includes an unmasked\n\
  253. // divide-by-zero operation,\n\
  254. // even if it's not in the output or if the output has alpha zero.\n\
  255. //\n\
  256. // For example, without sanitization for outAlpha,\n\
  257. // this renders without artifacts:\n\
  258. // if (outAlpha == 0.0) { outColor = vec3(0.0); }\n\
  259. //\n\
  260. // but using czm_branchFreeTernary will cause portions of the tile that are\n\
  261. // alpha-zero in the additional pass to render as black instead of blending\n\
  262. // with the previous pass:\n\
  263. // outColor = czm_branchFreeTernary(outAlpha == 0.0, vec3(0.0), outColor);\n\
  264. //\n\
  265. // So instead, sanitize against divide-by-zero,\n\
  266. // store this state on the sign of outAlpha, and correct on return.\n\
  267. \n\
  268. return vec4(outColor, max(outAlpha, 0.0));\n\
  269. }\n\
  270. \n\
  271. vec3 colorCorrect(vec3 rgb) {\n\
  272. #ifdef COLOR_CORRECT\n\
  273. // Convert rgb color to hsb\n\
  274. vec3 hsb = czm_RGBToHSB(rgb);\n\
  275. // Perform hsb shift\n\
  276. hsb.x += u_hsbShift.x; // hue\n\
  277. hsb.y = clamp(hsb.y + u_hsbShift.y, 0.0, 1.0); // saturation\n\
  278. hsb.z = hsb.z > czm_epsilon7 ? hsb.z + u_hsbShift.z : 0.0; // brightness\n\
  279. // Convert shifted hsb back to rgb\n\
  280. rgb = czm_HSBToRGB(hsb);\n\
  281. #endif\n\
  282. return rgb;\n\
  283. }\n\
  284. \n\
  285. vec4 computeDayColor(vec4 initialColor, vec3 textureCoordinates, float nightBlend);\n\
  286. vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat3 enuToEye, vec4 imageryColor, float specularMapValue, float fade);\n\
  287. \n\
  288. const float fExposure = 2.0;\n\
  289. \n\
  290. vec3 computeEllipsoidPosition()\n\
  291. {\n\
  292. float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0), 1.0);\n\
  293. vec2 xy = gl_FragCoord.xy / czm_viewport.zw * 2.0 - vec2(1.0);\n\
  294. xy *= czm_viewport.zw * mpp * 0.5;\n\
  295. \n\
  296. vec3 direction = normalize(vec3(xy, -czm_currentFrustum.x));\n\
  297. czm_ray ray = czm_ray(vec3(0.0), direction);\n\
  298. \n\
  299. vec3 ellipsoid_center = czm_view[3].xyz;\n\
  300. \n\
  301. czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid_center, czm_ellipsoidInverseRadii);\n\
  302. \n\
  303. vec3 ellipsoidPosition = czm_pointAlongRay(ray, intersection.start);\n\
  304. return (czm_inverseView * vec4(ellipsoidPosition, 1.0)).xyz;\n\
  305. }\n\
  306. \n\
  307. void main()\n\
  308. {\n\
  309. #ifdef TILE_LIMIT_RECTANGLE\n\
  310. if (v_textureCoordinates.x < u_cartographicLimitRectangle.x || u_cartographicLimitRectangle.z < v_textureCoordinates.x ||\n\
  311. v_textureCoordinates.y < u_cartographicLimitRectangle.y || u_cartographicLimitRectangle.w < v_textureCoordinates.y)\n\
  312. {\n\
  313. discard;\n\
  314. }\n\
  315. #endif\n\
  316. \n\
  317. #ifdef ENABLE_CLIPPING_PLANES\n\
  318. float clipDistance = clip(gl_FragCoord, u_clippingPlanes, u_clippingPlanesMatrix);\n\
  319. #endif\n\
  320. \n\
  321. #if defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_DAYNIGHT_SHADING) || defined(HDR)\n\
  322. vec3 normalMC = czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0)); // normalized surface normal in model coordinates\n\
  323. vec3 normalEC = czm_normal3D * normalMC; // normalized surface normal in eye coordiantes\n\
  324. #endif\n\
  325. \n\
  326. #if defined(APPLY_DAY_NIGHT_ALPHA) && defined(ENABLE_DAYNIGHT_SHADING)\n\
  327. float nightBlend = 1.0 - clamp(czm_getLambertDiffuse(czm_lightDirectionEC, normalEC) * 5.0, 0.0, 1.0);\n\
  328. #else\n\
  329. float nightBlend = 0.0;\n\
  330. #endif\n\
  331. \n\
  332. // The clamp below works around an apparent bug in Chrome Canary v23.0.1241.0\n\
  333. // where the fragment shader sees textures coordinates < 0.0 and > 1.0 for the\n\
  334. // fragments on the edges of tiles even though the vertex shader is outputting\n\
  335. // coordinates strictly in the 0-1 range.\n\
  336. vec4 color = computeDayColor(u_initialColor, clamp(v_textureCoordinates, 0.0, 1.0), nightBlend);\n\
  337. \n\
  338. #ifdef SHOW_TILE_BOUNDARIES\n\
  339. if (v_textureCoordinates.x < (1.0/256.0) || v_textureCoordinates.x > (255.0/256.0) ||\n\
  340. v_textureCoordinates.y < (1.0/256.0) || v_textureCoordinates.y > (255.0/256.0))\n\
  341. {\n\
  342. color = vec4(1.0, 0.0, 0.0, 1.0);\n\
  343. }\n\
  344. #endif\n\
  345. \n\
  346. #if defined(ENABLE_DAYNIGHT_SHADING) || defined(GROUND_ATMOSPHERE)\n\
  347. float cameraDist;\n\
  348. if (czm_sceneMode == czm_sceneMode2D)\n\
  349. {\n\
  350. cameraDist = max(czm_frustumPlanes.x - czm_frustumPlanes.y, czm_frustumPlanes.w - czm_frustumPlanes.z) * 0.5;\n\
  351. }\n\
  352. else if (czm_sceneMode == czm_sceneModeColumbusView)\n\
  353. {\n\
  354. cameraDist = -czm_view[3].z;\n\
  355. }\n\
  356. else\n\
  357. {\n\
  358. cameraDist = length(czm_view[3]);\n\
  359. }\n\
  360. float fadeOutDist = u_lightingFadeDistance.x;\n\
  361. float fadeInDist = u_lightingFadeDistance.y;\n\
  362. if (czm_sceneMode != czm_sceneMode3D) {\n\
  363. vec3 radii = czm_ellipsoidRadii;\n\
  364. float maxRadii = max(radii.x, max(radii.y, radii.z));\n\
  365. fadeOutDist -= maxRadii;\n\
  366. fadeInDist -= maxRadii;\n\
  367. }\n\
  368. float fade = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);\n\
  369. #else\n\
  370. float fade = 0.0;\n\
  371. #endif\n\
  372. \n\
  373. #ifdef SHOW_REFLECTIVE_OCEAN\n\
  374. vec2 waterMaskTranslation = u_waterMaskTranslationAndScale.xy;\n\
  375. vec2 waterMaskScale = u_waterMaskTranslationAndScale.zw;\n\
  376. vec2 waterMaskTextureCoordinates = v_textureCoordinates.xy * waterMaskScale + waterMaskTranslation;\n\
  377. waterMaskTextureCoordinates.y = 1.0 - waterMaskTextureCoordinates.y;\n\
  378. \n\
  379. float mask = texture2D(u_waterMask, waterMaskTextureCoordinates).r;\n\
  380. \n\
  381. if (mask > 0.0)\n\
  382. {\n\
  383. mat3 enuToEye = czm_eastNorthUpToEyeCoordinates(v_positionMC, normalEC);\n\
  384. \n\
  385. vec2 ellipsoidTextureCoordinates = czm_ellipsoidWgs84TextureCoordinates(normalMC);\n\
  386. vec2 ellipsoidFlippedTextureCoordinates = czm_ellipsoidWgs84TextureCoordinates(normalMC.zyx);\n\
  387. \n\
  388. vec2 textureCoordinates = mix(ellipsoidTextureCoordinates, ellipsoidFlippedTextureCoordinates, czm_morphTime * smoothstep(0.9, 0.95, normalMC.z));\n\
  389. \n\
  390. color = computeWaterColor(v_positionEC, textureCoordinates, enuToEye, color, mask, fade);\n\
  391. }\n\
  392. #endif\n\
  393. \n\
  394. #ifdef APPLY_MATERIAL\n\
  395. czm_materialInput materialInput;\n\
  396. materialInput.st = v_textureCoordinates.st;\n\
  397. materialInput.normalEC = normalize(v_normalEC);\n\
  398. materialInput.positionToEyeEC = -v_positionEC;\n\
  399. materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(v_positionMC, normalize(v_normalEC)); \n\
  400. materialInput.slope = v_slope;\n\
  401. materialInput.height = v_height;\n\
  402. materialInput.aspect = v_aspect;\n\
  403. czm_material material = czm_getMaterial(materialInput);\n\
  404. vec4 materialColor = vec4(material.diffuse, material.alpha);\n\
  405. color = alphaBlend(materialColor, color);\n\
  406. #endif\n\
  407. \n\
  408. #ifdef ENABLE_VERTEX_LIGHTING\n\
  409. float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_lightDirectionEC, normalize(v_normalEC)) * u_lambertDiffuseMultiplier + 0.3, 0.0, 1.0);\n\
  410. vec4 finalColor = vec4(color.rgb * czm_lightColor * diffuseIntensity, color.a);\n\
  411. #elif defined(ENABLE_DAYNIGHT_SHADING)\n\
  412. float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_lightDirectionEC, normalEC) * 5.0 + 0.3, 0.0, 1.0);\n\
  413. diffuseIntensity = mix(1.0, diffuseIntensity, fade);\n\
  414. vec4 finalColor = vec4(color.rgb * czm_lightColor * diffuseIntensity, color.a);\n\
  415. #else\n\
  416. vec4 finalColor = color;\n\
  417. #endif\n\
  418. \n\
  419. #ifdef ENABLE_CLIPPING_PLANES\n\
  420. vec4 clippingPlanesEdgeColor = vec4(1.0);\n\
  421. clippingPlanesEdgeColor.rgb = u_clippingPlanesEdgeStyle.rgb;\n\
  422. float clippingPlanesEdgeWidth = u_clippingPlanesEdgeStyle.a;\n\
  423. \n\
  424. if (clipDistance < clippingPlanesEdgeWidth)\n\
  425. {\n\
  426. finalColor = clippingPlanesEdgeColor;\n\
  427. }\n\
  428. #endif\n\
  429. \n\
  430. #ifdef HIGHLIGHT_FILL_TILE\n\
  431. finalColor = vec4(mix(finalColor.rgb, u_fillHighlightColor.rgb, u_fillHighlightColor.a), finalColor.a);\n\
  432. #endif\n\
  433. \n\
  434. #if defined(DYNAMIC_ATMOSPHERE_LIGHTING_FROM_SUN)\n\
  435. vec3 atmosphereLightDirection = czm_sunDirectionWC;\n\
  436. #else\n\
  437. vec3 atmosphereLightDirection = czm_lightDirectionWC;\n\
  438. #endif\n\
  439. \n\
  440. #if defined(GROUND_ATMOSPHERE) || defined(FOG)\n\
  441. if (!czm_backFacing())\n\
  442. {\n\
  443. bool dynamicLighting = false;\n\
  444. #if defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_DAYNIGHT_SHADING) || defined(ENABLE_VERTEX_LIGHTING))\n\
  445. dynamicLighting = true; \n\
  446. #endif\n\
  447. \n\
  448. vec3 rayleighColor;\n\
  449. vec3 mieColor;\n\
  450. float opacity;\n\
  451. \n\
  452. vec3 positionWC;\n\
  453. vec3 lightDirection;\n\
  454. \n\
  455. // When the camera is far away (camera distance > nightFadeOutDistance), the scattering is computed in the fragment shader.\n\
  456. // Otherwise, the scattering is computed in the vertex shader.\n\
  457. #ifdef PER_FRAGMENT_GROUND_ATMOSPHERE\n\
  458. positionWC = computeEllipsoidPosition();\n\
  459. lightDirection = czm_branchFreeTernary(dynamicLighting, atmosphereLightDirection, normalize(positionWC));\n\
  460. computeAtmosphereScattering(\n\
  461. positionWC,\n\
  462. lightDirection,\n\
  463. rayleighColor,\n\
  464. mieColor,\n\
  465. opacity\n\
  466. );\n\
  467. #else\n\
  468. positionWC = v_positionMC;\n\
  469. lightDirection = czm_branchFreeTernary(dynamicLighting, atmosphereLightDirection, normalize(positionWC));\n\
  470. rayleighColor = v_atmosphereRayleighColor;\n\
  471. mieColor = v_atmosphereMieColor;\n\
  472. opacity = v_atmosphereOpacity;\n\
  473. #endif\n\
  474. \n\
  475. rayleighColor = colorCorrect(rayleighColor);\n\
  476. mieColor = colorCorrect(mieColor);\n\
  477. \n\
  478. vec4 groundAtmosphereColor = computeAtmosphereColor(positionWC, lightDirection, rayleighColor, mieColor, opacity);\n\
  479. \n\
  480. // Fog is applied to tiles selected for fog, close to the Earth.\n\
  481. #ifdef FOG\n\
  482. vec3 fogColor = groundAtmosphereColor.rgb;\n\
  483. \n\
  484. // If there is lighting, apply that to the fog.\n\
  485. #if defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING))\n\
  486. float darken = clamp(dot(normalize(czm_viewerPositionWC), atmosphereLightDirection), u_minimumBrightness, 1.0);\n\
  487. fogColor *= darken; \n\
  488. #endif\n\
  489. \n\
  490. #ifndef HDR\n\
  491. fogColor.rgb = czm_acesTonemapping(fogColor.rgb);\n\
  492. fogColor.rgb = czm_inverseGamma(fogColor.rgb);\n\
  493. #endif\n\
  494. \n\
  495. const float modifier = 0.15;\n\
  496. finalColor = vec4(czm_fog(v_distance, finalColor.rgb, fogColor.rgb, modifier), finalColor.a);\n\
  497. \n\
  498. #else\n\
  499. // The transmittance is based on optical depth i.e. the length of segment of the ray inside the atmosphere.\n\
  500. // This value is larger near the \"circumference\", as it is further away from the camera. We use it to\n\
  501. // brighten up that area of the ground atmosphere.\n\
  502. const float transmittanceModifier = 0.5;\n\
  503. float transmittance = transmittanceModifier + clamp(1.0 - groundAtmosphereColor.a, 0.0, 1.0);\n\
  504. \n\
  505. vec3 finalAtmosphereColor = finalColor.rgb + groundAtmosphereColor.rgb * transmittance;\n\
  506. \n\
  507. #if defined(DYNAMIC_ATMOSPHERE_LIGHTING) && (defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING))\n\
  508. float fadeInDist = u_nightFadeDistance.x;\n\
  509. float fadeOutDist = u_nightFadeDistance.y;\n\
  510. \n\
  511. float sunlitAtmosphereIntensity = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.05, 1.0);\n\
  512. float darken = clamp(dot(normalize(positionWC), atmosphereLightDirection), 0.0, 1.0);\n\
  513. vec3 darkenendGroundAtmosphereColor = mix(groundAtmosphereColor.rgb, finalAtmosphereColor.rgb, darken);\n\
  514. \n\
  515. finalAtmosphereColor = mix(darkenendGroundAtmosphereColor, finalAtmosphereColor, sunlitAtmosphereIntensity);\n\
  516. #endif\n\
  517. \n\
  518. #ifndef HDR\n\
  519. finalAtmosphereColor.rgb = vec3(1.0) - exp(-fExposure * finalAtmosphereColor.rgb);\n\
  520. #else\n\
  521. finalAtmosphereColor.rgb = czm_saturation(finalAtmosphereColor.rgb, 1.6);\n\
  522. #endif\n\
  523. \n\
  524. finalColor.rgb = mix(finalColor.rgb, finalAtmosphereColor.rgb, fade);\n\
  525. #endif\n\
  526. }\n\
  527. #endif\n\
  528. \n\
  529. #ifdef UNDERGROUND_COLOR\n\
  530. if (czm_backFacing())\n\
  531. {\n\
  532. float distanceFromEllipsoid = max(czm_eyeHeight, 0.0);\n\
  533. float distance = max(v_distance - distanceFromEllipsoid, 0.0);\n\
  534. float blendAmount = interpolateByDistance(u_undergroundColorAlphaByDistance, distance);\n\
  535. vec4 undergroundColor = vec4(u_undergroundColor.rgb, u_undergroundColor.a * blendAmount);\n\
  536. finalColor = alphaBlend(undergroundColor, finalColor);\n\
  537. }\n\
  538. #endif\n\
  539. \n\
  540. #ifdef TRANSLUCENT\n\
  541. if (inTranslucencyRectangle())\n\
  542. {\n\
  543. vec4 alphaByDistance = gl_FrontFacing ? u_frontFaceAlphaByDistance : u_backFaceAlphaByDistance;\n\
  544. finalColor.a *= interpolateByDistance(alphaByDistance, v_distance);\n\
  545. }\n\
  546. #endif\n\
  547. \n\
  548. gl_FragColor = finalColor;\n\
  549. }\n\
  550. \n\
  551. \n\
  552. #ifdef SHOW_REFLECTIVE_OCEAN\n\
  553. \n\
  554. float waveFade(float edge0, float edge1, float x)\n\
  555. {\n\
  556. float y = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);\n\
  557. return pow(1.0 - y, 5.0);\n\
  558. }\n\
  559. \n\
  560. float linearFade(float edge0, float edge1, float x)\n\
  561. {\n\
  562. return clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);\n\
  563. }\n\
  564. \n\
  565. // Based on water rendering by Jonas Wagner:\n\
  566. // http://29a.ch/2012/7/19/webgl-terrain-rendering-water-fog\n\
  567. \n\
  568. // low altitude wave settings\n\
  569. const float oceanFrequencyLowAltitude = 825000.0;\n\
  570. const float oceanAnimationSpeedLowAltitude = 0.004;\n\
  571. const float oceanOneOverAmplitudeLowAltitude = 1.0 / 2.0;\n\
  572. const float oceanSpecularIntensity = 0.5;\n\
  573. \n\
  574. // high altitude wave settings\n\
  575. const float oceanFrequencyHighAltitude = 125000.0;\n\
  576. const float oceanAnimationSpeedHighAltitude = 0.008;\n\
  577. const float oceanOneOverAmplitudeHighAltitude = 1.0 / 2.0;\n\
  578. \n\
  579. vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat3 enuToEye, vec4 imageryColor, float maskValue, float fade)\n\
  580. {\n\
  581. vec3 positionToEyeEC = -positionEyeCoordinates;\n\
  582. float positionToEyeECLength = length(positionToEyeEC);\n\
  583. \n\
  584. // The double normalize below works around a bug in Firefox on Android devices.\n\
  585. vec3 normalizedPositionToEyeEC = normalize(normalize(positionToEyeEC));\n\
  586. \n\
  587. // Fade out the waves as the camera moves far from the surface.\n\
  588. float waveIntensity = waveFade(70000.0, 1000000.0, positionToEyeECLength);\n\
  589. \n\
  590. #ifdef SHOW_OCEAN_WAVES\n\
  591. // high altitude waves\n\
  592. float time = czm_frameNumber * oceanAnimationSpeedHighAltitude;\n\
  593. vec4 noise = czm_getWaterNoise(u_oceanNormalMap, textureCoordinates * oceanFrequencyHighAltitude, time, 0.0);\n\
  594. vec3 normalTangentSpaceHighAltitude = vec3(noise.xy, noise.z * oceanOneOverAmplitudeHighAltitude);\n\
  595. \n\
  596. // low altitude waves\n\
  597. time = czm_frameNumber * oceanAnimationSpeedLowAltitude;\n\
  598. noise = czm_getWaterNoise(u_oceanNormalMap, textureCoordinates * oceanFrequencyLowAltitude, time, 0.0);\n\
  599. vec3 normalTangentSpaceLowAltitude = vec3(noise.xy, noise.z * oceanOneOverAmplitudeLowAltitude);\n\
  600. \n\
  601. // blend the 2 wave layers based on distance to surface\n\
  602. float highAltitudeFade = linearFade(0.0, 60000.0, positionToEyeECLength);\n\
  603. float lowAltitudeFade = 1.0 - linearFade(20000.0, 60000.0, positionToEyeECLength);\n\
  604. vec3 normalTangentSpace =\n\
  605. (highAltitudeFade * normalTangentSpaceHighAltitude) +\n\
  606. (lowAltitudeFade * normalTangentSpaceLowAltitude);\n\
  607. normalTangentSpace = normalize(normalTangentSpace);\n\
  608. \n\
  609. // fade out the normal perturbation as we move farther from the water surface\n\
  610. normalTangentSpace.xy *= waveIntensity;\n\
  611. normalTangentSpace = normalize(normalTangentSpace);\n\
  612. #else\n\
  613. vec3 normalTangentSpace = vec3(0.0, 0.0, 1.0);\n\
  614. #endif\n\
  615. \n\
  616. vec3 normalEC = enuToEye * normalTangentSpace;\n\
  617. \n\
  618. const vec3 waveHighlightColor = vec3(0.3, 0.45, 0.6);\n\
  619. \n\
  620. // Use diffuse light to highlight the waves\n\
  621. float diffuseIntensity = czm_getLambertDiffuse(czm_lightDirectionEC, normalEC) * maskValue;\n\
  622. vec3 diffuseHighlight = waveHighlightColor * diffuseIntensity * (1.0 - fade);\n\
  623. \n\
  624. #ifdef SHOW_OCEAN_WAVES\n\
  625. // Where diffuse light is low or non-existent, use wave highlights based solely on\n\
  626. // the wave bumpiness and no particular light direction.\n\
  627. float tsPerturbationRatio = normalTangentSpace.z;\n\
  628. vec3 nonDiffuseHighlight = mix(waveHighlightColor * 5.0 * (1.0 - tsPerturbationRatio), vec3(0.0), diffuseIntensity);\n\
  629. #else\n\
  630. vec3 nonDiffuseHighlight = vec3(0.0);\n\
  631. #endif\n\
  632. \n\
  633. // Add specular highlights in 3D, and in all modes when zoomed in.\n\
  634. float specularIntensity = czm_getSpecular(czm_lightDirectionEC, normalizedPositionToEyeEC, normalEC, 10.0);\n\
  635. float surfaceReflectance = mix(0.0, mix(u_zoomedOutOceanSpecularIntensity, oceanSpecularIntensity, waveIntensity), maskValue);\n\
  636. float specular = specularIntensity * surfaceReflectance;\n\
  637. \n\
  638. #ifdef HDR\n\
  639. specular *= 1.4;\n\
  640. \n\
  641. float e = 0.2;\n\
  642. float d = 3.3;\n\
  643. float c = 1.7;\n\
  644. \n\
  645. vec3 color = imageryColor.rgb + (c * (vec3(e) + imageryColor.rgb * d) * (diffuseHighlight + nonDiffuseHighlight + specular));\n\
  646. #else\n\
  647. vec3 color = imageryColor.rgb + diffuseHighlight + nonDiffuseHighlight + specular;\n\
  648. #endif\n\
  649. \n\
  650. return vec4(color, imageryColor.a);\n\
  651. }\n\
  652. \n\
  653. #endif // #ifdef SHOW_REFLECTIVE_OCEAN\n\
  654. ";