SkyAtmosphereCommon.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "float interpolateByDistance(vec4 nearFarScalar, float distance)\n\
  3. {\n\
  4. float startDistance = nearFarScalar.x;\n\
  5. float startValue = nearFarScalar.y;\n\
  6. float endDistance = nearFarScalar.z;\n\
  7. float endValue = nearFarScalar.w;\n\
  8. float t = clamp((distance - startDistance) / (endDistance - startDistance), 0.0, 1.0);\n\
  9. return mix(startValue, endValue, t);\n\
  10. }\n\
  11. \n\
  12. vec3 getLightDirection(vec3 positionWC)\n\
  13. {\n\
  14. float lightEnum = u_radiiAndDynamicAtmosphereColor.z;\n\
  15. vec3 lightDirection =\n\
  16. positionWC * float(lightEnum == 0.0) +\n\
  17. czm_lightDirectionWC * float(lightEnum == 1.0) +\n\
  18. czm_sunDirectionWC * float(lightEnum == 2.0);\n\
  19. return normalize(lightDirection);\n\
  20. }\n\
  21. \n\
  22. void computeAtmosphereScattering(vec3 positionWC, vec3 lightDirection, out vec3 rayleighColor, out vec3 mieColor, out float opacity, out float underTranslucentGlobe)\n\
  23. {\n\
  24. float ellipsoidRadiiDifference = czm_ellipsoidRadii.x - czm_ellipsoidRadii.z;\n\
  25. \n\
  26. // Adjustment to the atmosphere radius applied based on the camera height.\n\
  27. float distanceAdjustMin = czm_ellipsoidRadii.x / 4.0;\n\
  28. float distanceAdjustMax = czm_ellipsoidRadii.x;\n\
  29. float distanceAdjustModifier = ellipsoidRadiiDifference / 2.0;\n\
  30. float distanceAdjust = distanceAdjustModifier * clamp((czm_eyeHeight - distanceAdjustMin) / (distanceAdjustMax - distanceAdjustMin), 0.0, 1.0);\n\
  31. \n\
  32. // Since atmosphere scattering assumes the atmosphere is a spherical shell, we compute an inner radius of the atmosphere best fit \n\
  33. // for the position on the ellipsoid.\n\
  34. float radiusAdjust = (ellipsoidRadiiDifference / 4.0) + distanceAdjust;\n\
  35. float atmosphereInnerRadius = (length(czm_viewerPositionWC) - czm_eyeHeight) - radiusAdjust;\n\
  36. \n\
  37. // Setup the primary ray: from the camera position to the vertex position.\n\
  38. vec3 cameraToPositionWC = positionWC - czm_viewerPositionWC;\n\
  39. vec3 cameraToPositionWCDirection = normalize(cameraToPositionWC);\n\
  40. czm_ray primaryRay = czm_ray(czm_viewerPositionWC, cameraToPositionWCDirection);\n\
  41. \n\
  42. underTranslucentGlobe = 0.0;\n\
  43. \n\
  44. // Brighten the sky atmosphere under the Earth's atmosphere when translucency is enabled.\n\
  45. #if defined(GLOBE_TRANSLUCENT)\n\
  46. \n\
  47. // Check for intersection with the inner radius of the atmopshere.\n\
  48. czm_raySegment primaryRayEarthIntersect = czm_raySphereIntersectionInterval(primaryRay, vec3(0.0), atmosphereInnerRadius + radiusAdjust);\n\
  49. if (primaryRayEarthIntersect.start > 0.0 && primaryRayEarthIntersect.stop > 0.0) {\n\
  50. \n\
  51. // Compute position on globe.\n\
  52. vec3 direction = normalize(positionWC);\n\
  53. czm_ray ellipsoidRay = czm_ray(positionWC, -direction);\n\
  54. czm_raySegment ellipsoidIntersection = czm_rayEllipsoidIntersectionInterval(ellipsoidRay, vec3(0.0), czm_ellipsoidInverseRadii);\n\
  55. vec3 onEarth = positionWC - (direction * ellipsoidIntersection.start);\n\
  56. \n\
  57. // Control the color using the camera angle.\n\
  58. float angle = dot(normalize(czm_viewerPositionWC), normalize(onEarth));\n\
  59. \n\
  60. // Control the opacity using the distance from Earth.\n\
  61. opacity = interpolateByDistance(vec4(0.0, 1.0, czm_ellipsoidRadii.x, 0.0), length(czm_viewerPositionWC - onEarth));\n\
  62. vec3 horizonColor = vec3(0.1, 0.2, 0.3);\n\
  63. vec3 nearColor = vec3(0.0);\n\
  64. \n\
  65. rayleighColor = mix(nearColor, horizonColor, exp(-angle) * opacity);\n\
  66. \n\
  67. // Set the traslucent flag to avoid alpha adjustment in computeFinalColor funciton.\n\
  68. underTranslucentGlobe = 1.0;\n\
  69. return;\n\
  70. }\n\
  71. #endif\n\
  72. \n\
  73. computeScattering(\n\
  74. primaryRay,\n\
  75. length(cameraToPositionWC),\n\
  76. lightDirection,\n\
  77. atmosphereInnerRadius,\n\
  78. rayleighColor,\n\
  79. mieColor,\n\
  80. opacity\n\
  81. );\n\
  82. \n\
  83. // Alter the opacity based on how close the viewer is to the ground.\n\
  84. // (0.0 = At edge of atmosphere, 1.0 = On ground)\n\
  85. float cameraHeight = czm_eyeHeight + atmosphereInnerRadius;\n\
  86. float atmosphereOuterRadius = atmosphereInnerRadius + ATMOSPHERE_THICKNESS;\n\
  87. opacity = clamp((atmosphereOuterRadius - cameraHeight) / (atmosphereOuterRadius - atmosphereInnerRadius), 0.0, 1.0);\n\
  88. \n\
  89. // Alter alpha based on time of day (0.0 = night , 1.0 = day)\n\
  90. float nightAlpha = (u_radiiAndDynamicAtmosphereColor.z != 0.0) ? clamp(dot(normalize(positionWC), lightDirection), 0.0, 1.0) : 1.0;\n\
  91. opacity *= pow(nightAlpha, 0.5);\n\
  92. }\n\
  93. ";